Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

$x{1}{2} versus $x{1}->{2}

by bcrowell2 (Friar)
on Jan 16, 2009 at 16:56 UTC ( [id://736875]=perlquestion: print w/replies, xml ) Need Help??

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

O Monks,

The following code shows three ways of indexing into a hash of hash references.

perl -e '%x = (1=>{3=>4}); print $x{1}->{3}' perl -e '%x = (1=>{3=>4}); print $x{1}{3}' perl -e '%x = (1=>{3=>4}); $r= $x{1}; print $r{3}'

The first one is the way I'm used to doing it, and it seems logical to me because $x{1} is a reference to a hash, not a hash, so you ought to use the -> to dereference it. However, I came across he second syntax in a cpan module, and when I looked in the camel book, this was actually the syntax presented there. Surprisingly (to me), it produced the same result. It seemed to me like it shouldn't work, because $x{1} is a reference, and you can't normally index into a hash via a reference without the ->. The third example doesn't work, which is what I expected.

Can anyone explain why syntax #2 works, while #3 doesn't? It seems like #3 is just doing the same thing as #2, but in two steps. Is this an exception that's built into the perl interpreter as a special case somewhere, or is there something I'm not understanding that makes this a regular usage?

TIA! -Ben

Replies are listed 'Best First'.
Re: $x{1}{2} versus $x{1}->{2}
by kennethk (Abbot) on Jan 16, 2009 at 17:04 UTC
    $x{1}{2} is syntactically equivalent to $x{1}->{2}, just like $y[1][2] is $y[1]->[2] (and all combinations thereof). When the interpreter encounters multiple keys or indices in a row, it automatically includes the dereference operation. Since multi-dimensionality is always handled in the same way (AoA), there is no possible ambiguity so there is no risk in supporting both. See perlref and perllol for more info.
Re: $x{1}{2} versus $x{1}->{2}
by jettero (Monsignor) on Jan 16, 2009 at 17:01 UTC
    This is covered fairly well in perlref. It's just a shortcut for convenience, nothing more (or less). It's clearer, in my opinion, to use the shortcuts.

    -Paul

Re: $x{1}{2} versus $x{1}->{2}
by jethro (Monsignor) on Jan 16, 2009 at 17:15 UTC
    To elaborate on kennethks explanation: If $r{3} wouldn't mean to the perl interpreter that it has to access %r instead of $r, then the third example could have easily been made to work by the designers of the perl language. It could have been another syntactic sugar aka shortform, but that syntax was already booked by direct hash access.
Re: $x{1}{2} versus $x{1}->{2}
by sgt (Deacon) on Jan 16, 2009 at 17:37 UTC

    Hi. Well #1 and #2 are completely equivalent. #1 is the long form. The second form is just syntactic sugar for #1.

    Perl does not do implicit dereferencing. $r being a reference you need the arrow or $$r{3} (which I like better written like ${ $r }{3} if not writing a one-liner). Maybe you can reread 'perldoc perlref' where you'll find quite a few examples. In particular look for "deref".

    I usually stick with the arrow everywhere, except where you can't (e.g slices don't play well the arrow as the context imposed on the RHS is scalar, but hey you can write your own slice function if you find it prettier).

    hth, cheers --stephan
Re: $x{1}{2} versus $x{1}->{2}
by mr_mischief (Monsignor) on Jan 16, 2009 at 17:36 UTC
    Consider this one:
    perl -e '%x = (1=>{3=>4}); %r = %{$x{1}}; print $r{3}'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://736875]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found