Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

interpolation with filetest operators

by ybiC (Prior)
on Dec 09, 2001 at 06:02 UTC ( #130492=perlquestion: print w/replies, xml ) Need Help??

ybiC has asked for the wisdom of the Perl Monks concerning the following question:

  Greetz fellow monks,
How can I replace the '-e' below with the $perm variable?   I've tried tips from this (superficially similar) thread to no avail.   Help?
sub FileCheck { my $file = $_[0]; my $perm = $_[1]; my $ooot = $_[2]; ## unless (-$perm $file) { unless (-e $file) { if ($ooot eq 'die') { die "\nError (fatal) accessing $file: $!"; } elsif ($ooot eq 'warn') { warn "\nError (non-fatal)accessing $file: $!"; } } }

    striving toward Perl Adept
    (it's pronounced "why-bick")

Replies are listed 'Best First'.
Re: interpolation with filetest operators
by wog (Curate) on Dec 09, 2001 at 06:20 UTC
    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/)

Re: interpolation with filetest operators
by Chmrr (Vicar) on Dec 09, 2001 at 06:08 UTC

    Looks like eval "-$perm \$file" will do the trick.

    Update: Since testing shows that it works, I made it a statement, not a question.

    Update 2: $TIMTOWTDI = eval "-$perm \"$file\""

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re (tilly) 1: interpolation with filetest operators
by tilly (Archbishop) on Dec 09, 2001 at 07:51 UTC
    An OT stylistic note.

    I find that explicit referencing of @_ inside of functions is problematic. It is too easy when modifying code to accidentally access this when you meant that. For which reason the two main styles you see go like:

    sub FileCheck { my $file = shift; my $perm = shift; my $oot = shift; # etc } # or sub FileCheck { my ($file, $perm, $oot) = @_; # etc }
    And now if you decide while developing code that you want to change the order of arguments, you are much less likely to make a mistake.

    (I also don't much like StudlyCaps, but that is one of those "important irrelevancies". It is good to be consistent about naming patterns, but which consistent choice you make is irrelevant. InOtherWords I don't code that way, but there is nothing wrong with doing so.)

Re: interpolation with filetest operators
by Anonymous Monk on Dec 09, 2001 at 06:13 UTC
Re: interpolation with filetest operators
by Aighearach (Initiate) on Dec 09, 2001 at 06:16 UTC
    Does "perm" mean "permissions?" If so, you need to stat() the file, and look at the results... see perldoc -f stat

    if not, then maybe what you want is eval "-$perm \$file"
    Snazzy tagline here

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-12-10 13:17 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (40 votes). Check out past polls.