Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: interpolation with filetest operators

by wog (Curate)
on Dec 09, 2001 at 06:20 UTC ( #130496=note: print w/replies, xml ) Need Help??

in reply to interpolation with filetest operators

Without resorting to string eval everytime you could use a dispatch table to do this:

{ # compile each test once... my %file_test = map { $_ => eval "sub { -$_ \$_[0] }" } qw(r w x o R W X O e z s f d l p S b c t u g k T B M A C); sub FileCheck { my($file,$perm,$badness) = @_; die "invalid file test" unless exists $file_test{$perm}; unless ($file_test{$perm}->($file)) { if ($badness eq 'die') { die "\nError (fatal) accessing $file: $!"; } elsif ($badness eq 'warn') { warn "\nError (non-fatal) accessing $file: $!"; } } } }

This also makes it relatively simple to create "new" filetests, should you want to...

If you use eval "-$perm... I would advise checking $@ to see if you had an invalid filetest requested.

update: added $_ => so it works. Much apologies. (Also s/exsits/exists/)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://130496]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2017-01-21 06:16 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (182 votes). Check out past polls.