In terms of scoping, there are three kinds of variables in Perl.
Lexical variables are lexically scoped, which means they are only visible in the current lexical scope (basically file or block).
Package variables, on the other hand, can be used using their qualified form (e.g. $Foo::x
) from anywhere in the interpreter, and they can be used without qualification by any code that shares the variable's package.
Certain package variables are visible without qualification anywhere in the interpreter. These include punctuation vars and a few named vars such as @ARGV
and STDOUT
. For example, $x
refers to $Foo::x
when in package Foo
and $Bar::x
when in package Bar
(assuming no lexical var named $x
is in scope), but $_
always refers to $::_
.
Variables are destroyed when they are no longer referenced.
Here are ways to create variable.
my
and state
create a lexical variable.
our
creates a lexical variable that is aliased to the variable of the same name in the current package. In other words, our $x;
is equivalent to my \$x = \$Foo::x;
when in package Foo
.
Package variables are created on use.
local
doesn't create any variables. It simply backs up a variable until the current lexical scope is destroyed. It is restored from its backed-up value at that point.
my
does the same thing.
No. local
does not change the scope of a variable. While a lexical variable is only visible in a lexical scope, a localized package variable is still visible across the entire interpreter.
$x = 123;
sub foo { print "$x\n"; }
{ local $x = 456; foo(); } # 456
foo(); # 123
$x = 123;
sub foo { print "$x\n"; }
{ my $x = 456; foo(); } # 123
foo(); # 123
What else for local
local
is primarily used to approximate the functionality of my
for variables that cannot otherwise be declared lexically.
Historically, that was all variables. Since 5.6, only punctuation variables cannot be declared lexically.
What is "global" variable?
A global variable is a variable that can seen globally.
All package variables can be seen by any code in the interpreter, so they're all global.
Or are they? To see them from other packages, you need to qualify them. Are $x
and $Foo::x
the same variable?
To some, global variables refers to the set of package variables you can use unqualified. It means that package
changes the set of global variables. And since the package
directive is usually used on a file-basis, that means file-level lexicals are also effectively global by this definition. And they are indeed called that sometimes.
But if the package
changes the set of variables that are global, then they're not really global, are they? So think some people, which only consider punctuation variables (e.g. $_
) and the few named variables that can be used unqualified from anywhere (*::STDOUT
) to be global.
In short, it's a pretty useless term.
Is it possible to add the my scoped variables in @EXPORT array and use it in another packages?
No. @EXPORT
is used by Exporter. Exporter would not be able to find anything but package symbols (since files are compiled in fresh lexical scopes), so @EXPORT
must only contain package symbols.