In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of Perl package and module, which is very detailed and has certain reference value. Friends who are interested must finish it!
Perl packages and modules
Perl packages and modules
Each package in Perl has a separate symbol table, which defines the syntax as follows:
Package mypack
This statement defines a package named mypack, and the names of all variables and subroutines defined thereafter are stored in the symbol table associated with the 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 packages and represent different variables.
Accessing variables from one package to another can be specified by "package name + double colon (::) + variable name".
The default symbol table for storing variables and the names of subroutines is associated with a package named main. If you define other packages in the program, you can re-specify the main package when you want to switch back to the default symbol table:
Package main
In this way, the next program seems to have never defined a package, and the names of variables and subroutines are stored as usual.
The files in the following example have main and Foo packages. The special variable _ _ PACKAGE__ is used to output the package name:
Instance #! / usr/bin/perl# main package $I = 1; print "package name:", _ _ PACKAGE__, "$I\ n"; package Foo;# Foo package $I = 10; print "package name:", _ _ PACKAGE__, "$I\ n"; package main;# reassigns main package $I = 100; print "package name:", _ _ PACKAGE__, "$I\ n" Print "package name:", _ _ PACKAGE__, "$Foo::i\ n"; 1
Execute the above program, and the output is as follows:
Package name: main 1 package name: Foo10 package name: main 100package name: main 10BEGIN and END module
The Perl language provides two keywords: BEGIN,END. They can contain a set of scripts for execution before or after the program body is run.
The syntax format is as follows:
BEGIN {...} END {...} BEGIN {...} END {...}
Each BEGIN module is executed after the Perl script is loaded and compiled, but before other statements are executed.
Each END statement block is executed before the interpreter exits.
BEGIN and END statement blocks are particularly useful when creating Perl modules.
If you don't understand, let's take a look at an example:
Instance #! / usr/bin/perlpackage Foo;print "Begin and Block instance\ n"; BEGIN {print "this is a BEGIN statement block\ n";} END {print "this is an END statement block\ n";} 1
Execute the above program, and the output is as follows:
This is the BEGIN statement block Begin and Block instance. This is the END statement block. What is the Perl module?
The Perl package is used in Perl5 to create the module.
The Perl module is a reusable package with the same name as the package and a file suffix of. Pm.
Below we define a module Foo.pm with the code as follows:
Instance #! / usr/bin/perlpackage Foo;sub bar {print "Hello $_ [0]\ n"} sub blat {print "World $_ [0]\ n";} 1
You need to pay attention to the following points about modules in Perl:
The functions require and use will be loaded into a module.
INC is a special array built into Perl that contains a directory path to the location of the library routine.
The require and use functions call the eval function to execute the code.
At the end 1; execution returns TRUE, which is required, otherwise an error is returned.
Require and Use functions
The module can be called through the require function, as follows:
Example #! / usr/bin/perlrequire Foo; Foo::bar ("a"); Foo::blat ("b")
You can also refer to it through the use function:
Example #! / usr/bin/perluse Foo; bar ("a"); blat ("b")
We noticed that the require reference needs to specify the function using the package name, while use does not. The main difference between the two is:
1. Require is used to load module or perl programs (.pm suffix can be omitted, but .pl must have)
2. The Perl use statement is introduced at compile time, and require is introduced at run time
3. When Perl use introduces the module, it also introduces the sub-module of the module. While require cannot be introduced, it is necessary to redeclare
4. USE is found in the current default @ INC. Once the module is not in @ INC, USE cannot be introduced, but require can specify the path.
5. When USE references a module, if the module name contains a:: double colon, the double colon will be used as a path separator, equivalent to / or\ under Unix or Windows. Such as:
Use MyDirectory::MyModule
You can export list symbols from the module by adding the following statement to the use module:
Require Exporter;@ISA= qw (Exporter)
The @ export array contains the names of variables and functions that are exported by default:
Package Module;requireExporter;@ISA= qw (Exporter); @ EXPORT= qw (bar blat); # default exported symbol sub bar {print "Hello $_ [0]\ n";} sub blat {print "World $_ [0]\ n";} sub splat {print "Not $_ [0]\ n"} # Not exportable 1; create Perl module
You can easily create a Perl module by distributing the included tool h3xs through Perl.
You can type h3xs in command line mode to see its parameter list.
H3xs syntax format:
$h3xs-AX-n ModuleName
Parameter description:
-An ignores autoload mechanism
-X ignores XS element
-n specifies the name of the extension module
For example, if your module is in the Person.pm file, use the following command:
$h3xs-AX-n Person
Executing the above procedure will output:
WritingPerson/ lib/Person.pmWriting Person/Makefile.PLWriting Person/READMEWriting Person/t/Person.tWriting Person/ChangesWritingPerson/MANIFEST
You can see the newly added directories and file descriptions in the Person directory:
README: this file contains some installation information, module dependencies, copyright information, etc.
Changes: this file is used as a modification log (changelog) file for your project.
Makefile.PL: this is the standard Perl Makefile constructor. Used to create a Makefile.PL file to compile the module.
MANIFEST: this file is used to automatically build module version distributions of type tar.gz. In this way, you can take your module to CPAN to publish or distribute it to others. It contains a list of all the files you have in this project.
Person.pm: this is the main module file that contains your mod_perl handle code (handler code).
Person.t: some test scripts for this module. By default, it just checks the loading of the module, and you can add some new test units.
T /: test file
Lib/: the directory where the actual source code is stored
You can use the tar (on Linux) command to package the above directories as Person.tar.gz.
Install the Perl module
We can extract and install the Person.tar.gz file we just compressed, and perform the following steps:
Tar xvfz Person.tar.gzcd Personperl Makefile.PLmakemake install
First run "perl Makefile.PL" to generate Makefile in the current directory
Then run "make" to compile and create the required library files
Then use "make test" to test whether the compilation results are correct; finally, run "make install" to install the library files to the system directory, and the whole compilation process is finished.
The above is all the content of the article "sample Analysis of Perl packages and Modules". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.