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;
source share