Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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/)


Comment on Re: interpolation with filetest operators
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://130496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2014-09-17 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (74 votes), past polls