in reply to Spot the bug!
What's wrong with the function? Nothing!
The only bug I see is in the code under the spoiler tag. You can't rely on a function not modifying $@, $!, $1, etc. If you need to pass these variables to a function, pass them by copy.
print foo( 3 ), "\n"; # 3 $@ = 3; print foo( $@ ), "\n"; # 0 Bad. Passing special var. $@ = 3; print foo( "$@" ), "\n"; # 3 There we go. $@ = 3; my $e = $@; print foo( $e ), "\n"; # 3 print $e, "\n"; # 3 Still avilable to us.
In theory, this problem exists whenever a global is passed to a function which modifies that global. $@ is a global modified by eval.
Rethrowing with die $@ considered harmful demonstrates a similar problem.
Update: Major rephrasing — I wasn't clear — but no new content except for the link.
|
|---|