in reply to Re: Is this a hash slice?
in thread Is this a hash slice?

You never need to put braces around the hash name unless its name is wacky, and I can't figure out how to legally make one so wacky, so let's just ignore that possibility.

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:

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
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.

See perldoc perlreftut for a nice statement of the (only 2!) rules for using references.


Caution: Contents may have been coded under pressure.