in reply to Re^2: Traversing a hash of hashes of hashes
in thread Traversing a hash of hashes of hashes

hello,

extra braces are genally not needed to do the dereference

# plain hash note parens() my %plain = (a=>1,b=>2); # an hash reference note braces {} my $ref = {c=>3,d=>4}; #dereferencing example foreach my $k ( keys %$ref ) # is the same of foreach my $k ( keys %{$ref} )

Generally braces in %{$ref} are not needed because perl knows that there is only one possible way to dereference it.

For more example see tye's tutorial References quick reference

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^4: Traversing a hash of hashes of hashes
by 1nickt (Canon) on Mar 29, 2017 at 12:32 UTC

    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.
    $ perl -Mwarnings -Mstrict -MData::Dumper -E' my @foo = ( [qw/ a b c /], undef ); say "@{ $_ // [] }" for @foo; ' a b c
    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.

    IIRC the Perl docs even recommend to always use the braces, but I can't find the "reference" now.

    Hope this helps!


    The way forward always starts with a minimal test.
      I also prefer the braces, but for a different reason. Dereferencing is documented in Using References. Method 2 always works. All of the others produce cleaner looking code, but require author (and often, the reader) to know and understand the related rules of applicability and precedence.
      Bill
Re^4: Traversing a hash of hashes of hashes
by kcorj2244 (Novice) on Mar 29, 2017 at 12:07 UTC
    Understood. However I face a strange issue with this: Here is an example. $bigHash is similar to my example. A hash full of other hashes full of other hashes and so forth. I don't have direct access to %bigHash only $bigHash. So, me wanting to dereference it would assume that my %newHash = %$bigHash would do the trick right? The issue is, is %newHash only contains the FIRST hash of $bigHash. I'm not entirely sure on what causes this, but I'd love to learn!
      Hello,

      %newHash = %$bigHash will suffice. I do not understand what you mean you do not have access to %bigHash in your example %bigHash simply does not exists: it exists an anonymous hash ie $bigHash

      For the copy what you say is impossible:

      perl -MData::Dumper -e '$href={a=>1,b=>{c=>1,d=>{e=>5}}}; %copy = %$hr +ef; print Dumper \%copy' $VAR1 = { 'a' => 1, 'b' => { 'c' => 1, 'd' => { 'e' => 5 } } };

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.