Let's take a look at our usage.
Require 5.006
When the version number is less than 5.006, the failure is returned, causing the module to fail to load.
So its role is to ensure that the module invokes the Perl version of the environment.
Our, like my, are declarations of variables,
But our statement is the package global variable,
And my declaration is a lexical variable.
However, after our declared variables, it will become like a lexical variable,
In fact, this is our purpose: to deceive strict pragma, so that strict think it is a lexical variable, in fact, is not.
There is a simple way to understand our:
1, you will take our declared variables and my declaration as the same.
2, remember the difference between our and my: we are declaring a package global variable, so store in the symbol table (which can be accessed anywhere through full qualification), and my declaration is a true lexical variable that can only be accessed in a closed block.
Use my situation:
Copy Code code as follows:
my $var = 1;
{
my $var = 2;
Print $var, "\ n";
}
Print $var, "\ n";
Output:
2
1
Use our situation:
Copy Code code as follows:
Our $var = 1;
{
Our $var = 2;
Print $var, "\ n";
}
Print $var, "\ n";
Output:
2
2
Attached: our usage in Perl
In fact, our presence has its history,
Unlike other languages, Perl can easily declare variables,
In the age of Perl 4, I didn't need my stuff at all,
Just write a name, it's a variable.
This is still true in Perl 5, unless you explicitly declare it as a lexical variable with my, all variables are (package) global variables and can be used without declaring them directly.
But that's a bad thing, that's when you accidentally write the wrong name, or when the symbol is quoted, the string is wrong, and it can cause a lot of trouble (because, in Perl 5 syntax, these are all correct, and the result is to produce a new variable, obviously, that's not what you want.) )
So, in order to solve these problems, the strict and warnings two pragma are introduced in Perl 5, and their role is to limit the variables without declaring direct use,
After strict and warnings restrictions, all directly used variables that are not declared will have an error. But the variable I declare is a local variable, which cannot create a variable.
So we can't use global variables (note 1),
Therefore, we introduce the role of Our,our is to declare a global variable, but let strict and warnings think it is a lexical variable, so our declared variable is also a lexical scope. But it's actually a global variable.
Note 1:
If we do not use our, we have two ways to create global variables:
1, with no strict "vars" temporarily turn off the strict pragma, the declaration is finished again with use strict "VARs" open.
2, with the fully qualified name of the variable, such as $main:: var or $foo:: Bar this way.
There is no relationship between the three use,package,our.
Use is to load a. PM File,
Package is to toggle the current name space,
Our is to create a variable in the current namespace, and if the variable already exists, we will only act as a declaration.
You still don't understand the difference between our and my.
Our (or any cosmetic) statement is "package global variable", it is "attachment" above the "package", where it is stored in the "Package symbol table",
My declaration is "lexical variable", which is "dependent" on "code block", where it is stored in "variable label thin" of "code block", so lexical variables cannot be accessed outside of code blocks (except for passing references).
But package global variables are different and can be accessed with full qualification.