Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Just another Perl shrine
 
PerlMonks  

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: $!"; } } }

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

Comment on interpolation with filetest operators
Download Code
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: interpolation with filetest operators
by Anonymous Monk on Dec 09, 2001 at 06:13 UTC
Re: interpolation with filetest operators
by Aighearach 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

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 (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.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-04-19 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls