2011-01-12 12 views

答えて

12
sub f { 
    return [@_]; 
} 

$ref = f(2, 3, 5); 
print "@$ref\n"; 

[@foo]構築物は、新たな匿名アレイ(@fooのコピー)への参照を作成します。

2

配列としてその引数を返すサブルーチンを作成するには、いくつかの方法があります。これらの例が示すように

my ($x, $y) = (3, 4); 

my $array = array $x, $y; 
my $capture = capture $x, $y; 

say "@$array, @$capture"; # prints '3 4, 3 4' 

$x++; 

say "@$array, @$capture"; # prints '3 4, 4 4' 

$$capture[1] *= 2; 

say "@$array, @$capture"; # prints '3 4, 4 8' 

say "$x $y"; # prints '4 8' 

、:

sub array {[@_]} # returns an array reference that is a copy of its argument 

sub array_verbose { # the same as array(), but spelled out 
    my @copy = @_; 
    return \@copy; 
} 

sub capture {\@_} # returns a reference to the actual argument array 

arraycaptureの間にいくつかの重要な違いがあります。 array()によって生成された配列は値によってコピーされ、それらの値は元の引数から独立しています。 capture()によって生成された配列は、その引数リストへの双方向エイリアスを保持します。

速度にも違いがあります。 capture()は、array()よりも約40%高速です。なぜなら、配列の要素をコピーする必要がないからです(あるいは、それについても見てください)。もちろん、この速度の差は引数リストの長さによって変わります。

も、その要素に触れないcapture()の追加の効果は、通常のメモリを割り当てることになる引数が使用されている場合は、引数をタッチするまで、その割り当てが起こらないということです。私自身のコードで

my %hash; 
my $hashcap = capture $hash{a}, $hash{b}, $hash{c}; 

say join ', ' => keys %hash; # prints nothing 

$_++ for @$hashcap; 

say join ', ' => keys %hash; # prints 'c, a, b' 

、私は通常capture()cap()と綴るか、インラインで書く:

my $y = sub{\@_}->(map $_**2, 1..10); 
関連する問題