home PYTHONJAVA
 

Perl programs and modules

Each package in Perl has a separate symbol table with a definition syntax of:

package mypack;

This statement defines a package named mypack. The names of all variables and subroutines defined thereafter are stored in the symbol table associated with the package until another package is encountered. b> until the statement.

Each symbol table has its own set of variables, subroutine names, and each group name is irrelevant, so you can use the same variable name in different packages, and represent different variables.

Variables that access another package from one package can be specified by "package name + double colon ( :: ) + variable name".

The default symbol table for storing the names of variables and subroutines is associated with a package named main. If you have other packages defined in the program, you can re-specify the main package when you want to switch back to using the default symbol table:

package main;

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 examples have main and Foo packages. Special variable __PACKAGE__ is used to output the package name:

Instance

#!/usr/bin/perl # essential package $i= 1 ; print "Package call: " , __PACKAGE__ , " $in"; package Foo; # Foo bundle $i = 10 ; print "Package name: " , __PACKAGE__ , " $in"; package major; # Re-specify the principle bundle $i= a hundred ; print "Package call: " , __PACKAGE__ , " $in"; print "Package call: " , __PACKAGE__ , " $Foo::in< /span>"; 1;

Execute the above program, the output is:

package call : main 1
Package call : Foo 10
Package call : primary 100
Package call: major 10

BEGIN and END modules

The Perl language presents two keywords: BEGIN, END. They can every contain a fixed of scripts for execution before or after the frame of this system.

The

syntax is as follows:

BEGIN  ... < /span>
END  ... 
BEGIN  ... 
END  ... 
  • Each BEGIN module is completed after the Perl script is loaded and compiled however earlier than other statements are completed.

  • Each END statement block is finished before the interpreter exits.

  • The
  • BEGIN and END announcement blocks are specially beneficial while growing Perl modules.

If you don't understand it, we are able to see an example:

Instance

#!/usr/bin/perl package deal Foo; print "Begin and Block timesn"; BEGIN print "This is a BEGIN statement blockn" END print "This is the END announcement blockn" 1;

Execute the above program, the output is:

This is BEGIN assertion block
Begin and  Block Instance
This is  END Statement Blocks

What is a Perl module?

Perl5 makes use of the Perl bundle to create modules.

The Perl module is a reusable package with the equal name because the package name and a described record suffix of .Pm.

The following we've defined a module Foo.Pm, the code is as follows:

Instance

#!/usr/bin/perl package deal Foo; sub bar print "Hello $_[0]n" sub blat print "World $_[0]n" 1;

The following factors should be noted about modules in Perl:

    The
  • features require and use will load a module.

  • @INC is a unique array constructed into Perl that includes a listing direction to the location of the library routine.

  • The
  • require and use capabilities name the eval feature to execute the code.

  • End 1; Execution returns TRUE, that's required, in any other case an blunders is again.


Require and Use capabilities

The

module can be called with the require characteristic as follows:

Instance

#!/usr/bin/perl require Foo; Foo::bar ( "a" )< /span>; Foo::blat ( "b" )< /span>;

also can be referenced by way of the use function:

Instance

#!/usr/bin/perl use Foo; bar( "a" ); blat( "b" );

We observed that the require reference desires to apply the package deal name to specify the characteristic, and use does not, the primary distinction among the two is:

  • 1, require is used to load the module or perl program (.Pm suffix can be neglected, however .Pl should be)
  • 2, the Perl use assertion was added at collect time, and the require turned into added at runtime
  • 3. Perl use introduces modules and introduces submodules of modules. And require can not be introduced, to be re-declared
  • four, USE is to discover in the contemporary default @INC, as soon as the module isn't in @INC, use USE can not be introduced, however require can specify the direction
  • five. When USE refers to the module, if the module name carries :: double colon, the double colon can be used because the course separator, that's equivalent to Unix// under Windows. Such as:

    use MyDirectory::MyModule

The list symbol can be exported from the module by including the subsequent announcement:

require Exporter;
@ISA = qw(Exporter);
The

@EXPORT array consists of the names of the variables and capabilities exported via default:

bundle Module;

require Exporter ;
@ISA = qw(Exporter);
@EXPORT = qw(bar blat); # default exported symbols

sub bar  print "Hello $_[0]n" 
sub blat  print "World $_[0]n" 
sub splat  print "Not $_[0]n"  # Not exported !

1;

Create a Perl module

It's easy to create a Perl module by using distributing the local device h2xs with Perl.

You can kind h2xs in command line mode to see a list of its parameters.

h2xs syntax layout:

$ h2xs -AX < /span>-n ModuleName

Parameter description:

  • -A Ignore the autoload mechanism

  • -X Ignore XS elements

  • -n Specify the call of the extension module

For instance, if your module is in a Person.Pm record, use the subsequent command:

$ h2xs -AX < /span>-n Person

Executing the above application will output:

Writing Person/lib/Person< /span>.pm Writing Person /Makefile.PL Writing Person /README Writing Person /t/Person.t Writing Person /Changes Writing Person /MANIFEST

Person listing you could see the newly delivered listing and document description:

  • README: This record carries a few set up information, module dependencies, copyright statistics, etc.

  • Changes: This file is used as a changelog record to your undertaking.

  • Makefile.PL : This is the same old Perl Makefile constructor. Used to create a Makefile.PL report to collect the module.

  • MANIFEST: This file is used to mechanically build a module version distribution of type tar.Gz. This way you may take your module to CPAN for distribution or distribution to others. It contains a listing of all of the files you have got in this project.

  • Person.Pm : This is the principle module file, containing your mod_perl handler code.

  • Person.T : Some check scripts for this module. By default it just exams the loading of the module, you may add some new check gadgets.

  • t/ : Test report

  • lib/ : The listing in which the real supply is stored

You can use the tar (on Linux) command to bundle the above directory as Person.Tar.Gz.


Install Perl module

We can unpack the Person.Tar.Gz document that we simply compressed. The steps are as follows:

tar xvfz Person.< /span>tar.gz Cd Person Perl Makefile.PL Make Make set up

First run "perl Makefile.PL" to generate Makefile inside the cutting-edge listing;

Then run "make" to bring together and create the desired library documents;

After the usage of "make check" to check whether the compilation result is accurate; sooner or later run "make install" to put in the library record to the gadget listing, and the complete compilation procedure ends.

...





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.