HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Perl Each package has a separate symbol table definition syntax is:
package mypack;
This statement defines a package calledmypack in the name of all the variables and subroutines defined hereinafter are stored in the symbol table associated with the package, the packageuntil it encounters another statement so far.
Each symbol table has its own set of variables, subroutines, each group name is not relevant, so you can use the same variable name in different packages, which represent different variables.
Access from one package to another package variable, you can specify the "package name + double colon (::) + variable name" approach.
Storing variables and subroutine names default symbol table associated with the package is calledmain-linked.If the program is defined in other packages, when you want to switch back to using the default symbol table, you can reassign the main package:package main;
Thus, the next program as if never defined packages, variables and subroutine names as usual storage.
The following example file has main and Foo package.__PACKAGE__ Special variable for output package names:
#!/usr/bin/perl # main 包 $i = 1; print "包名 : " , __PACKAGE__ , " $i\n"; package Foo; # Foo 包 $i = 10; print "包名 : " , __PACKAGE__ , " $i\n"; package main; # 重新指定 main 包 $i = 100; print "包名 : " , __PACKAGE__ , " $i\n"; print "包名: " , __PACKAGE__ , " $Foo::i\n"; 1;
The above program, the output is:
包名 : main 1 包名 : Foo 10 包名 : main 100 包名: main 10
Perl language provides two keywords: BEGIN, END. They can be a separate set of scripts that execute before the program runs or body after running.
Syntax is as follows:
BEGIN { /en. } END { /en. } BEGIN { /en. } END { /en. }
EachBEGIN block but executed before other statements executed after the Perl script loaded and compiled.
EachEND block of statements executed before the interpreter quits.
BEGIN and ENDblock is particularly useful when you create a Perl module.
If you do not understand big, we can see instances:
#!/usr/bin/perl package Foo; print "Begin 和 Block 实例\n"; BEGIN { print "这是 BEGIN 语句块\n" } END { print "这是 END 语句块\n" } 1;
The above program, the output is:
这是 BEGIN 语句块 Begin 和 Block 实例 这是 END 语句块
Perl5 with Perl package to create a module.
Perl module is a reusable package, the module name and the same package name, file extension defined for.pm.
Below we define a module Foo.pm, code as follows:
#!/usr/bin/perl package Foo; sub bar { print "Hello $_[0]\n" } sub blat { print "World $_[0]\n" } 1;
Perl modules concerning to note the following:
Function anduse will requirea load module.
@INC Perl is a special built-in array that contains the directory path to the location of the library routines lies.
require and usefunction callsevalfunction to execute code.
End1; execution returns TRUE, this is a must, otherwise it returns an error.
Module can be invoked by therequire function as follows:
#!/usr/bin/perl require Foo; Foo::bar( "a" ); Foo::blat( "b" );
You can also use the function to refer to:
<pre> #!/usr/bin/perl use Foo; bar( "a" ); blat( "b" );
We note the need to require a reference to the package name specified function, and do not need to use, the main difference between the two is:
use MyDirectory::MyModule
You can export a list of symbols from the module by adding the following statement to use modules:
require Exporter; @ISA = qw(Exporter);
@EXPORT Array contains default variables and functions derived from the name:
package Module; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(bar blat); # 默认导出的符号 sub bar { print "Hello $_[0]\n" } sub blat { print "World $_[0]\n" } sub splat { print "Not $_[0]\n" } # Not exported! 1;
You can easily create a Perl module Perl distribution comes with a tool h2xs.
You can type in the command line mode h2xs look at its parameter list.
h2xs syntax:
$ h2xs -AX -n ModuleName
Parameter Description:
-A Ignored autoload mechanism
-X Ignored XS element
-n Specifies the extension name
For example, if your modulePerson.pm file, use the following command:
$ h2xs -AX -n Person
The above program will output:
Writing Person/lib/Person.pm Writing Person/Makefile.PL Writing Person/README Writing Person/t/Person.t Writing Person/Changes Writing Person/MANIFEST
Person under the directory you can see the newly added directories and files Explanation:
README: This file contains information about installation, module dependencies, copyright information, and so on.
Changes: This change log file as your project (changelog) file.
Makefile.PL: This is the standard Perl Makefile builder. Used to create a Makefile.PL file to compile the module.
MANIFEST: This file is used to automatically build tar.gz type of module version distribution. So you can put your CPAN module to get published or distributed to others. It contains a list of all of your files in this project.
Person.pm: This is the main module file that contains the code for your mod_perl handler (handler code).
Person.t: for this module some test scripts. By default, it's just checking module loaded, you can add some new test unit.
t /: test file
lib /: actual source storage directory
You can use tar (Linux on) command to the above directory packaged as Person.tar.gz.
We can justPerson.tar.gz compressed file to decompress the installation, perform the following steps:
tar xvfz Person.tar.gz cd Person perl Makefile.PL make make install
First run "perl Makefile.PL" in the current directory generated Makefile;
Then run "make" to compile and create the required library file;
Followed by "make test" test compile the results are correct; the last run "make install" to install the library files to the system directory, thus ending the entire compilation process.