in reply to (Ovid) Re: Warnings, and declaring Hashes of Hashes.
in thread Warnings, and declaring Hashes of Hashes.

For reasons that I've never been able to get an answer to, the following syntax is valid:
@array->[$idx]; %hash->{$key};


japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: (Ovid) Re: Warnings, and declaring Hashes of Hashes.
by snax (Hermit) on Nov 30, 2000 at 15:09 UTC
    Here's a guess :)

    The -> notation implies a context -- in fact, it implies that whatever to the left is a reference, and whatever is to its right is "owned" by the referred-to object.

    Perl seems pretty smart about context.

    So, in these cases, it interprets the thing to the left as a reference to the object named, and voila, does the right thing, since the "members" are being referred to properly.

    If you try to use ref() on the whole "thing" (hash or array) you won't see it as a reference because it is simply interpreting the "thing" in a scalar context, though.

    Update:
    Looking in perlref there is the cryptic comment:

    . References of the appropriate type can spring into existence if you dereference them in a context that assumes they exist. Because we haven't talked about dereferencing yet, we can't show you any examples yet.

    (See number 6).

    I'm guessing that's the case here.

      The cryptic comment in perlref that you refer to is this behaviour:
      $foo{bar}{baz}; # Assumes $foo{bar} exists and is a hash print ref($foo{bar}); # And lo and behold, it is!
      And I am with japhy on not knowing why this works. Certainly there is nothing I know of about context that would explain it...
        Yeah -- I just saw this in "autovivification" in the Cookbook.

        I still think it's a context example that simply isn't documented. I'm not so curious to go dig in the code, though. Or perhaps I should say I am that curious, but I don't have the time...