Let's take a look at the usage of our.
Require 5.006
If the version number is less than 5.006, a failure is returned, causing the module to fail to be loaded.
Therefore, it is used to ensure the Perl version of the module call environment.
Like my, our declares variables,
However, our declares global variables,
My declares lexical variables.
However, the variable declared by our will become like a lexical variable,
In fact, this is also the purpose of our's existence: To cheat strict pragma, so that strict thinks it is a lexical variable, but it is not actually.
There is a simple way to understand our:
1. You will regard the variables declared by our as the same as those declared by my.
2. Remember the difference between our and my: our declares a package global variable, so it is stored in the symbol table (which can be accessed anywhere through full limits ), my declares a real lexical variable, which can only be accessed in closed blocks.
My usage:
Copy codeThe Code is as follows: my $ var = 1;
{
My $ var = 2;
Print $ var, "\ n ";
}
Print $ var, "\ n ";
Output:
2
1
Our usage:
Copy codeThe Code is as follows: our $ var = 1;
{
Our $ var = 2;
Print $ var, "\ n ";
}
Print $ var, "\ n ";
Output:
2
2
Appendix: usage of our in perl
In fact, the appearance of our has its history,
Perl is different from other languages. variables can be declared as needed,
In the era of Perl 4, I don't need anything at all,
Just write a name, which is a variable.
This is still true in Perl 5. Unless you explicitly declare it as a lexical variable with my, all variables are global variables and can be directly used without declaration.
However, the downside is that if you accidentally write the wrong name or parse the symbolic reference, it will cause a lot of trouble (according to the Perl 5 syntax, these are all correct. The result is to generate a new variable. Obviously, this is not your purpose .)
To solve these problems, we have introduced the strict and warnings pragma in Perl 5. Their role is to restrict the direct use of variables without declaration,
After strict and warnings restrictions, all variables directly used without declaration will report an error. However, the variables declared by my are local variables, and local variables cannot be created,
Therefore, we cannot use global variables (note 1 ),
Therefore, our is introduced again. The role of our is to declare a global variable, but make strict and warnings think it is a lexical variable. Therefore, the variables declared by our are also lexical. But it is actually a global variable.
Note 1:
If you do not use our, you can create global variables in two ways:
1. use no strict "vars" to temporarily turn off the strict pragma, declare that it is used up and use strict "vars" to open it.
2. Use the variable's fully qualified name, such as $ main: var or $ foo: bar.
There is no relationship between use, package, and our.
Use is to load a. pm file,
Package refers to switching the current namespace,
Our creates a variable in the current namespace. If the variable already exists, our serves only one declaration.
You still don't understand the difference between our and my.
Our (or no modifier) declares the "package global variable", which is "attached" to the "package, its storage location is "Package symbol table ",
My declares "lexical variables", which are "attached" to the "code block". Its storage location is "variable label thin" of the "code block ", therefore, lexical variables cannot be accessed from outside the code block (except for passing references ).
But the global variables of the package are different and can be accessed with full limits.