At 11:12 AM -0400 10/18/2001, Greg London wrote:
>for (my $cnt=0; $cnt<5; $cnt++)
> {
> my $var = 'hello';
> my $ref = \$var;
> print "ref is $ref \n";
> }
>
>it turns out that ref is pointing to
>the exact same address every time
>through the loop.
This isn't surprising. Each time you finish
an iteration of the loop, $var and $ref go out of
scope and are destroyed, since no external references
to them remain.
>
>when I modify the script like this:
>
>my @arr;
>for (my $cnt=0; $cnt<5; $cnt++)
> {
> my $var = 'hello';
> my $ref = \$var;
> push(@arr, $ref);
> print "ref is $ref \n";
> }
>
>I get what I originally expected,
>namely, each "my"ed variable is at a different
>address.
Sure, because in this case, you've added $ref to an
array which continues to exist after the loop iteration
finishes. The array has a reference to $ref, which in
turn refers to $var, so neither one is destroyed when
they go out of scope.
>in the first script, was perl "my"ing a
>variable everytime through the loop,
>but it just happened to be at the same address?
Probably.
>i.e. "my" a var, then garbage collect,
I thought Perl didn't do garbage collection -- just
reference counting, which happens whenever a variable
goes into or out of scope, or a reference is created
or destroyed.
--
Ron Newman [EMAIL PROTECTED]
http://www2.thecia.net/users/rnewman/