So the eval must optimize itself based on the context, but a normal non-eval'ed block ending with $a + $b would not optimize away, and (I think) still perform and return the addition, even though being in void context.reduce { print wantarray ? "array\n" : defined wantarray ? "scalar\n" : "void\n" } 1 .. 2; # prints "void"
Certainly I agree that you are right, that this must be a bug in List::Util. The block should be in scalar context since the return value is the important part of this operation. I really wonder how the module can get the return value of something that it executes in void context??
Also, I got some strange behavoir when I add a return statement to the eval. Both should behave the same way, but don't. Why on earth $a gets set to 4 for every addition is a mystery...
Update: sgifford claims to see scalar context from the block in his reply above -- running his code, I still get void context reported.. This is with Perl 5.8, and List::Util 1.07_00. But even in scalar context, he still gets the weird behavoir? The wonders never cease!my $result = reduce { print "eval: $a + $b\n"; eval "$a + $b" } 1 .. 4; print "result=$result\n\n"; # eval: 1 + 2 # eval: + 3 # eval: + 4 # result=4 $result = reduce { print "eval: return $a + $b\n"; eval "return $a + $b" } 1 .. 4; print "result=$result\n\n"; # eval: return 1 + 2 # eval: return 4 + 3 # eval: return 4 + 4 # result=4
blokhead
In reply to Re: Re: Re: An anomaly with List::Util::reduce and/or eval?
by blokhead
in thread An anomaly with List::Util::reduce and/or eval?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |