Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: interpolation with filetest operators

by wog (Curate)
on Dec 09, 2001 at 06:20 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found