In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the Perl package. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Perl package
The Perl program stores the names of variables and subroutines in the symbol table, and the collection of names in the symbol table of perl is called Perl package (package).
1. Definition of Perl package
Multiple Perl packages can be defined in a program, and each Perl package has a separate symbol table. The definition syntax is as follows:
Packagemypack
This statement defines a Perl package named mypack, and the names of all variables and subroutines defined thereafter are stored in the symbol table associated with the Perl package until another package statement is encountered.
Each symbol table has its own set of variables and subroutine names, and each group of names are irrelevant, so you can use the same variable name in different Perl packages and represent different variables. Such as:
$var=14
Packagemypack
$var=6
The * statement creates the variable $var and stores it in the main symbol table, and the third statement creates another variable of the same name $var and stores it in the symbol table of the mypackPerl package.
2. Switch between Perl packages
You can switch back and forth between Perl rooms at any time in the program, such as:
1Parl 2: 3 Pol packagepack1; 4 Variations 26; 5 packagepack2; 6 Variations 34; 7 packagepack1; 8:print ("$packagepack1")
The running results are as follows:
$program
twenty-six
$
The third line defines the Perl package pack1, the fourth line creates the variable $var, which is stored in the symbol table of the Perl package pack1, the fifth line defines the new Perl package pack2, and the sixth line creates another variable $var, which is stored in the symbol table of the Perl package pack2. This leaves two separate $var, stored in different Perl packages. The seventh line specifies pack1 as the current Perl package because the Perl package pack1 is defined so that the definitions and calls of all variables and subroutines are the names stored in the symbol table of the Perl package. So the call to $var on line 8 is $var in the pack1Perl package, with a value of 26.
3. Main package
The default symbol table that stores the names of variables and subroutines is associated with a Perl package named main. If you define other Perl packages in your program, you can re-specify the mainPerl package when you want to switch back to the default symbol table:
Packagemain
In this way, the next program seems to have never defined a Perl package, and the names of variables and subroutines are stored as usual.
4. Reference to Perl package
In a Perl package, you can reference variables or subroutines in other Perl packages by preceding the variable name with the Perl package name and a single quote, such as:
Packagemypack; $var=26; packagemain; print ("$mypack'var\ n")
Here, $mypack'var is the variable $var in the mypackPerl package.
Note: in Perl5, the Perl package name and variable name are separated by a double colon, that is, $mypack::var. Single quotation mark references are still supported, but may not be supported in future versions.
5. Specify no current Perl package
In Perl5, you can specify that there is no current Perl package with the following statement:
Package
At this point, all variables must clearly indicate the name of the Perl package to which they belong, otherwise they will be invalid-- an error.
$mypack::var=21;#ok
$var=21;#error-nocurrentpackage
This is the case until the current Perl package is specified with the package statement.
6. Perl package and subroutine
The definition of the Perl package affects all statements in the program, and Perl includes subroutines, such as:
Packagemypack; subroutinemysub {local ($myvar); # stuffgoeshere}
Here, mysub and myvar are both part of the Perl package mypack. To call the subroutine mysub outside the Perl package mypack, specify the Perl package: $mypack'mysub.
You can switch Perl packages in subroutines:
Packagepack1; subroutinemysub {$var1=1; packagepack2; $var1=2;}
This code creates two variables $var1, one in the Perl package pack1 and the other in the Perl package pack2. The local variables in the Perl package can only be used in statement blocks such as its defined subroutines, just like ordinary local variables.
7. Define private data with Perl package
The most common use of Perl packages is to be used in files that contain global variables used by subroutines and subroutines. Defining such Perl packages for subroutines can ensure that the global variables used by subroutines cannot be used elsewhere, and such data is private data. Further, you can guarantee that the Perl package name cannot be used anywhere else. Private data example:
1 packageprivpack; 2 5:#Thisfunctionisthelinktotheoutsideworld; 3: 4 packagemain; 6:subprintval {7:&privpack'printval (); 8:} 9: 10 11:subprintval packageprivpack; 11:subprintval {12:print ("$valtoprint\ n"); 13:} 14: 15 11:subprintval packagemain; 16 12:print 1 valuerequire
This subroutine can only produce output after calling printval.
The file is divided into two parts: the part that communicates with the outside world and the private part. The former is the default mainPerl package, and the latter is the Perl package privpack. The subroutine printval defined in lines 6-8 can be called by other programs or subroutines. Printval outputs the value of the variable $valtoprint, which is defined and used only in the Perl package privpack. Lines 15 and 16 ensure that it works properly after it is included in the Perl statement by other programs, and line 15 sets the current Perl package back to the default Perl package main,16 line to return a non-zero value so that require does not report an error.
8. Perl packages and system variables
The following variables work in the Perl package even if called from other mainPerl packages:
File variables STDIN,STDOUT,STDERR and ARGV
Variables% ENV,%INC,@INC,$ARGV and @ ARGV
Other system variables containing special characters
9. Access the symbol table
The array% _ package is available to find the symbol table in the program, where package is the name of the Perl package to which the symbol table you want to access belongs. For example,% _ main contains a default symbol table.
There is usually no need to look up the symbol table yourself.
This is the end of the article on "how to use the Perl package". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.