in reply to Need explanation for: useless use of string ne in void context
The quick answer is that and binds less tight than parentheses or &&. The long answer is to first look at what Perl sees:
>perl -MO=Deparse -e "$File::Find::prune= not $recurse and ($File::Fin +d::dir ne $File::Find::topdir);" $File::Find::dir ne $File::Find::topdir if $File::Find::prune = !$recu +rse;
So we now see that Perl doesn't group what "we" see as an expression together, but splits it in two. To group together what we want together, we can use parentheses (like you did), or the sigil-versions of operators, which do bind tighter:
>perl -MO=Deparse -e "$File::Find::prune= not $recurse && ($File::Find +::dir ne $File::Find::topdir);" $File::Find::prune = !($recurse && $File::Find::dir ne $File::Find::to +pdir);
Having the two kinds of operators isn't all weird, because the more lax binding operators are incredibly convenient when writing code that depends on the return value of statements when you don't want parentheses:
open my $fh, $file or die "Couldn't open '$file': $!";
If or did bind as close as ||, then you would need parentheses:
open (my $fh, $file) || die "Couldn't open '$file': $!";
Otherwise, Perl interprets things "wrong again", and groups $filename || die ... together:
>perl -MO=Deparse -e "open my $fh, $filename || die qq(couldn't open $ +filename: $!)" open my $fh, $filename || die("couldn't open ${filename}: $!"); -e syntax OK >perl -MO=Deparse -e "open my $fh, $filename or die qq(couldn't open $ +filename: $!)" die "couldn't open ${filename}: $!" unless open my $fh, $filename; -e syntax OK
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need explanation for: useless use of string ne in void context
by JavaFan (Canon) on Jan 06, 2009 at 14:42 UTC |