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.
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
Back to
Seekers of Perl Wisdom
|
|