Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2024-07-21 17:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.