[tpm] Stupid question time!

Mike Stok mike at stok.ca
Fri Jan 29 13:03:09 PST 2010

On Jan 29, 2010, at 3:55 PM, Madison Kelly wrote:

> What is the difference between:
> if (not $foo && $bar ne "") {}
> if ((not $foo) && ($bar ne "")) {}
> I assumed it was mainly an aesthetic/readability thing, but I realize that no, it's logically different. Thanks for enlightening me?

It's a precedence thing, here's how to get perl to show you what it's doing:

ratdog:~ mike$ perl -MO=Deparse,-p -e 'if (not $foo && $bar ne "") {}'
if ((not ($foo && ($bar ne '')))) {
-e syntax OK
ratdog:~ mike$ perl -MO=Deparse,-p -e 'if ((not $foo) && ($bar ne "")) {}'
if (((not $foo) and ($bar ne ''))) {
-e syntax OK

The  "symbol" logical operators have a higher precedence than the "word" operators, so mixing them can be an exercise in memory and/or parenthesization.  See perldoc perlop.

Hope this helps,



Mike Stok <mike at stok.ca>

The "`Stok' disclaimers" apply.

More information about the toronto-pm mailing list