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

I eneter a script and want to go one way if either one of two files exist
or anouther way if neither of the two files exist. To the best of my knowledge || means or. So I make .
if (-e "data/$file.data") || (-e "data/$otherfile.data") { print " <BR>Hooray at least one of the the files exists"; } else { print "<BR>Hohum neither of the files exist"; }

So I tried
if (-e "data/$file.data") or (-e "data/$otherfile.data") { print " <BR>Hooray one of the files exist"; } else { print "<BR>Hohum neither of the files exist"; }

I cant find any information on this in the limited tutorials I have.
Can somone please show me the simple solution.

Replies are listed 'Best First'.
Re: The or thing.
by bobf (Monsignor) on Feb 17, 2006 at 05:28 UTC

    That code should have generated a syntax error. You're missing a set of parentheses around the two conditions in the if:

    if( (-e "data/$file.data") || (-e "data/$otherfile.data") )
    Please refer to perlsyn for documentation on if, and perlop for documentation on || and or.

Re: The or thing.
by planetscape (Chancellor) on Feb 17, 2006 at 07:06 UTC
Re: The or thing.
by spiritway (Vicar) on Feb 17, 2006 at 05:28 UTC

    It seems to me that you need to put parentheses around the entire 'if' statement (both tests):

    if ((-e "$file.data") || (-e "$otherfile.data"))

    When I run your test this way, I get the Hooray message. When I omit the enclosing parentheses, I can't even get the thing to run - so I'm wondering what it was that you were able to get working.

Re: The or thing.
by strat (Canon) on Feb 17, 2006 at 09:01 UTC

    Btw: if you just want to know if there is something with the name $file.data (e.g. a file, a directory, a link, a socket, ...), -e is correct. But if you want to know if it is a file and not a directory or socket or link or the like, you could better use -f, e.g

    if (-f "data/$file.data" or -f "data/$otherfile.data) { ...

    see: perldoc -f -f

    if you also want to execute the script from other directories (e.g. because it is in $PATH or %PATH%, like ls or dir), then the script would try to find "data/$file.data" in a wrong place. If you want to keep ./data/ relatively to your script but don't want to write an absolute path, you can use FindBin, e.g.

    use FindBin; my $dataPath = "$FindBin::Bin/data"; if (-f "$dataPath/$file.data" or -f "$dataPath/$otherdata.data") { ...

    this way is also often a good choise for Perl/CGI scripts because some webservers don't change there actual directory to the one your script is inside

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

A reply falls below the community's threshold of quality. You may see it by logging in.