use strict; use warnings; package MyClass; { use Tie::Array; our @ISA = ('Tie::Array'); our @data; #mandatory methods sub TIEARRAY { my $class = shift; bless \@data, $class; @data = @_; return \@data } sub FETCH { my ($self, $index ) = @_; print "FETCH($index)\n"; return $data[$index] } sub STORE { my ($self, $index, $value) = @_; print "STORE($index)\n"; $data[$index] = $value } sub FETCHSIZE { print " "; return scalar @data } }; package main; $|++; local $" = ", "; my @x; tie @x, "MyClass", 0, 0, 0; my $x = \$x[2]; $$x++; print "x = (@x)\n"; =begin comment When I first saw the above, it looked like no matter how many items were tied, they would all refer to the same @MyClass::data internal array. The next few lines showed that's true: when I tied @y to the same class, @x lost its data; and when an element of @y was changed, the same happened to @x. =cut my @y; tie @y, "MyClass", 0, 0, 0; print "y = (@y)\n"; print "x = (@x)\n"; $y[1] = 3.14; print "y = (@y)\n"; print "x = (@x)\n"; untie @y; # gives a warning: untie attempted while 2 inner references still exist undef $x; # uncomment to reduce next warning to 1 instead of 2; comment to keep next warning at 2 untie @x; # warning, but with only 1 warning if previous line uncommented print "y = (@y)\n"; print "x = (@x)\n"; # so what's the second inner reference? #### =begin comment When I first saw the above, it looked like no matter how many items were tied, they would all refer to the same @MyClass::data internal array. The next few lines showed that's true: when I tied @y to the same class, @x lost its data; and when an element of @y was changed, the same happened to @x. =cut my @y; tie @y, "MyClass", 0, 0, 0; print "y = (@y)\n"; print "x = (@x)\n"; $y[1] = 3.14; print "y = (@y)\n"; print "x = (@x)\n"; untie @y; # gives a warning: untie attempted while 2 inner references still exist undef $x; # uncomment to reduce next warning to 1 instead of 2 untie @x; # warning, but with only 1 warning if previous line uncommented print "y = (@y)\n"; print "x = (@x)\n"; # so what's the second inner reference? =begin comment I wanted to see if I understood enough: make a replica, but use a new anonymous array, rather than having an @data; MySecond properly keeps @x and @y from interacting. =cut package MySecond; { use Tie::Array; our @ISA = ('Tie::Array'); #mandatory methods sub TIEARRAY { my $class = shift; my $self = bless [], $class; @$self = @_; return $self } sub FETCH { my ($self, $index ) = @_; print "FETCH($index)\n"; return $self->[$index] } sub STORE { my ($self, $index, $value) = @_; print "STORE($index)\n"; $self->[$index] = $value } sub FETCHSIZE { my ($self) = @_; print " "; return scalar @$self } }; tie @x, "MySecond", 0, 0, 0; tie @y, "MySecond", 0, 0, 0; $x[1] = 2.718; $y[0] = 0.000281828; print "|| x = (@x)\n"; print "|| y = (@y)\n";