Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shallow copy reference into variable in Perl

In Perl, you can assign to a variable a reference to another variable, like this:

my @array = (1..10);
my $ref = \@array;

And, as it is a reference, you can do something like this and both variables will be affected:

push @array, 11;
push @$ref, 12;

and both variables will contain 1..12, because they both point to the same space.

Now, I'd like to know if there is any way you can do the same thing, but starting with a ref and later assigning that reference to a plain variable. For example:

my $ref = [1..12];
my @array = # something here that makes @array point to the same space $ref contains

I know I can just assign it like this:

my @array = @$ref;

but, that's a copy. If I alter $ref or @array, those will be independent changes.

Is there some way to make @array point to the same variable as $ref?

like image 343
Francisco Zarabozo Avatar asked Apr 06 '13 03:04

Francisco Zarabozo


2 Answers

Four ways:

  • our @array; local *array = $ref;
  • \my @array = $ref; (Experimental feature added to 5.22)
  • use Data::Alias; alias my @array = @$ref;
  • Using magic (e.g. tie my @array, 'Tie::StdArrayX', $ref;)

But of course, the sensible approach is to do

my $array = $ref;

and use @$array instead of @array.



Aforementioned Tie::StdArrayX:

package Tie::StdArrayX;
use Tie::Array qw( );
our @ISA = 'Tie::StdArray';
sub TIEARRAY { bless $_[1] || [], $_[0] }
like image 192
ikegami Avatar answered Dec 25 '22 04:12

ikegami


This can be done when the array is a package variable, using typeglobs.

my $foo = [7,8,9];
our @bar;
*bar = $foo;
$foo->[1] = 3;
print @bar;     # "739"

Lexical variables (i.e. my @bar) can't be assigned to with typeglobs, though. Maybe there is a lexical solution or workaround built around PadWalker.

like image 38
mob Avatar answered Dec 25 '22 06:12

mob