To be honest, this thing is a bit of a toss of people, mainly in the book's statement is very obscure, in fact, it is not difficult to understand.
Our, "Limit your name to a certain scope," in fact, it is explicitly declared a "global variable", although it is defined in a module or function, the outside can also be accessed, if it has been declared, again with "our", indicating that the use of the global, not the same name of the private or local variables
Copy Code code as follows:
Our $PROGRAM _name = "Waiter";
{
My $PROGRAM _name = "something";
Our $PROGRAM _name = "Server"; #这里的our和外面的相同, different from the preceding sentence.
# The code called here sees ' server '
}
# The code executed here still sees ' server '.
My, "Limit the name and value are limited to a certain scope", in short, is only this layer module or function can see this variable, one layer or lower layer are not visible.
Copy Code code as follows:
Sub greeting1{
My ($hello) = "How are";
Greeting2 ();
}
Sub greeting2{
print "$hello \ n";
}
$hello = "How are you doing?"
Greeting2 ();
Greeting1 ();
Greeting2 ();
Run Result:
Copy Code code as follows:
How are you doing?
How are you doing?
How are you doing?
How about a are you? No, when you call Greeting2 in Greeting1, Greeting2 can't see the greeting1 private $hello variable, you can see only the global variables outside $hello
Local, "To limit the value of a certain range", also known as "dynamic lexical range", a bit difficult to understand. My understanding is that this layer and the lower layer of the function can see the variables in this layer, but the layer of the upper layer is not. What the scope is, not only depends on the function of this layer, but also on the next level of the program length and depth, so called "dynamic range."
Copy Code code as follows:
Sub greeting1{
Local ($hello) = "How are";
Greeting2 ();
}
Sub greeting2{
print "$hello \ n";
}
$hello = "How are you doing?"
Greeting2 ();
Greeting1 ();
Greeting2 ();
Run Result:
Copy Code code as follows:
How are you doing?
How are to do?
How are you doing?
It's not the same as my time, is it? At this point when the greeting1 call Greeting2, greeting2 can see greeting1 local variable $hello, the external global variable of course hidden.