outputssub func { return (0, 1, 2) } if (1 and do { ($result) = func($param); $result }) { print "1\n"; } else { print "0\n"; } if (1 and ($result) = func($param)) { print "1\n"; } else { print "0\n"; }
0 1
($result) = func($param)
is a list assignment, and it happens to be in scalar context. List assignment in scalar context returns the number of elements returned by its RHS. (See Mini-Tutorial: Scalar vs List Assignment Operator.)
If func returns three values, the second snippet is equivalent to
if (... and do { ($result) = func($param); 3 })
which is different than
if (... and do { ($result) = func($param); $result })
As shown above, the difference will matter when func returns more than one value and the first is false.
You can avoid the do using a list slice.
if (... and $result = (func($param))[0])
Scalar assignment returns its LHS.
In reply to Re: Is there any difference between these two statement?
by ikegami
in thread Is there any difference between these two statement?
by PerlOnTheWay
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |