You need braces around a hash reference any time it is not a simple scalar variable and you are not fetching a single element, because the precedence of the dereferencing operators will not DWYM otherwise. You may put braces around a hashref any time you feel like it. And those braces around the hashref are a true BLOCK: you can put any code you like in there, as long as the final result is a hash ref. It's just like a do-block, but the do is implied by the dereferencing.
To illustrate the precedence considerations:
The syntax error line thinks $bar must be a reference, like $foo was earlier. But it is $bar{a} that is the reference we want to dereference.my $foo = {a => 1}; # $foo is a hashref print $$foo{a}; # Ok print @$foo{a}; # one-element slice, but ok print $foo->{a}; # Preferred single-element fetch my %bar = (a => {b => 1}); # %bar is a HoH print ${$bar{a}}{b}; # Ok print @{$bar{a}}{b}; # one-element slice, but ok print @$bar{a}{b}; ### syntax error! print $bar{a}{b}; # Preferred single-element fetch
See perldoc perlreftut for a nice statement of the (only 2!) rules for using references.
In reply to Re^2: Is this a hash slice?
by Roy Johnson
in thread Is this a hash slice?
by tphyahoo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |