Scope is the valid range and visible range of variables. variables are valid only in scope;
In Perl, data scopes are classified into global scopes, lexical scopes, and dynamic scopes. In PE rl5, variables and variable scopes can be declared using the keyword "my" and "local;
1. Lexical domain: lexical
Variables declared using the my keyword are called lexical variables. The scope of lexical variables is the lexical domain. Lexical variables are local variables only in the scope. The generated lexical variables are not in the symbol table of the package, it can only exist and be accessed in its own block;
The syntax for declaring lexical variables using the my keyword is:
My (@ array );
Or
My ($ variable, @ array );
In this way, one or more lexical variables are declared. In perl5, these lexical variables can only be declared inCodeBlock exists and accesses. Once the block declares these lexical variables, these lexical variables cannot be accessed again, just as a bucket is allocated to these lexical variables, when the block ends, the storage space for storing these lexical variables is released. After the block is released, it cannot be accessed;
Lexical variables can be used to control the scope of a variable to reduce unnecessary losses. For example, to avoid overwriting the value of a variable with the same name, perl5ProgramAll members use the my keyword to declare variables;
The existence of lexical variables cannot be understood as ending when it leaves the scope where it is located, and the existence time of lexical variables is subject to certain rules. perl5 sets a reference count for each lexical variable, the reference count determines the time when the lexical variable exists. When the lexical variable is out of the scope, the lexical variable may not end. Each lexical variable has its own reference count. When defining and declaring lexical variables, the reference count is 1. When the block where the lexical variable is located ends, the reference count decreases by 1. When the reference count of the lexical variable is 0, the perl5 interpreter Automatically releases the space of the lexical variable in the memory;
A lexical variable is a private variable that can only be referenced by objects in its block. These features of lexical variables enable perl5 to implement recursive calls to subprograms. The recursive call of a subroutine means that the subroutine itself is directly or indirectly called during the execution of the subroutine. In the recursive call process, the subroutine should avoid its own call without termination;
Ii. dynamic scope:
In perl5, the local keyword is used to specify the dynamic scope of a variable, but the my keyword is more effective than the local keyword. The dynamic scope variable is used to temporarily change the value of an existing global variable. Dynamic scope variables can be accessed only in the declaration and definition of its blocks and the subroutines it calls. In addition to defining and declaring blocks of dynamic scope variables, these dynamic scope variables cannot be accessed. When the dynamic scope variable replaces the global variables, the global variables are automatically backed up. When the dynamic scope variable leaves the scope, the global variables automatically restore the original value, that is, the dynamic scope variable changes dynamically. The syntax for defining the dynamic scope variable is as follows:
Local (@ array );
Or
Local ($ variable, @ array );
In this way, one or more dynamic scope variables can be defined;
These features of dynamic scope variables make it very useful when using internal global variables such as argv, $ _, @ INC and $ "in a program; when you modify the original value of a global variable in the scope, you must restore the value of the global variable in time. Dynamic scope variables can automatically save and restore the modified backups of global variables;