The following snippet does not work properly:
$k{"foo"}=1; $k{"bar"}=2; if(not defined($k{"foo"}) && not defined($k{"bar"})){ print "Not defined\n"; } else{ print "Defined" }
Since both $ k {"foo"} and $ k {"bar"} are defined, the expected result is determined to be "Defined". However, running the code returns "Undefined".
Now, playing with the code, I realized that placing parentheses around each of the not defined() calls leads to the desired result:
if((not defined($k{"foo"})) && (not defined($k{"bar"}))){print "Not Defined"}
I suppose this has something to do with operator precedence, but can anyone explain what exactly is going on?
perl
terdon
source share