When there is a need of creating poratable and installable PERL module, then there is standard way of creating a Perl Module Tree. This is done using
h2xs utility that comes alongwith PERL. The syntax is as follows:
h2xs -AXn Module_Name
where :
- -A omits the Autoloader code (best used by modules that define a large number of infrequently used subroutines)
- -X omits XS elements (eXternal Subroutine, where eXternal means external to Perl, i.e. C)
- -n specifies the name of the module
Step 1:
Let's create a module 'Global.pm' triggering the following commands:
h2xs -XAn Global
The output of the command will produce the following files/results :
Writing Global/lib/Global.pm (#contains actual source code)
Writing Global/Makefile.PL
Writing Global/README
Writing Global/t/Global.t (#contains test files)
Writing Global/Changes
Writing Global/MANIFEST (#contains the list of all files in the package)
Step 2:
Now change into the Global/lib subdirectory :
cd Global/lib
Step 3:
Open up the file called Global.pm, using your favorite text editor(Say, Vi) :
vi Global.pm
Step 4:
Find the line 'our EXPORT_TAGS' which should like below:
our %EXPORT_TAGS = ( 'all' => [ qw( ) ] )
Step 5:
Now add name of all sub-routines (Say, printMSG and showDate) to the above line as follows:
our %EXPORT_TAGS = ( 'all' => [ qw(printMSG showDate) ] );
where printMSG and showDate are name of subroutines
Step 6:
Now find the line 'our $VERSION = '0.01';' and add definition of all the sub-routine under the line:
sub printMSG
{
use Carp;
my @msg=@_;
if (!defined($msg[0]))
{
carp "Nothing has been passed to printMSG\n";
}
print @msg;
return wantarray ? @msg : $msg[0];
}
sub showDate
{
return localtime(time);
}
Step 7:
Now find the line '=head1 SYNOPSIS' and underneath it add your own description:
use Global;
printMSG($msg); # prints user-defined message
showDate(); #shows date
Step 8:
Now save the file (Global.pm) and at the terminal, issue the following command:
perl Makefile.PL
The following messages will be observed if everything is ok :Checking if your kit is complete...
Looks good
Writing Makefile for Global
Step 9:
Now issue the following command:
make
if everthing is fine, following messages will appear:cp lib/Global.pm blib/lib/Global.pm
Manifying blib/man3/Global.3
Step 10:
Run the below command to test:
make test
if everything goes successful, you will see the below output:PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/Global....ok
All tests successful.
Files=1, Tests=1, 0 wallclock secs ( 0.01 cusr + 0.01 csys = 0.02 CPU)
Target "test" is up to date.
step 11 (Installing Perl Module):
Fisrst change to root, or super user and issue the following sequence of commands to install any Perl module:
perl Makefile.PL or
perl Makefile.PL PREFIX=/my/perl/directory (#if you want to install in /my/perl/directory)
make
make install
You may see something like this:
Installing /usr/lib/perl5/site_perl/..../Global.pm
Installing /usr/share/man/man3/Global.3pm
Writing /usr/lib/perl5/site_perl/........./Global/.packlist
Appending installation info to /usr/lib/perl5/......./perllocal.pod
Step 12 (Testing):
To test the module, you may use as follows:
#!/usr/bin/perl
use Global;
use strict;
use warnings;
printMSG("Hi Rabindra");
showDate();