Differences between my and our in perl

Source: Internet
Author: User
Tags vars

Usage of our in perl
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.

Code example

Copy codeThe Code is as follows: my $ var = 1;

{
My $ var = 2;
Print $ var, "\ n ";
}

Print $ var, "\ n ";

Output:

2

1

Copy codeThe Code is as follows: our $ var = 1;

{
Our $ var = 2;
Print $ var, "\ n ";
}

Print $ var, "\ n ";

Output:

2

2
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.

Bytes ------------------------------------------------------------------------------------

Use
Package
Our
There is no relationship between the three.

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.

Bytes ------------------------------------------------------------------------------------

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.