A Perl package is a collection of code and a Perl module is a package defined in a file with the same name as that of the package name and having the .pm extension. A package lives in its own namespace. It means that two different modules may contain a function or a variable of the same name.
Declaring a Perl module
We use package to define a package in a module. A module name is same as that of the package name and has .pm extension. A module returns a true value to the Perl interpreter. This is explained after the following example. Let's declare a Perl module.
#File name is p.pm
use strict;
use warnings;
#Declaring package p
package p;
sub Hello{
print "Hello\n";
}
1;
Here, the name of the file is "p.pm" and the name of the package is p. Notice that 1; is written at the end of the code. This will return a true value to the interpreter as mentioned above. We can write anything which is true in Perl instead of 1.
Using a Perl module
To use a module, we use require or use functions. We use :: to access a function or a variable from a module. Let’s look at this using an example:
#p.pm is defined in parent directory
#Filename have .pl extension
use strict;
use warnings;
#using package p
use p;
#Function Hello of p
p::Hello();
Modules in different directory
If a module is present in some sub-directory, then also we use :: to tell the path of the module. For example, if a module 'b' is present in a sub-directory 'a', then we use use a::b; to load the module b. Let's see an example to understand this:
#p.pm is defined in directory ab which
#is in parent directory
use strict;
use warnings;
#using package p
use ab::p;
#Function Hello of p
p::Hello();
Using variable from modules
We can also use variables from different packages. But we need to declare them first before using them. We do this by use vars qw($scalar @array %hash) and we can also use our ($scalar @array %hash) with Perl v5.6.0 or higher versions. Let's use it in an example:
#This is p.pm
use strict;
use warnings;
package b;
our ($var_name);
sub Hello{
print "Hello $var_name\n";
}
1;
#p.pm is in parent directory
use strict;
use warnings;
#using package p
use p;
#using var_name from p
$p::var_name = "Sam";
#Function Hello of p
p::Hello();
BEGIN and END
BEGIN and END is used when we want to run some piece of code at the beginning and some at the end. The codes written within BEGIN{...} are executed at the beginning of the script and codes written within END{...} are executed at the end of the script. Let's see an example of this.
use strict;
use warnings;
BEGIN{
print "Starting...\n";
}
END{
print "END OF PROGRAM\n";
}
print "Hello World;\n";
Hello World;
END OF PROGRAM
Export and import of functions
We can make any function defined in a package for direct use by using Exporter. We just have to write:
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA=qw(Exporter);
@EXPORT=(function1, function2);
We have used Exporter module and then @EXPORT array to export the functions. Let's see an example:
#p.pm
package p;
use strict;
use warnings;
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA=qw(Exporter);
@EXPORT=("Hello");
sub Hello{
print "Hello\n";
}
use strict;
use warnings;
use p;
Hello();
Hello World;
END OF PROGRAM
As you can see that here we have directly used the function 'Hello'.