|
|
|
Clear questions and runnable code get the best and fastest answer |
|
| PerlMonks |
Re: question about variabies/references (ignore my previous botched entry)by chromatic (Archbishop) |
| on Apr 05, 2000 at 19:04 UTC ( #6941=note: print w/ replies, xml ) | Need Help?? |
|
Here's a funny thing. $scott and $main::scott refer to the same variable, assuming you haven't declared that you're in another package. Another piece of the puzzle is that putting curly braces around anything that looks like a variable causes Perl to interpolate the value of that variable. Doing: $main::{$test} = "skot2"; first causes interpolation -- so that the interpreter now has: $main::scott = "skot2"; From there, I assume plaid is correct in that the interpreter decides that you really meant to do a typeglob assignment, and takes "skot2" to be a symbolic reference (that is, the name of another variable). Since it hasn't been declared before, $scot2 (and $main::scot2) are autovivified. Next, $main::scott is aliased to $scot2. The $$test line is even more tricky. You might also write it ${$test} = "surprise!"; to be consistent with my explanation. If $test were a normal reference, this would dereference it. Since it contains the name of a variable ($skot2 has just been created automatically), Perl considers it a symbolic reference, looks up $main::skot2 in the symbol table, and assigns the value of "surprise!" to it. Because $main::scott is aliased to $skot2, accessing it gets you the same value ("surprise!"). Because you're in package main, you can leave off the $main:: portion, and accessing $scott also gets you your "surprise!". These are three or four things that will eventually surprise any new Perl programmer. Using strict and -w will warn you when these things happen (except for autovivification in hashes, which is worth another page of explanation). Use them liberally!
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||