in reply to using 'or' followed by ','
When the parens are there, you get the warning. When you comment the line with parens and uncomment the other, you still get only two lines printed to STDOUT, but nothing printed to STDERR.%h=qw/a 1 b 0 c 2/; while (($k,$v)=each %h) { $x=$v or warn("got a false value for key $k"),next; # $x=$v or warn "got a false value for key $k", next; print "got a true value for key $k: $v\n"; }
Apparently, when the parens are removed, the comma-separated things after "warn" are grouped and treated as args passed to warn -- and the second arg happens to be an expression that must be evaluated in order to pass the resulting value as an arg. But evaluation of "next" as the second arg has the effect of jumping directly to the next loop iteration -- so warn never gets called.
By putting parens around the single arg to "warn", you make sure that this expression is fully evaluated, and then the "next" expression is also evaluated, causing the jump to the next iteration. The comma binds tighter than "or", so the expressions separated by commas are evaluated as if they were a block relative to "or".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: using 'or' followed by ','
by pg (Canon) on Jul 22, 2005 at 02:25 UTC |