プラス中括弧。
名前の無いハッシュへのリファレンスは、中括弧を使って作ることができます:
...
開き中括弧が BLOCK の開始でないことを Perl に教えるために、文の最初の中括弧の前に + や return をつけて、曖昧さをなくすようにする必要がある場合があります。
...
sub hashem { { @_ } } # ちょっと間違い
sub hashem { +{ @_ } } # ok
sub hashem { return { @_ } } # ok
「ちょっと間違い」な { } と +{} と、処理速度ってどうなんだろ?
...と、役には立たないだろうけど
気になったので、いつも通り試してみる。
#!/usr/bin/perl -w
use strict;
use Benchmark qw(:all);
my $result = timethese(10000, {
'normal' => sub {
my @test = ();
push @test, &normal('test') for (1..1000);
},
'plus' => sub {
my @test = ();
push @test, &plus('test') for (1..1000);
},
});
cmpthese($result);
exit;
sub normal { { param => $_[0] } }
sub plus { +{ param => $_[0] } }
normal と命名するのは、どうかと自分でも思ったけど
そこはその場限りの超適当スクリプトなので、勘弁していただくとして。
結果。
Benchmark: timing 10000 iterations of normal, plus...normal: 30 wallclock secs (29.84 usr + 0.14 sys = 29.98 CPU) @ 333.56/s (n=10000)
plus: 34 wallclock secs (33.28 usr + 0.15 sys = 33.43 CPU) @ 299.13/s (n=10000)
Rate plus normal
plus 299/s -- -10%
normal 334/s 12% --
Benchmark: timing 10000 iterations of normal, plus...
normal: 32 wallclock secs (32.15 usr + 0.15 sys = 32.30 CPU) @ 309.60/s (n=10000)
plus: 34 wallclock secs (33.27 usr + 0.14 sys = 33.41 CPU) @ 299.31/s (n=10000)
Rate plus normal
plus 299/s -- -3%
normal 310/s 3% --
...そうですか。 +{} のが遅い、と。ふむ。
ちなみに return {} のバージョンも含めて、計測してみたけど
速い順で言うと {}、return {}、+{} でした。