I know what my
is in Perl. It defines a variable that exists only in the scope of the block in which it is defined. What does our
do? How does our
differ from my
?
相关问题
- $ENV{$variable} in perl
- JavaScript variable scope question: to var, or not
- Is it possible to pass command-line arguments to @
- Redirecting STDOUT and STDERR to a file, except fo
- Scope on each last elements of a has_many relation
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
- What is a good way to deploy a Perl application?
- Speeding up Selenium Webdriver
It's an old question, but I ever met some pitfalls about lexical declarations in Perl that messed me up, which are also related to this question, so I just add my summary here:
1. definition or declaration?
The output is
var: 42
. However we couldn't tell iflocal $var = 42;
is a definition or declaration. But how about this:The second program will throw an error:
$var
is not defined, which meanslocal $var;
is just a declaration! Before usinglocal
to declare a variable, make sure that it is defined as a global variable previously.But why this won't fail?
The output is:
var: 42
.That's because
$a
, as well as$b
, is a global variable pre-defined in Perl. Remember the sort function?2. lexical or global?
I was a C programmer before starting using Perl, so the concept of lexical and global variables seems straightforward to me: just corresponds to auto and external variables in C. But there're small differences:
In C, an external variable is a variable defined outside any function block. On the other hand, an automatic variable is a variable defined inside a function block. Like this:
While in Perl, things are subtle:
The output is
var: 42
,$var
is a global variable even it's defined in a function block! Actually in Perl, any variable is declared as global by default.The lesson is to always add
use strict; use warnings;
at the beginning of a Perl program, which will force the programmer to declare the lexical variable explicitly, so that we don't get messed up by some mistakes taken for granted.Just try to use the following program :
my is used for local variables, where as our is used for global variables. More reading over Variable Scoping in Perl: the basics .
An example:
Great question: How does
our
differ frommy
and what doesour
do?In Summary:
Available since Perl 5,
my
is a way to declare:$package_name::variable
.On the other hand,
our
variables are:$package_name::variable
.Declaring a variable with
our
allows you to predeclare variables in order to use them underuse strict
without getting typo warnings or compile-time errors. Since Perl 5.6, it has replaced the obsoleteuse vars
, which was only file-scoped, and not lexically scoped as isour
.For example, the formal, qualified name for variable
$x
insidepackage main
is$main::x
. Declaringour $x
allows you to use the bare$x
variable without penalty (i.e., without a resulting error), in the scope of the declaration, when the script usesuse strict
oruse strict "vars"
. The scope might be one, or two, or more packages, or one small block.Let us think what an interpreter actually is: it's a piece of code that stores values in memory and lets the instructions in a program that it interprets access those values by their names, which are specified inside these instructions. So, the big job of an interpreter is to shape the rules of how we should use the names in those instructions to access the values that the interpreter stores.
On encountering "my", the interpreter creates a lexical variable: a named value that the interpreter can access only while it executes a block, and only from within that syntactic block. On encountering "our", the interpreter makes a lexical alias of a package variable: it binds a name, which the interpreter is supposed from then on to process as a lexical variable's name, until the block is finished, to the value of the package variable with the same name.
The effect is that you can then pretend that you're using a lexical variable and bypass the rules of 'use strict' on full qualification of package variables. Since the interpreter automatically creates package variables when they are first used, the side effect of using "our" may also be that the interpreter creates a package variable as well. In this case, two things are created: a package variable, which the interpreter can access from everywhere, provided it's properly designated as requested by 'use strict' (prepended with the name of its package and two colons), and its lexical alias.
Sources: