cheesestraws has asked for the wisdom of the Perl Monks concerning the following question:
Here's a minimal test case that reliably reproduces a problem I've been having. As I understand perl, the three sets of print statements ought to produce the same output each time.
use strict; no warnings "numeric"; print "Reusing one variable:\n"; my $val = "ff"; print "(" . ($val & "") . ")\n"; $val = 7; print "(" . ($val & "") . ")\n"; $val = "ff"; print "(" . ($val & "") . ")\n"; print "\n"; print "Using multiple variables:\n"; my $v1 = "ff"; print "(" . ($v1 & "") . ")\n"; my $v2 = 7; print "(" . ($v2 & "") . ")\n"; my $v3 = "ff"; print "(" . ($v3 & "") . ")\n"; print "\n"; print "Using a sub:\n"; sub xx { my $arg = shift; print "(" . ($arg & "") . ")\n"; } xx("ff"); xx(7); xx("ff");
Since xx has no side-effects except for output, there should be no state maintained between multiple calls to it.
However, what this code actually does seems to be depend on what version I'm running it under:
- Under 5.16.3 i386-openbsd and 5.14.2 x86_64-linux-gnu-thread-multi, it does what I'd expect, giving the output:
Reusing one variable: () (0) () Using multiple variables: () (0) () Using a sub: () (0) ()
- Under versions before 5.14, though, I get a different result (tested on v5.10.0 darwin-thread-multi-2level, 5.10.1 i486-linux-gnu-thread-multi and 5.12.4 darwin-thread-multi-2level, which are the versions I currently can get access to):
Reusing one variable: () (0) () Using multiple variables: () (0) () Using a sub: () (0) (0)
The output of the last set of prints no longer matches the previous two sets, which suggests that there's some state being preserved between calls to the sub. It looks a bit like $arg's numeric status is somehow getting "stuck".
Putting a print inside the subroutine that prints $arg verifies that $arg is receiving the right value. Putting intermediate calls to other subroutines, or other code, in between the calls to xx doesn't seem to change anything, either.
I've trudged my way through the perldeltas between 5.12 and 5.14 and I couldn't see anything relevant, but then I don't really know what it'd come under, and I don't know much about the innards of perl, so it's possible I've completely missed something.
Can anyone shed some light on why this is happening?
Thanks!
|
---|