in reply to Relative Merits of References
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Relative Merits of References
by ikegami (Patriarch) on May 11, 2006 at 14:25 UTC | |
by johngg (Canon) on May 11, 2006 at 18:47 UTC | |
Re^2: Relative Merits of References
by UnderMine (Friar) on May 12, 2006 at 13:19 UTC |