Assignment to a glob
*glob = VALUE
contains some magic that depends on the type of VALUE
(i.e., return value of, say, Scalar::Util::reftype(VALUE)
). If VALUE
is a reference to a scalar, array, hash, or subroutine, then only that entry in the symbol table will be overwritten.
This idiom
local *array = shift();
#use @array here
works as documented when the first argument to the subroutine is an array reference. If the first argument was instead, say, a scalar reference, then only $array
and not @array
would be affected by the assignment.
A little demo script to see what is going on:
no strict;
sub F {
local *array = shift;
print "\@array = @array\n";
print "\$array = $array\n";
print "\%array = ",%array,"\n";
print "------------------\n";
}
$array = "original scalar";
%array = ("original" => "hash");
@array = ("orignal","array");
$foo = "foo";
@foo = ("foo","bar");
%foo = ("FOO" => "foo");
F ["new","array"]; # array reference
F \"new scalar"; # scalar reference
F {"new" => "hash"}; # hash reference
F *foo; # typeglob
F 'foo'; # not a reference, but name of assigned variable
F 'something else'; # not a reference
F (); # undef
Output:
@array = new array $array = original scalar %array = originalhash ------------------ @array = orignal array $array = new scalar %array = originalhash ------------------ @array = orignal array $array = original scalar %array = newhash ------------------ @array = foo bar $array = foo %array = FOOfoo ------------------ @array = foo bar $array = foo %array = FOOfoo ------------------ @array = $array = %array = ------------------ @array = orignal array $array = original scalar %array = originalhash ------------------
Additional doc at perlmod
and perldata
. Back in the days before references were a part of Perl, this idiom was helpful for passing arrays and hashes into subroutines.