in reply to '||' vs 'or' with subroutines
Operator precedence is the cause of this. || has much higher precedence than 'or' does. Consider the following:
#!perl -l sub foo { print "Foo!"; return "foo"; } my $barbar=0 || foo(); print $barbar; print "---"; my $or =0 or foo(); print $or; __END__ Foo! foo --- Foo! 0
What this shows is that $var = $x || $y; is handled as $var=($x || $y); and $var = $x or $y; is handled as ($var=$x) or $y; which is actually a logical operator used as flow control and as such is pretty well equivelent to unless ($var=$x) { $y }. Where this gets interesting is that its a lot nicer to write open my $fh,$mode,$file or die "$file:$!" than it is to write my $fh; unless (open $fh,$mode,$file) { die "$file:$!" }
|
|---|