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

Buried in another thread the issue was raised about why
%h->{key} = 'value'; @a->[0] = 12;
constructs worked. I suggested it was a context issue, since the -> implies a reference. This is a little different than other "context" concepts I'm used to in Perl, so maybe I'm off base.

Further, in Programming Perl, 2nd Ed., it says (page 79):

Just as in C and C++, -> is an infix dereference operator. If the right side is either a [...] or {...} subscript, then the left side must be either a reference to an array or hash (or a location capable of holding a hard reference, if it's an lvalue (assignable)).

So this would suggest that in this context %h and @a above are references -- or treated as references.

Anyone know if there is a deep reason for this?

Replies are listed 'Best First'.
Re: What's with the -> operator?
by Dominus (Parson) on Nov 30, 2000 at 19:36 UTC
    Says snax:

    > So this would suggest that in this context %h and @a above are
    > references--or treated as references.
    >
    > Anyone know if there is a deep reason for this?

    It would suggest that, but it isn't really true. Since it isn't true, there isn't any reason for it at all, deep or otherwise.

    Here is what is really going on: When you write $h->{key}, Perl looks up the glob *h, and extracts the scalar part of the glob. Then it converts the scalar into a hash using a routine called rv2hv; if the scalar did not contain a hashref, rv2hv would raise a fatal error.

    When you write %h->{key} it is almost the same. Perl looks up the glob *h and extracts the hash part of the glob instead of the scalar part. Then it calls rv2hv. rv2hv is written so that if it has a hash already, it simply returns it, as if it had been passed a reference to that hash in the first place.

    rv2hv could have raised an error here, but it doesn't. As a result, there's an oddity in the language that you can write %h->{key} and Perl interprets it as if you had written {\%h}->{key} instead.

    (If $h or %h is a lexical variable, replace 'looks up the glob' with 'looks in the pad', as appropriate.)

      Ahh, thanks. That makes a lot of sense and I had actually pondered globbing a bit, but I've never found a reason to need to use globbing, so I haven't learned that much about it. Perhaps now is a good time...
        Says snax:

        > I had actually pondered globbing a bit...
        No, the globs are irrelevant here. The rv2hv thing is the essential point.

        If $h and %h are lexical variables, then there are no globs involved at all, but the behavior is the same.

Re: What's with the tt-/tt operator?
by Fastolfe (Vicar) on Nov 30, 2000 at 19:29 UTC
    I think because of the fact that @array->[$index] isn't really valid, and that Perl tries to be accomodating, it's turning it into $array[$index]. The only difference is that you can't use @array->[0, 1, 2] to get a slice (but then you can't use $array[0..2] either).

    Perhaps somebody else can better express what's going on internally that makes this happen...