Differences and relationships between my, local, and our in Perl

Source: Internet
Author: User
"Our"," The name is limited to a certain range ", in fact, is to clearly declare a" global variable ", although defined in a module or function, can be accessed outside, if it has already been declared, use "our" again to indicate that the global variable is used, not the private or local variable with the same name.

Our $ program_name = "waiter ";
{

My $ program_name = "something ";
Our $ program_name = "server"; # The our here is the same as the outside, which is different from the previous sentence.
# The Code called here shows "server"
}
# The Code executed here still shows "server ".

"My"," The name and value are limited to a certain range. "Simply put, this variable can only be seen by the module or function at the current layer, but not by the module or function at the higher or lower layers.
Sub greeting1 {
My ($ Hello) = "how are you do? ";
Greeting2 ();
}

Sub greeting2 {
Print "$ Hello/N ";
}
$ Hello = "How are you doing? ";
Greeting2 ();
Greeting1 ();
Greeting2 ();

Running result:
How are you doing?
How are you doing?
How are you doing?
--------------------------
How are you do? No. When you call greeting2 in greeting1, greeting2 cannot see the private $ Hello variable of greeting1. Only the external global variable $ hello can be seen.

"Local"," Limiting the value to a certain range ", also known as" dynamic lexical range ", is a bit hard to understand. In my understanding, the functions at the current layer and the lower layer of the current layer can see the variables at the current layer, but the functions at the upper layer cannot. What is the scope depends not only on the functions of the current layer, but also on the program length and depth of the next layer, so it is called "dynamic range ".

Sub greeting1 {
Local ($ Hello) = "how are you do? ";
Greeting2 ();
}

Sub greeting2 {
Print "$ Hello/N ";
}
$ Hello = "How are you doing? ";
Greeting2 ();
Greeting1 ();
Greeting2 ();

Running result:
How are you doing?
How are you do?
How are you doing?
-----------------------
Is it different from my? When greeting1 calls greeting2, greeting2 can see the local variable $ Hello of greeting1, and the external global variables are hidden.

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.