Shallow copy reference into variable in Perl
Asked Answered
E

2

7

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?

Exception answered 6/4, 2013 at 3:35 Comment(3)
Duplicate https://mcmap.net/q/1479779/-typeglob-aliasesRefined
Take note of Ikegami's note about the most sensible approach. If you work with nested Perl structures, enough, you'll get used to retrieving it as a ref, instead of copying it to an array variable. It only adds 1-character to type to $.Rainbow
@Axeman2: Yes, of course. This question was more on the educational spirit. :-)Exception
B
7

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] }
Bromine answered 6/4, 2013 at 12:9 Comment(0)
U
5

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.

Unintentional answered 6/4, 2013 at 5:27 Comment(1)
Data::Alias, not PadWalker.Bromine

© 2022 - 2024 — McMap. All rights reserved.