Can I name an anonymous array in Perl?

#!/usr/bin/perl -w use strict; my $aref = [1, 2, 3]; my @a = @$aref; # this line $a[1] = 99; print "aref = @$aref\n"; print "a = @a\n"; 

outputs the result:

 aref = 1 2 3 a = 1 99 3 

The output shows that @a and @$aref do not belong to the same array.

The marked line is where my problem is. The value of scalar $aref is a reference to an anonymous array. In the marked line, I was hoping to make the array of the @a variable a reference to this array, but what happens is that the anonymous array is copied and @a refers to a copy of the anonymous array. Assignment and printing operations show this.

I understand that when assigning to an array, the right side of the assignment represents the context of the list, so @$aref forced to list its elements. Is there a way to give @a name of the array referenced by $aref ?

+5
source share
4 answers

I wonder why you do this? I assume this is a performance issue, but the usual solution is to pass your data by reference. It is just as easy to write $aref->[1] as $a[1]

You could alias your link in the package symbol table by assigning typeglob, but the alias should be a package variable

 use strict; use warnings; my $aref = [1, 2, 3]; our @a; *a = $aref; $a[1] = 99; print "aref = @$aref\n"; print "a = @a\n"; 

Output

 aref = 1 99 3 a = 1 99 3 

There are several modules that offer good syntax and allow you the aliases of lexical variables

Here's the version that uses Lexical::Alias , which has the advantage of lexical anti-aliasing variables and can be more reliable than the typeglobs assignment. Data::Alias works very similarly. The output is identical to the above.

 use strict; use warnings; use Lexical::Alias qw/ alias_r /; my $aref = [1, 2, 3]; alias_r $aref, \my @a; $a[1] = 99; print "aref = @$aref\n"; print "a = @a\n"; 

an alternative way is to use alias instead of alias_r with

 alias @$aref, my @a; 
+7
source
  •  our @array; local *array = $aref; 

    Pros: built-in function with 5.6.
    Cons: Ugly. Uses a global variable, so the variable is considered under the name subs.

  •  use Data::Alias qw( alias ); alias my @array = @$aref; 

    Pros: Cleanliness.
    Cons: This module breaks down into almost every Perl release (although it fixes quickly, if not before the actual release).

  •  use feature qw( refaliasing ); no warnings qw( experimental::refaliasing ); \my @array = $aref; 

    Pros: built-in function.
    Cons: Perl 5.22+ is required, and even then this feature is experimental.

+6
source

To expand on the answer to Borodin, I tested this with Lexical :: Alias:

 #!/usr/bin/perl -w use strict; use Lexical::Alias 'alias_a'; my $aref = [1, 2, 3]; my @a; alias_a(@$aref, @a); $a[1] = 99; print "aref = @$aref\n"; print "a = @a\n"; 
+1
source

One option is to use the Data::Alias package from CPAN.

So you can write:

 #!/usr/bin/perl use Data::Alias qw( alias ); my $aref = [1, 2, 3]; alias my @a = @$aref; $a[1] = 99; print "aref = @$aref\n"; print "a = @a\n"; 

A related question about SO can be found here: Is it possible to assign the address of one array to another in Perl?

+1
source

All Articles