Re: Trouble with variable scoping
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 1 Sep 2006 05:26:10 -0700
Roman Daszczyszak wrote:
What is the scope of a variable that is declared at the top of a
file using 'my'?
I had thought that this would just make a variable that is scoped to
the entire file, yet 'local' does not localize the variable in a
subroutine, even though it (local) according to what I've read will
'save a global variable's value and reset it back upon the variable
going out of scope'.
You are confusing file scope with global. A lexical variable has the
scope of it's inner-most block, or until the end of the current file if
there is no block. A global (ie, package) variable has scope of the
entire program, spanning blocks, packages, and even files.
example.pl:
#!/usr/bin/perl
use strict;
use warnings;
use SomeMod;
my $lex = 'lexical';
our $glob = 'global';
SomeMod::myfunct();
__END__
SomeMod.pm:
package SomeMod;
sub myfunct {
print "Global: $main::glob\n"
print "Lexical: $lex\n";
}
1;
In the above example, SomeMod::myfunct will be able to print the value
of $main::glob just fine, because it's a global variable. But it does
*not* have access to the lexical variable $lex, because $lex's scope is
only the file example.pl. It cannot be seen anywhere else.
Does that make sense?
That sounded exactly like the behavior I was
after, yet it did not work. According to the error, 'local' does not
work on lexically scoped variables.. so what other kinds of scoping
are there? I only know of using 'my' to declare variables (which are
scoped to the current block (which I understand) and are lexically
scoped (which I don't)) because it's required by 'use strict'.
That is possibly the most common misperception about Perl that I've
ever encountered. 'use strict;' does *NOT* "force you to declare your
variables" as everyone likes to say. What it does do is force you to
fully-qualify any global variables you decide to use. That is, if you
want to use the global variable $glob, you have to refer to it as
$main::glob.
Ordinarily, if your program uses $foo, Perl will first check to see if
a lexical variable $foo exists in the current scope. If it does,
that's the variable you're referring to. If it does not, Perl then
assumes you meant the global variable $foo belonging to the current
package. use strict; overrides this behavior, forcing you to say
$main::foo if you mean $foo belonging to the main package. And then
the 'our' keyword overrides 'use strict;', allowing you to use the
short name of the variable, but only for the duration of the lexical
scope of the our:
#!/usr/bin/perl
$main::foo = 'Global Variable';
print $foo; #no lexical in scope - Perl assumes I meant $main::foo
{
my $foo = 'Lexical';
print $foo; #lexical $foo now in scope, so Perl prints the lexical
#if I want the existing global, I fully qualify it:
print $main::foo
}
use strict;
print $foo;
#ERROR! No lexical exists in this scope, and 'strict' prevent
#Perl from assuming I meant $main::foo
{
our $foo;
#I can now refer to $main::foo as $foo, for the duration of this
scope
print $foo;
}
#now I need to fully qualify $main::foo again
__END__
Hope this helps,
Paul Lalli
.
- References:
- RE: Trouble with variable scoping
- From: Roman Daszczyszak
- RE: Trouble with variable scoping
- Prev by Date: Re: Reading whole file with while(INPUT) but I need to access each line
- Next by Date: Re: Trouble with variable scoping
- Previous by thread: RE: Trouble with variable scoping
- Next by thread: Re: Trouble with variable scoping
- Index(es):
Relevant Pages
|