in reply to What's with the tt-/tt operator?
> 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.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: What's with the - operator?
by snax (Hermit) on Nov 30, 2000 at 20:27 UTC | |
by Dominus (Parson) on Nov 30, 2000 at 20:48 UTC |