Hi Tim,
So, all this stuff just has to do with how Perl resolves what a name refers
to. If you have script that looks like...
$var1 = 1;
my $var1 = 3;
print $var1;
you, of course get 3. But probably not for the reason you think. The first
declaration and initialization of $var1 creates a symbol table entry. A
symbol table entry is the alternative of a lexical scope. That lexical
scope is what you get with 'my $var1 = 3;'. It turns out that Perl prefers
(that is finds first) the lexical kind, so the print $var1 gets you that.
You can still get the original $var1 though. $main::var1 refers to the
symbol table entry of main, that points to the table entry for $var1. So if
you run the following code...
$var1 = 1;
my $var1 = 3;
print $var1;
print "\n";
print $main::var1;
You get
3
1
Creepy huh.
The lexical scope of a variable cascades out from the current block that the
variable is being evaluated in. So
my $var1 = 3;
{
my $var1 = 4;
print $var1; #prints '4'
}
print $var1; # prints '3';
This might explain why Paul had all those curly braces. If the $var1 was
not declared inside those braces it would have used the one declared outside
the braces.
It's also worth mentioning that if you 'use strict;' you can't declare
you're own symbol table variables so all of your user defined variables will
be lexically scoped.
I just thought of something though, I'm not sure if 'use vars...' declares
lexical or symbol table variables.
I hope all this helps.
Peter C.