好的,所以很容易创建对数组的引用......
my @a;
my $b=\@a;
#can now reference the same list of scalars from either @$b or @a
但是我怎么能反过来呢?例如:
my $a=[1..4];
my @b;
#some magic happens here and now @b is an alias for @$a
@b=(6..10);
print "@$a\n"; #should print "6 7 8 9 10"
我认为这会通过 typeglobs 发生,但那些只是让我望而却步。想法?
对哈希和数组做同样的事情也会很好。
编辑:这似乎可行,但它有点笨拙,因为它只是将匿名数组元素复制到“别名”,然后将自身重新指向数组:
my @b=@$a;
$a=\@b;
有更好的想法吗?