Analysis of my and our differences in Perl _perl

Source: Internet
Author: User
Tags vars

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

code example

Copy Code code as follows:

my $var = 1;

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

Print $var, "\ n";

Output:

2

1

Copy Code code as follows:

Our $var = 1;

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

Print $var, "\ n";

Output:

2

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

------------------------------------------------------------------------------------

Use
Package
Our
There is no relationship between the three.

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.

------------------------------------------------------------------------------------

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.

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.