Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Remembering values from multiple condition 'if'

by Bukowski (Deacon)
on Jul 01, 2002 at 17:36 UTC ( #178616=perlquestion: print w/replies, xml ) Need Help??

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

Hello gracious PerlMonks,

I have a short question, its perplexing me but its the end of the day and I am decaffeinated...

Let us say I have a series of filetests in an 'if' statement like this:

if (-e "$f" || -e "$a" || -e "$scratchfile" || -e "$fasta_output") { print "Files belonging to a previous version exist\n"; #delete old files somehow }else{ #continue with analysis }
If one, or more, of those files exists, how can I delete them by directly referencing the results of the 'if' statement, rather than writing 4 separate 'unlink's for each of the conditions tested? I am sure there is a more elegant way to do it than the way I will do it if I carry on uneducated...

Many thanks, in advance,

Bukowski - aka Dan (
"Coffee for the mind, Pizza for the body, Sushi for the soul" -Userfriendly

Replies are listed 'Best First'.
Re: Remembering values from multiple condition 'if'
by broquaint (Abbot) on Jul 01, 2002 at 17:44 UTC
    Perhaps it would be better it iterate through the files like so
    foreach my $file ($f, $a, $scratchfile, $fasta_output) { if(-e $file) { print "Files belonging to a previous version exist\n"; unlink $file; } else { ... } }
    Also note you don't have to quote the filenames like you might in a shell script as perl Does The Right Thing.


Re: Remembering values from multiple condition 'if'
by Corion (Patriarch) on Jul 01, 2002 at 17:46 UTC

    I think you're wanting to do too much in one step. I'd do it the following way :

    sub unlink_exists { my ($filename) = @_; if (-e $filename) { unlink $filename or die "Couldn't remove '$filename' : $!\n"; print "Removed $filename\n"; }; return 1; }; unlink_exists($_) for ($f,$a,$scratchfile,$fasta_output);

    This will, of course print up to 4 lines instead of one line, but it's much more readable in my opinion, and also provides you with much better diagnostics.

    It's not possible to write unlink_exists as a function that returns true if the file was detected and successfully removed like the following :

    sub unlink_exists { my ($filename) = @_; my $result = -e $filename; if ($result) { unlink $filename or die "Couldn't remove '$filename' : $!\n"; }; return $result; }; if ( unlink_exists($a) || unlink_exists($f) ... );

    because Perl does not fully evaluate expressions - it stops as soon as the expressions value is determined.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: Remembering values from multiple condition 'if'
by lestrrat (Deacon) on Jul 01, 2002 at 17:44 UTC

    When in doubt, rethink code structure... ;)

    for my $file ( qw/ file1 file2 file3 file4 ) { if( -e $file ) { unlink($file) || print STDERR "error unlinking $file: $!\n"; } }
      There's really no point in testing to see if an unlink might succeed before attempting it. Just do the unlink:
      unlink qw/ file1 file2 file3 file4 /;
      Although your way is cooler if you want clean error messages if something goes wrong.

      -- Randal L. Schwartz, Perl hacker

Re: Remembering values from multiple condition 'if'
by TheHobbit (Pilgrim) on Jul 01, 2002 at 21:40 UTC

    besides the solution proposed by other monks, there is the following

    use strict; my @files = grep ($_,map -e $_ && $_ , qw(foo foobar bar)); if (@files) { print "there are files\n"; #here @files holds the names of the files print(join(",",@files),"\n"); } else { print "no files\n"; }
    which places the existing file's names in an array.


    Leo TheHobbit
      Argh, I'm out of votes. Consider yourself ++ed. Just a question though, why don't you just do like so? my @files = grep -e, qw(foo foobar bar); ____________
      Makeshifts last the longest.

        Well, with that grep, @files will end up containing only ones, and you'll not even be able to sort out exactly 'which' files where there...


        Leo TheHobbit

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-01-20 12:19 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (56 votes). Check out past polls.