On Thu, Mar 14, 2002 at 10:32:56AM -0800, bob ackerman wrote:
> confusion i have with comma operator.
> docs seem to indicate it evaluates both sides, but i see examples where it
> looks like if it evaluates to false on LHS it doesn't evaluate RHS. i see
> this in examples of writing a case statement.
> $x='b';
> {
> ($x eq 'a') && (print 'a'), last;
> print 'x';
> }
> i see it work sortof sometimes - if $x isn't equal 'a', 'last' isn't
> evaulated and it prints 'x'.
> shouldn't it also evaluate 'last', whether the LHS of that line is true or
> not?
I'm not sure why you're seeing this result. last is always evaluated here:
Given $x = "a", you should see just an "a" printed. Given $x = "b" you
should see nothing printed. Are you certain you didn't introduce an error?
If you have a version of code that's printing the 'x' please paste it, along
with your usage of it to show it's printing the 'x'.
The expression "($x eq 'a') && (print 'a'), last" is equivalent to "(($x eq
'a') && (print 'a')), last". The last is evaluated regardless of of the
truth of the test. The expression (as you point out in a later email) "($x
eq 'a') and (print 'a'), last" is equivalent to "($x eq 'a') and ((print
'a', last)" because of the precedence of the and operator; the comma, and
the last, are only reached if the test is true.
It might help if you used the Deparse module like so:
perl -MO=Deparse -wle '$x = "b"; { ($x eq "a") and (print "a"), last; print "x"; }'
Notice that this has nothing to do with how the comma evaluates its
arguments, but when the branch with the comma in it is reached.
Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com
--
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]