in reply to question about variabies/references (ignore my previous botched entry)
You're seeing this weird behavior because you have several
variables that point to the same
value; at the time of the first print, you haven't yet
set that value, so it's undefined. When you set the value
in the next line, it sets that value, so when you do the
second print, they have a value.
Let's step through it:
This is self-explanatory--you're just setting a scalar variable. Later you'll use that value as a symbolic reference.$test = "scott";
Here you're manipulating the symbol table; you're saying that $main::scott should point to the same thing that $main::skot2 points to. You can do this symbol table manipulation quite easily, and there's a definite potential for confusion--for example, there's a difference between $main::{"scott"} and $main::scott. The first is a symbol table entry and the second is a scalar variable.$main::{$test} = "skot2";
$scott and $main::scott are the same variable, because $scott is found in package main, and the second is just a fully-qualified version of the first. You haven't set a value for this variable yet. You've modified $main::{"scott"} but not $main::scott. Perhaps that's the real source of your confusion?print "As you see, ($scott) and ($main::scott) " . "aren't here\n";
Here's where you set the value. $test is equal to "scott", so here you're just using symbolic references to change the value of $scott (which is the same as $main::scott and points to $skot2). So, it makes sense that, in the next line...$$test = "surprise!";
you now have values for your variables.print "Now ($scott) and ($skot2) and ($main::scott) " . "have decided to show up\n";
Does this make sense?
|
---|
In Section
Seekers of Perl Wisdom