Nivlem has asked for the wisdom of the Perl Monks concerning the following question:
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nested sub's beget undead variables?
by chromatic (Archbishop) on Nov 10, 2010 at 19:39 UTC | |
by Nivlem (Initiate) on Nov 10, 2010 at 20:03 UTC | |
|
Re: Nested sub's beget undead variables?
by ikegami (Patriarch) on Nov 10, 2010 at 20:20 UTC |