Re: Trouble with variable scoping



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

.



Relevant Pages

  • Re: Setting the values of Perl variables from a C program embedding Perl
    ... Does this only work with global variables? ... get_svon a variable that hasn't been created yet in Perl and ... Well, this is needed usually in perl extension, not for embedding perl ... They visible only in the scope of the ...
    (comp.lang.perl.moderated)
  • Re: Syntax simplification
    ... >>IMO, the type/behaviour polymorphism of has always ... and that of empty words and empty sentences. ... 'foo bar'", or "take the first word of the sentence 'foo bar'". ... >reason for dynamic scope -- the user should not have to know that there is ...
    (comp.lang.logo)
  • Re: Many global vars in an A97 app - good or bad? Why?
    ... especially the past about appropriate scope. ... >Passing data between forms: why do so few people know about the ... >>Global variables are decidely poorly suited to these two purposes, ... This could be a global class module variable, ...
    (comp.databases.ms-access)
  • Re: Switching From YUICompressor to Closure Compiler Using ANT
    ... if (foo) foo.bar; ... to anyone understanding the difference between function expressions and function declarations. ... One just needs to know ECMAScript syntax and what could be changed into something shorter without changing program behavior. ... a called function can be inlined where it shares scope. ...
    (comp.lang.javascript)
  • Re: How to eliminate this global variable, silent?
    ... Excluding it because it doesn't have global scope is perfectly correct. ... The only argument you can present is: the standard doesn't ... define "global variables". ... yadda yadda. ...
    (comp.lang.c)