Hello,
I am seeing some unexpected behaviour when nesting a function inside another.
The inner function keeps seeing the old version of variables declared in the outer function.
For example:
#!/usr/bin/perl use strict; sub outer_function { my ($v1, $v2) = @_; print STDERR "Outer \$v1: " . \$v1 . " = $v1\n"; return inner_function($v1) . ", $v2"; sub inner_function { print STDERR "Inner \$v1: " . \$v1 . " = $v1\n"; return $v1; } } print "First call: " . outer_function("A", "B") . "\n"; print "Secnd call: " . outer_function("X", "Y") . "\n";
I was expecting the (standard) output to be:
First call: A, B Secnd call: X, Y
But instead I get:
Outer $v1: SCALAR(0xa0078a8) = A Inner $v1: SCALAR(0xa0078a8) = A First call: A, B Outer $v1: SCALAR(0x9fee760) = X Inner $v1: SCALAR(0xa0078a8) = A Secnd call: A, Y
Why does inner_function() keep seeing the old version of $v1 instead of the new one that outer_function() sees?
Is there any way to prevent this, and have inner_function() see the same address as outer_function() every time they are called?
Thanks in advance!
N
PS: Perl version is "v5.10.0 built for i486-linux-gnu-thread-multi" (Debian 5.0.6, perl 5.10.0-19lenny2)
In reply to Nested sub's beget undead variables? by Nivlem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |