Perl Package And Module

                                                                                                                         

 

Perl Package And Module

Module:

  • A Perl modules are namespaces that contain function and variables.
  • A Perl module is a reusable package defined in a library file whose name is the same as the name of the package with a .pm as extension.

Why Module:

  • Organize the components of you program
    • Separate large programs in functional blocks
  • Code reuse
    • A module can easily be included in a new script
  • Sharing
    • A module is a convenient way to publish or share your code with others. There are a lot of modules with a wide range of functions available for Perl.

Namespace :

  • A namespace provides a certain context to functions/variables.

 

Introduction to Package :

  • A package is a collection of code which lives in its own namespace .
  • A namespace is a named collection of unique variable names (also called a symbol table).
  • Namespaces prevent variable name collisions between packages .
  • Packages enable the construction of modules.
  • You can explicitly refer to variables within a package using the :: package qualifier.

Example:

#!/usr/bin/perl

# This is main package

$i = 1;

print “Package name : ” , __PACKAGE__ , ” $i\n”;

package Foo; # This is Foo package

$i = 10;

print “Package name : ” , __PACKAGE__ , ” $i\n”;

1;

package main; # This is again main package

$i = 100;

print “Package name : ” , __PACKAGE__ , ” $i\n”;

print “Package name : ” , __PACKAGE__ , ” $Foo::i\n”;

Result:

Package name : main 1

Package name : Foo 10

Package name : main 100

Package name : main 10

Note: This example having main and Foo packages in a file. Here special variable __PACKAGE__ has been used to print package name.

 

Package Declaration:

  • In order to designate that a code belongs to a different namespace you should use the package directive.
  • For instance, if you want your module name to be “MyModule” your file should look something like this:

Example:

# This is the file  MyModule.pm

package MyModule;

use strict;

use warnings;

sub a_function {

print “Hello, World!\n”;

}

1;

Note that a module has to return a true value to the perl interpreter, which explains the use of “1;”.

 

  • A namespace may contain sub-namespaces. To separate namespace components, use the :: separator.

Example:

package pack1::pack2;

use strict;

use warnings;

my $counter = 0;

sub get_counter  {

   return $counter++;

}

1;

 

Where to find a module:

  • A module is a separate file that may contain one or more different namespaces.
  • That file is found in a place that corresponds to the module’s name.
  • To find the filename of the module relative to the script’s directory, replace every :: with a slash and add “.pm” to the name of the last component.
  • For instance: the MyModule module will be found in the file “MyModule.pm”;
  • The Hello::World module will be found in the file “World.pm” under the Hello sub-directory; etc.

 

Loading Modules and Importing their Functions :

  • In order to have access to a module from within your script (or from within another module) you can use the use directive followed by the name of the module as it is deduced from its path. Here’s an example: assume that the file “MyModule.pm” is this:

Module Add Module to your script:

Result: Hello,World!

 

Using Variables from a Different Namespace :

  • It is also possible to use the global variables of different packages. However, such variables need to be declared using the use vars qw($myvar1 @myvar2) construct.
  • Here’s an example for a module that defines a variable and another one that access it:

 

BEGIN and END:

  • There are two special code blocks for Perl modules – BEGIN and END. These blocks are executed when a module is first loaded, and when the Perl interpreter is about to unload it, respectively.
  • Here’s an example for a logging module that makes use of this facility:

 

Difference between Namespaces and Modules:

  • A namespace or package is a container for MyPackage::MySubPack::my_func() symbols.
  • A module, on the other hand, is a file that can contain any number of namespaces, or simply drop everything into the current namespace (although it shouldn’t).
  • It is possible to switch to other packages using the package statement. However, you then have to remember not to use them, because Perl will look for a file corresponding to that name.
  • A module can put itself in a completely different namespace than its designated module name. (e.g: a module loaded with use The Module; can declare all of its identifiers in the Completely Different Package namespace.)

Installing Perl Module :

  • Download a Perl module in the form tar.gz file. Use the following sequence to install any Perl Module:

tar xvfz module_name.tar.gz

cd module_name

Perl Makefile.pl

make

make install

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *