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
Coping with Scoping is a good overview of Perl scoping rules. It's old enough that
our
is not discussed in the body of the text. It is addressed in the Notes section at the end.The article talks about package variables and dynamic scope and how that differs from lexical variables and lexical scope.
The perldoc has a good definition of our.
This is only somewhat related to the question, but I've just discovered a (to me) obscure bit of perl syntax that you can use with "our" (package) variables that you can't use with "my" (local) variables.
Output:
This won't work if you change 'our' to 'my'.
Will Output this:
In case using "use strict" will get this failure while attempting to run the script:
The PerlMonks and PerlDoc links from cartman and Olafur are a great reference - below is my crack at a summary:
my
variables are lexically scoped within a single block defined by{}
or within the same file if not in{}
s. They are not accessible from packages/subroutines defined outside of the same lexical scope / block.our
variables are scoped within a package/file and accessible from any code thatuse
orrequire
that package/file - name conflicts are resolved between packages by prepending the appropriate namespace.Just to round it out,
local
variables are "dynamically" scoped, differing frommy
variables in that they are also accessible from subroutines called within the same block.