Re: Interesting behaviour with lexical variable
- From: "jl_post@xxxxxxxxxxx" <jl_post@xxxxxxxxxxx>
- Date: 30 Oct 2006 09:47:41 -0800
Ferry Bolhar wrote:
Hi to all,
while playing with code like this:
use warnings;
while (my $input = <STDIN>){
my $num = abs $input;
print add();
sub add {
$num + $num;
}
}
Unless you really know what you're doing, you should never define a
function inside a loop or another function. In fact, many Perl
programmers (me included) recommend that all your functions be defined
at the top of your script, before you start your main code. Do that,
and your problem will go away.
I found out that once the loop get executed for the second
time, the variable $sum gets "splitted", taking the new value
from $input in the loop, but leaving the old, previous value
in add().
I believe that's because the add() function is never defined until
the first time through the loop. Once it gets defined, it uses the
current value of $num in its return value, which appears to go out of
scope at the end of the loop. However, a reference is still retained
(by the add() function), so $num (declared the first time time through
the loop) never completely goes out of scope, as it is retained by the
add() function.
So the second (and subsequent) times through the loop, a new $num is
created, which is a DIFFERENT instance of the one that the add()
function uses (remember: the add() function uses the very first
instance of $num). It never stops using that first instance of $num,
which is why the add() function will always return the same thing.
You may mistakenly think that the add() function will always use the
latest value of $num, but it won't: the add() function only needs to
be defined once, and after that it never gets defined again. So it
never stops using the first instance of $num.
If this sounds confusing, it's because it kind of is. But change
your coding style so that all your functions are defined before your
main code (and not in any loops or functions either), and you should
never encounter this problem again.
I hope this helps, Ferry.
-- Jean-Luc
.
- Follow-Ups:
- Re: Interesting behaviour with lexical variable
- From: anno4000
- Re: Interesting behaviour with lexical variable
- References:
- Interesting behaviour with lexical variable
- From: Ferry Bolhar
- Interesting behaviour with lexical variable
- Prev by Date: Re: Naive threading performance questions
- Next by Date: Re: Printing and Formatting data from hash (or array)
- Previous by thread: Interesting behaviour with lexical variable
- Next by thread: Re: Interesting behaviour with lexical variable
- Index(es):
Relevant Pages
|