J
Jerry Krinock
After several hours of head-scratching, I finally found out why I
changes I made to an array passed by reference to a subroutine are not
seen outside the subroutine, thanks to Steve Litt's Perls of Wisdom
[1]:
"Any change the subroutine performs to @_ or any of its members like
$_[0], $_[1], etc, are changes to the original argument. HOWEVER,
assigning @_ or its elements to other variables makes a separate copy.
Changes to the separate copy are unknown outside of the subroutine."
Maybe if someone could explain the reason why Perl would do this, make
a copy upon *assignment*, it would be easier for me to remember. Is
this a "feature" of the language, or some unfortunate implementation
reality? The reason why I discovered this is that since I'm not a
full-time Perl guy, I try and write readable code, defining lots of
variables. However, as you can see from my code below, my nice and
pedantic subroutine pushBad() doesn't work because it makes a local
assignment, but the hard-to-read pushGood() works.
Thanks for any insights!
Jerry Krinock
1. http://www.troubleshooters.com/codecorn/littperl/perlsub.htm.
Steve apparently knows it well; he repeated that text in six places on
this page )
#!/usr/bin/perl
use strict ;
my @array = ("start") ;
pushBad(\@array) ;
print("Passed back from pushBad: @array\n") ;
pushGood(\@array) ;
print("Passed back from pushGood: @array\n") ;
sub pushBad {
my $arrayRef = shift ;
my @array = @$arrayRef ;
push (@array, "bad") ;
print("After pushing, in pushBad: @array\n") ;
}
sub pushGood {
push(@{$_[0]}, "good") ;
print("After pushing, in pushGood: @{$_[0]}\n") ;
}
CONSOLE OUTPUT:
After pushing, in pushBad: start bad
Passed back from pushBad: start
After pushing, in pushGood: start good
Passed back from pushGood: start good
changes I made to an array passed by reference to a subroutine are not
seen outside the subroutine, thanks to Steve Litt's Perls of Wisdom
[1]:
"Any change the subroutine performs to @_ or any of its members like
$_[0], $_[1], etc, are changes to the original argument. HOWEVER,
assigning @_ or its elements to other variables makes a separate copy.
Changes to the separate copy are unknown outside of the subroutine."
Maybe if someone could explain the reason why Perl would do this, make
a copy upon *assignment*, it would be easier for me to remember. Is
this a "feature" of the language, or some unfortunate implementation
reality? The reason why I discovered this is that since I'm not a
full-time Perl guy, I try and write readable code, defining lots of
variables. However, as you can see from my code below, my nice and
pedantic subroutine pushBad() doesn't work because it makes a local
assignment, but the hard-to-read pushGood() works.
Thanks for any insights!
Jerry Krinock
1. http://www.troubleshooters.com/codecorn/littperl/perlsub.htm.
Steve apparently knows it well; he repeated that text in six places on
this page )
#!/usr/bin/perl
use strict ;
my @array = ("start") ;
pushBad(\@array) ;
print("Passed back from pushBad: @array\n") ;
pushGood(\@array) ;
print("Passed back from pushGood: @array\n") ;
sub pushBad {
my $arrayRef = shift ;
my @array = @$arrayRef ;
push (@array, "bad") ;
print("After pushing, in pushBad: @array\n") ;
}
sub pushGood {
push(@{$_[0]}, "good") ;
print("After pushing, in pushGood: @{$_[0]}\n") ;
}
CONSOLE OUTPUT:
After pushing, in pushBad: start bad
Passed back from pushBad: start
After pushing, in pushGood: start good
Passed back from pushGood: start good