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

Hello fellow inmates, er.. monks. Today, I am intrigued by the following seemingly innocuous bit of code:
no strict 'refs'; $y = 'bob'; $x = '$y'; $$x = 'wierd'; print $$x; # prints 'wierd' ($$x has a value) print ' - '; # print a little seperator print $bob; # prints nothing ($bob is still undefined)
I set $x to '$y' instead of 'y' (which is how one would normally reference $y through $$x). $bob is left undefined, so we can deduce that the $$x isn't dereferenced twice. I've traversed the %:: main package symbol table, and it's not in there, either. Where, then, is $$x stored, and how is it dereferenced?

Very mysterious...

Replies are listed 'Best First'.
Re: Enigmatic Symbolic References
by chipmunk (Parson) on Dec 07, 2000 at 07:17 UTC
    $$x is ${'$y'}, and '$y' is in the main symbol table:
    DB<1> l 2==> $y = 'bob'; 3: $x = '$y'; 4: $$x = 'wierd'; 5 6: print $$x; # prints 'wierd' ($$x has a value) 7: print ' - '; # print a little seperator 8: print $bob; # prints nothing ($bob is still undefined) DB<1> n main::(-:3): $x = '$y'; DB<1> n main::(-:4): $$x = 'wierd'; DB<1> n main::(-:6): print $$x; # prints 'wierd' ($$x has a value) DB<1> X ~y $$y = 'wierd' $y = 'bob' DB<2> x ${$::{'$y'}} 0 'wierd' DB<3>
    (Remember that $x = '$y' is very different from $x = "$y".)

    Update: Double quotes in the first paragraph changed to single quotes.

Re: Enigmatic Symbolic References
by Adam (Vicar) on Dec 07, 2000 at 07:20 UTC
    Its hidden in $y
    If you take out the lines with the word "bob" in them the bob symbol dissapears from %::, but $y does not. very revealing.