in reply to HoH Weirdness

The answer to the first section of code is rather simple. The string 'Apple' is being used as a reference. If you compile it under "use strict 'refs'" (or just "use strict"), you'll see what I'm talking about.

Now, as to how using a string as a reference works ... I'm a little fuzzier on that. But, I do like using it, in very controlled doses. :)

Now, as to the second question ... I did a little further playing around and found that so long as the assignment of Apple never happened before the assignment of Red, for that given hash, neither hash correctly printed "Red Apple".

<speculation>
I'm going to guess that the moment the interpreter sees that the string 'Apple' can be made into a reference, it marks 'Apple' as a valid reference for everyone. Why it affects the hash seemingly defined before it? Because, the interpreter, in its first pass, will find all the possible references and remember those. Then, in the second pass, it will use those references it knows about.
</speculation>

Nifty theoretical problem!

Replies are listed 'Best First'.
Re: Re: HoH Weirdness
by merlyn (Sage) on Jun 08, 2001 at 22:48 UTC
    I'm going to guess that the moment the interpreter sees that the string 'Apple' can be made into a reference, it marks 'Apple' as a valid reference for everyone. Why it affects the hash seemingly defined before it? Because, the interpreter, in its first pass, will find all the possible references and remember those. Then, in the second pass, it will use those references it knows about.
    No, that's too much voodoo. It's simpler than that. The first hash assignment puts values in %main::Apple, using a symref (use strict would have caught that.) And the second set of assignments causes it to reference %main::Apple as well, the very same hash.

    No magic needed to explain that.

    -- Randal L. Schwartz, Perl hacker