Symbol table
Each package has its own symbol table, which is a hash table. The keyword is the variable name, file handle, directory handle, and subroutine in the package. The value is typeglob.
Each symbol is assigned a typeglob (* X), which indicates all types named X.
Each package has its own symbol table. Whenever the package declaration is used, switch to the symbol table of the package. You can use the "package name + double colon (:) + variable name" method to access the variables of another package from one package. Because the variables assigned by my function cannot be accessed from outside the package, and they are not stored in the Package symbol table, they are stored inProgramIn the created buffer. Therefore, when using the "my" variable, you cannot access them through the symbol table of the package, because they are not there!
Require Function
Require can import the Perl library and then execute the routines orCodeIt is similar to # include in C.
The format of require is: require expr. You can also import $ _ variable values without parameters. The library file imported by require must exist in a path contained by @ INC. Otherwise, the function fails to be executed and returns information similar to the following:
1$ ./Require.PL2Can'T locate hello. PL in @ INC (@ INC contains: /usr/local/lib/perl5/usr/local/share/perl5/usr/lib/perl5/vendor_perl/usr/share/perl5/vendor_perl/usr/lib/perl5/usr /share/perl5 .) at. /require. PL line 3.
Test code:
1 # ! /Usr/bin/perl 2 Require " PWD. pl " ; 3 Require " Ctime. pl " ; 4 & Initpwd; # The initialization function, which is located in PWD. pl. 5 Printf " The present working directory is % s \ n " , $ ENV {PWD }; 6 & Chdir ( " ../.. " ); # Switch directory. This function is located in PWD. pl. 7 Printf " The present working directory is % s \ n " , $ ENV {PWD }; 8 $ Today = & Ctime ( Time ); # Obtain the time. This function is located in ctime. pl. 9 Print " $ Today " ;
Execution result:
1$ ./Require.PL2The present working directory is/home/beyes/perl/Package3The present working directory is/home/beyes
If you open a library file such as PWD. pl or ctime. pl, you may notice that the last statement in the library file is "1 ;". This is required for the require function. At this time, the require function will not load the file into the program. We can also write our own library by imitating PWD. pl and put it in the correct directory so that we can use the custom library. When using a custom library, do not forget to write "1;" in the last line. Otherwise, a similar error message will be returned when using require: XXX. PL did not return a true value at XXX. PLX lines 5.
@ INC Array
@ INC is a special array built in Perl. It contains the directory path pointing to the location of the library routine. If the library you include is not under these directories, you can use the-L option switch in the command line, which is similar to using the-L option for third-party libraries during GCC compilation.@ INC is useless to the use command. It only serves the require command. The use command directly accesses the Perl Module Directory (for example,/usr/lib/perl5/5.6 /)
You can use the perl-V command to view the content of the @ INC array:
1 $ Perl-V2...3@ INC:4/Usr/Local/Lib/Perl55/Usr/Local/Share/Perl56/Usr/lib/perl5/Vendor_perl7/Usr/share/perl5/Vendor_perl8/Usr/lib/Perl59/Usr/share/perl5
The-E Option of the Perl Command can be followed by the command, so that the @ INC content can be printed separately:
1$ Perl-e'Print "@ INC \ n"'2/Usr/Local/Lib/perl5/usr/Local/Share/perl5/usr/lib/perl5/vendor_perl/usr/share/perl5/vendor_perl/usr/lib/perl5/usr/share/perl5.
As shown in the preceding figure, the last element of the @ INC array is the current directory. If you want to call the current working directory the first element in the search path, you can use unshift (@ INC ,"..
Package concept
A separate namespace is also called a package ). A separate namespace means that Perl holds a separate symbol table for all the variables in the namepackage. By default, all the variables in the package are global variables, while the package mechanism allows users to switch the namespace, so that the variable with the weight is called a private variable, even if they have the same name outside the package.
Package Scope
The package scope starts from the declared position until the end of the file or the end of the inmost closed block. It can also be the declaration of another package. The Package function is used to declare another package. Reference the variables in another package in one package. You can add a special symbol that represents the variable data type before the package name, followed by a double colon and variable name, for example, $ friend: Name.
Switch between Perl packages and reference the packages:
1 # ! /Usr/bin/perl 2 3 @ Town = QW (China Wencheng qinglan ); 4 $ Friend = " Jasmine " ; 5 # In the main package 6 Print " In Main: \ $ friend is $ friend \ n " ; 7 # Declare the boy package and switch to the boy 8 Package Boy; 9 # $ Name in the namespace of the boy package does not interfere with each other in main. 10 $ Name = " Tony " ; 11 Print " In boy \ $ name is $ name. \ n " ; 12 # Use $ friend in the main package and modify it. 13 $ Main : Friend = " Obama " ; 14 Print " In boy \ @ town is @: town \ n " ; # The main package is the default package. You do not need to write the main name. Replace the main with the @ symbol. 15 # Switch back to main package 16 Package Main; 17 Print " In Main: \ $ name is $ name \ n " ; 18 Print " In Main: \ $ main is $ BOY: Name \ n " ; 19 Print " In Main: \ $ friend is $ friend. \ n " ;
Running result:
1In Main:$ FriendIs jasmine2In boy$ NameIs Tony.3In boy@ TownIs China Wencheng qinglan4In Main:$ NameIs5In Main:$ MainIs Tony6In Main:$ FriendIs Obama.
Create a package and its Child routines:
1 # ! /Usr/bin/perl 2 Unshift ( @ INC , " D: \ perlwork1 " ); 3 Require " Average. pl " ; 4 Print " Enter your midterm scores \ n " ; 5 6 @ Scores = Split ( ' ' , <Stdin> ); 7 8 Printf " The average is %. Lf. \ n " , Average: ave ( @ Scores ); # Ave subroutine in application package
Create package
1 # ! /Usr/bin/perl 2 Package Average 3 Sub Ave 4 { 5 My ( @ Grades ) = @_ ; 6 My ( $ Num_of_grades ) = $ # Grades + 1; 7 Foreach $ Grade ( @ Grades ) 8 { 9 $ Total + = $ Grade ; 10 } 11 $ Total / $ Num_of_grades ; 12 } 13 1 ;
Module
Perl5 uses a Perl package to create a module by creating a Perl Package and storing it in a file with the same name. For example, a Perl package named mymodult is stored in the mymodult. PM file (the extension. PM indicates perlmodule ). The module mymodult in the following example contains the subprograms myfunc1 and myfunc2 and the variables $ myvar1 and $ myvar2.
1 # ! /Usr/bin/perl 2 Package Mymodule; # Define the module name mymodule. PM 3 Require Exporter; # Use the exporter. PM module to export the module symbol to another package (loader) 4 @ Isa = QW (exporter ); # @ Isa the array contains the package name required by this module. This module uses the module exporter. PM. 5 # @ Export lists the child routines exported from this module to the module caller by default. Myfunc3 is not in the Export List (@ export). The caller cannot directly call myfunc3. 6 @ Export = QW (myfunc1, myfunc2 ); 7 @ Export_ OK = QW ( $ Myvar1 $ myvar2 ); 8 9 Sub Myfunc1 10 { 11 $ Myvar1 + = 1 ; 12 } 13 14 Sub Myfunc2 15 { 16 $ Myvar2 + = 2 ; 17 } 18 19 Sub Myfunc3 20 { 21 $ Myvar3 + = 2 ; 22 }
Note: The. PM file is not only placed in the Perl working library directory, but also in the current directory.