in reply to Re^3: Traversing a hash of hashes of hashes
in thread Traversing a hash of hashes of hashes
Generally braces in %{$ref} are not needed because perl knows that there is only one possible way to dereference it.
Generally true, but ... IMO it's better to use the curly braces for clarity, and also because then you can dereference the result of any expression inside the block enclosed by them. This helps to avoid errors caused by attempting to dereference undefined structures, for example. Here's a simplified example (most often, I find, this happens when trying to dereference the value of a hash key that may or may not be defined):
$ perl -Mwarnings -Mstrict -MData::Dumper -E' my @foo = ( [qw/ a b c /], undef ); say "@$_" for @foo; ' a b c Can't use an undefined value as an ARRAY reference at -e line 3.
It's not necessary all the time, but like most things, if you are going to need it sometimes, you might as well develop the habit through consistency.$ perl -Mwarnings -Mstrict -MData::Dumper -E' my @foo = ( [qw/ a b c /], undef ); say "@{ $_ // [] }" for @foo; ' a b c
IIRC the Perl docs even recommend to always use the braces, but I can't find the "reference" now.
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Traversing a hash of hashes of hashes
by BillKSmith (Monsignor) on Mar 29, 2017 at 14:19 UTC |