Closed p5pRT closed 16 years ago
A "my" variable is declared in subroutine A. Subroutine B is declared within the scope of subroutine A so that it can access this variable. The first time subroutine A is called it all works correctly. The second time subroutine A is called it (A) gets a new\, clean copy of the variable\, but B still carries on using the copy that is got the first time.
Is this a bug or do I have a profound misunderstanding of "my"?
The program below demonstrates the behaviour ****************************************************************************** #!/usr/local/bin/perl
@l = qw(long green fish); print "in test_my: "\, \@l\, "\<"\, join (" "\, @l)\, ">\n"; &my_sub(@l);
@l = qw(mad cows); print "in test_my: "\, \@l\, "\<"\, join (" "\, @l)\, ">\n"; &my_sub(@l);
sub my_sub { my @l; print "top of my_sub "\, \@l\, "\<"\, join (" "\, @l)\, ">\n"; foreach $t (@_) { print "in my_sub loop: $t\n"; &my_sub_sub($t); } print "bottom of my_sub "\, \@l\, "\<"\, join (" "\, @l)\, ">\n";
sub my_sub_sub { print "in my_sub_sub: @_\n"; push @l\, @_; print "in my_sub_sub "\, \@l\, "\<"\, join (" "\, @l)\, ">\n"; }
}
******************************************************************************
running it produces:
******************************************************************************
ferlinghetti/perl: ./test_my_for_mailing
in test_my: ARRAY(0x140011310)\
l.moye@2020speech.com (via RT) wrote:
A "my" variable is declared in subroutine A. Subroutine B is declared within the scope of subroutine A so that it can access this variable. The first time subroutine A is called it all works correctly. The second time subroutine A is called it (A) gets a new\, clean copy of the variable\, but B still carries on using the copy that is got the first time.
Is this a bug or do I have a profound misunderstanding of "my"?
The program below demonstrates the behaviour
This is not a bug; in fact\, this is documented behaviour. Looks like you have rediscovered closures! (See the perlref manpage for info on closures\, this is likely to be a feature you want to use.)
If you run your example program with -w\, you'll see the warning "Variable "@l" will not stay shared at - line 22." This warning and its meaning are described in the perldiag manpage.
Thanks for your report anyway. Hope this helps.
Hi Rafael\,
Thanks for your advice\, and sorry for reporting a feature as a bug!
However\, I think there is still a problem for old C-hackers like myself in understanding the documentation.
Had I used the same constructiuon in 'C' it would have worked as expected. All the explanations about closure refer to "anonymous" subroutines: Perfaq7\, for example:
Closures are implemented in Perl as anonymous subroutines with lasting references to lexical variables outside their own scopes.
There is nothing to warn the user that closure will occur for a named subroutine (as in my example)\, and that one needs the anonymous subroutine mechanism to enable one subroutine within the scope of another to get useful values of the outer one's "my" variables.
Might I suggest that it would be possible\, perhaps under the explanation of "my"\, to point out that closure occurs in locally declared subroutines\, and to refer the reader to\, not just Perlref (which I found somewhat confusing)\, but also to Perldiag --- "Variable will not stay shared"\, which explains how to cope with it.
Regards\, Laurie
p5p@spam.wizbit.be - Status changed from 'open' to 'resolved'
Migrated from rt.perl.org#17196 (status was 'resolved')
Searchable as RT17196$