Source:
Http://www.jb51.net/article/35528.htm
Our usage in Perl
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 calls the Perl version of the environment.
Our, like my, are declarations of variables,
But our statement is the package global variables,
And my statement is a lexical variable.
However, with our declared variables, it becomes like a lexical variable,
In fact, this is the purpose of our existence: to deceive strict pragma, so that strict think it is a lexical variable, but actually not.
There is an easy way to understand our:
1, you will consider the variables we declare as the same as my declaration.
2, remember the difference between our and my: We declare a package global variable, so in the symbol table is stored (can be fully qualified to access anywhere), and my declaration is a real lexical variable, can only be accessed in a closed block.
Code examples
The code is as follows:
1 my $var = 1;
2
3
4 {
5 my $var = 2;
6 print $var, "\n";
7 }
8
9 print $var, "\n";
Output:
2
1
The code is as follows:
1 our $var = 1;
2
3
4 {
5 our $var = 2;
6 print $var, "\n";
7 }
8
9 print $var, "\n";
Output:
2
2
Our usage in Perl
In fact, our appearance has its history,
Perl is different from other languages, you can declare variables casually,
In Perl 4, there was no need for my or anything,
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, otherwise all variables are (package) global variables and can not be declared for direct use.
However, there is a disadvantage, that is, million accidentally wrote the wrong name, or to solve the symbolic reference, the string operation is wrong, will cause a lot of trouble (because according to Perl 5 syntax, these are correct, the result is to produce a new variable, it is clear that this is not the purpose you want.) )
Therefore, in order to solve these problems, in Perl 5 introduced the strict and warnings two pragma, their role is to limit the variable is not declared direct use,
After strict and warnings restrictions, all non-declared directly used variables will be error-free. But the variables I declare are local variables, and local cannot create variables,
So, we can't use global variables (note 1),
Therefore, 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 variables are also 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 switch off strict pragma, declared finished with use strict "VARs" open.
2, use the fully qualified name of the variable, such as $main:: var or $foo:: Bar this way.
------------------------------------------------------------------------------------
Use
Package
Our
There is no relationship between the three.
Use is to load a. PM File,
The package is to toggle the current namespace,
Our purpose is to create a variable in the current namespace, and if the variable already exists, our only function is to declare it.
------------------------------------------------------------------------------------
Our (or nothing else) declares the "Package global variable", which is "attached" above "package", where it is stored in "Package symbol table",
My statement is "lexical variable", which is "dependent" on the "code block", where it is stored in the "code block" variable label, so lexical variables can not be accessed from outside the code block (in addition to passing the reference).
But the package global variables are different and can be accessed with full qualification.
Analysis of the difference between my and our in Perl