@foo = ("initial");
*bar = \@foo;
$bar[0] = "final";
print @foo;
####
my @foo = ("initial");
use strict;
my @bar;
*bar = \@foo;
$bar[0] = "final";
print @foo;
####
package wombat;
our @bar = ("haha"); # this is going to get
# accidentally clobbered
use strict;
sub A1 {
my @foo = (1,2,3,4);
return B2(3,\@foo); # pass in reference to @foo
}
sub B2 {
my ($n,$foo) = @_;
# now instead of always indexing $foo as a reference,
# I will use a type glob
# to allow me to access it as though it were an array.
# @bar here is intended to be just a temporary convenience
# variable name. But it accidentally collides with
# a package global used elsewhere.
# my @bar; # cannot do this, which would logically
# be locally scoped as I want it to be
# and avoid stepping on package globals.
our @bar; # must do this instead, which because of
#an accidental name collision with the
# package global that is also called
# "@bar", is going to have a sideeffect.
*bar = $foo;
# now do something with @bar
# --this is just a trivial placeholder process
@bar = map { sin($n+$_) } @bar;
return $bar[0]+$bar[4]; # just a trivial placholder
}
print A1(); # prints the expected result
print @bar; # but @bar is no longer "haha".
# because it gets clobbered because
# I could not control the scope of the typeglob