use strict; use warnings; use Data::Dumper; my $a; %$a=(a=>1,b=>2); print Dumper($a);
And it prints
$VAR1 = { 'a' => 1, 'b' => 2 };
UPDATE : I see now that the illegal part was the "my %$h" part rather than the assigment part. So that could have been changed simply by splitting the declaration to a separate line, or by changing the list to a {}.
However it seems an odd way to do it when you could just say :
$a={a=>1,b=>2};
I benchmarked the two versions, and the first %$a=() is 50% slower than the second ($a={}).
Regarding the difference between $h{name} versus $h->{name}, there is a slight speed difference (10%), because the second version first needs to dereference $h before doing the lookup, but it is so blazingly fast that I wouldn't worry about it. (Tested on a small hash - not sure for bigger hashes)
Rather just use whatever is easier to read, as there are bound to be other bottlenecks that make much more difference to your code's performance.
In reply to Re: Relative Merits of References
by clinton
in thread Relative Merits of References
by pbeckingham
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |