in reply to order of operations with conditionals at the end
As a general solution to determining the order of operations, you can add the -p option to -MO=Deparse; you write it like -MO=Deparse,-p. This adds parentheses showing how perl parses your code.
ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -MO=Deparse,-p -E ' > my $message = "Congrats" unless not defined $reward; > ' Global symbol "$reward" requires explicit package name at -e line 2. -e had compilation errors. use warnings; use strict 'refs'; BEGIN { $^H{'feature_unicode'} = q(1); $^H{'feature_say'} = q(1); $^H{'feature_state'} = q(1); $^H{'feature_switch'} = q(1); } ((not defined(${'reward'})) or (my $message = 'Congrats')); ken@ganymede: ~/tmp $
You'll see how you get an error about $message not being declared: this is different from not being defined. Here it is again with this variable being declared but left undefined.
ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -MO=Deparse,-p -E ' > my $reward; > my $message = "Congrats" unless not defined $reward; > ' use warnings; use strict 'refs'; BEGIN { $^H{'feature_unicode'} = q(1); $^H{'feature_say'} = q(1); $^H{'feature_state'} = q(1); $^H{'feature_switch'} = q(1); } my($reward); (defined($reward) and (my $message = 'Congrats')); -e syntax OK ken@ganymede: ~/tmp $
Note differences between the error and error-free versions:
See also:
-- Ken
|
|---|