LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   listing files Net::FTP (https://www.linuxquestions.org/questions/programming-9/listing-files-net-ftp-555719/)

ghostdog74 05-23-2007 04:23 AM

you can set up a small test: eg
Code:

$var = open(FILE,"<file") or print "cannot open";
print "\$var is  " . $var . "\n" ;
close(FILE);
$var2 = open(FILE,"<file") || print  "cannot open";
print "\$var2 is " . $var2 . "\n";
close(FILE);

where "file" doesn't exist...
here's my output:
Code:

# ./test.pl
cannot open$var is
cannot open$var2 is 1


chrism01 05-23-2007 05:03 AM

From Perl docs ( http://perldoc.perl.org/perlop.html )

"Logical or and Exclusive Or

Binary "or" returns the logical disjunction of the two surrounding expressions. It's equivalent to || except for the very low precedence. This makes it useful for control flow

print FH $data or die "Can't write to FH: $!";

This means that it short-circuits: i.e., the right expression is evaluated only if the left expression is false. Due to its precedence, you should probably avoid using this for assignment, only for control flow.

$a = $b or $c; # bug: this is wrong
($a = $b) or $c; # really means this
$a = $b || $c; # better written this way

However, when it's a list-context assignment and you're trying to use "||" for control flow, you probably need "or" so that the assignment takes higher precedence.

@info = stat($file) || die; # oops, scalar sense of stat!
@info = stat($file) or die; # better, now @info gets its due

Then again, you could always use parentheses. "

See last 2 examples here.

Edit: re ghostdog's example above, I always use something like
... or print "Cannot open file <filename>: $!\n";
where $! is perl's built-in error code marker/special var (auto translates to error msg)


All times are GMT -5. The time now is 08:10 AM.