Arunkumar_141 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

when a hash is defined with a reference , it's value is obtained by dereferencing using ->. Here keys of hash is referencing to a hash. so while dereferencing, why is it allowing $hash->{a}{1} as it should allow only $hash->{a}->{1} $hash={a=>{1=>2}};

print "ref($hash->{a})"; #says its an hash reference print "$hash->{a}->{1}" ;#returns 2 as expected print "$hash->{a}{1}"; #also returns 2 ,my question is how without der +eferencing the key of $hash->{a} its value is getting displayed?

2019-03-07 Athanasius added paragraph and code tags

Replies are listed 'Best First'.
Re: Query on References
by holli (Abbot) on Mar 05, 2019 at 17:54 UTC
    Well, you've seen it. $hash->{a}->{1} returns the same as $hash->{a}{1} because it means the same. You can omit the deref arrow in nested structures for convenience. This also works for arrays.
    $ref = [ [2, 4], [8, 16] ]; say $ref->[1][1]; #16


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: Query on References
by tybalt89 (Monsignor) on Mar 05, 2019 at 18:31 UTC

    From perldoc perlref

    One more thing here. The arrow is optional between bracket +s subscripts, so you can shrink the above down to $array[$x]{"foo"}[0] = "January"; Which, in the degenerate case of using only ordinary arrays +, gives you multidimensional arrays just like C's: $score[$x][$y][$z] += 42;
Re: Query on References
by AnomalousMonk (Archbishop) on Mar 05, 2019 at 20:33 UTC

    Another way to look at it is to realize that in Perl, different basic types can have the same name. There has to be some way to distinguish between, e.g., a hash and a scalar containing a hash reference. The  -> (arrow) operator (see perlop) makes this syntactic distinction, as it were. Below the top level, nested hashes (and arrays) can only be references, so the compiler does not need an arrow operator for disambiguation (but an  -> can always be used at lower level(s) if you wish).

    c:\@Work\Perl\monks>perl -wMstrict -le "my %hash = ( 'structure' => { 'uses plain sigil' => 'dereference' } +); my $hash = { 'reference' => { 'needs -> operator' => 'at top' } +}; ;; print $hash {'structure'}{'uses plain sigil' }; print $hash -> {'reference'}{'needs -> operator'}; " dereference at top
    See also The Perl Data Structures Cookbook.


    Give a man a fish:  <%-{-{-{-<

Re: Query on References
by thanos1983 (Parson) on Mar 05, 2019 at 17:51 UTC

    Hello Arunkumar_141,

    Please read How do I post a question effectively? and change your question accordinly so you can help us help you. :)

    Looking forward to your updated version of your question :).

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Query on References
by BillKSmith (Monsignor) on Mar 05, 2019 at 22:29 UTC
    Perl provides several syntaxes for dereferencing (Using References). In general, you are free to use whichever one you feel is easiest to use. Method 2 has the advantage that it always works (no exceptions or special cases). Unfortunately, the resulting code can be nearly impossible for people to read. Your choice of the arrow operator is probably the most common. See link above for details.
    Bill