Your SlideShare is downloading. ×
How to write  Perl Modules Some practical advice about how to build a perl module March 21, 2003 – Stanford Linear Acceler...
Agenda <ul><li>What a Perl Module is </li></ul><ul><li>Why it is convenient to use Perl Modules </li></ul><ul><li>How to m...
What a Perl Module is <ul><li>A Perl Module is a self-contained piece of [Perl] code that can be used by a Perl program (o...
Why it is convenient to use PMs <ul><li>Installation is straightforward: perl Makefile.PL; make; make install </li></ul><u...
Before making a PM <ul><li>Chose an appropriate name for the PM </li></ul><ul><li>Each Perl Module has an unique name. Per...
Let’s start to make a Perl Module <ul><li>Create the Perl Module Tree: </li></ul><ul><ul><ul><li>-A omits the Autoloader c...
What h2xs does for the developer <ul><li>Creates subdirs: ./IEPM/PingER/ </li></ul><ul><li>Creates the following files and...
Some advice on writing the code of a PM <ul><li>A PM can use other PMs </li></ul><ul><li>use strict;  (i.e. $IEPM::PingER:...
How to use a PM in a Perl program <ul><li>Another way is to use @EXPORT_OK in the Perl Module: </li></ul><ul><li>@EXPORT_O...
Use Carp in Perl Modules <ul><li>The Carp module allows to present error messages from the caller's perspective  </li></ul...
Example: the beginning of IEPM::PingER <ul><li>package IEPM::PingER; </li></ul><ul><li>use 5.008; </li></ul><ul><li>use st...
Example: the subroutine ping_it <ul><li>PingER.PM </li></ul><ul><li>sub  ping_it  { </li></ul><ul><li>my  ($ping_cmd,$dest...
Preparing the package for shipping <ul><li>Prepare the package for shipping is straightforward: </li></ul><ul><li>$ perl M...
References <ul><li>Perl Module Mechanics http:// world.std.com/~swmcd/steven/perl/module_mechanics.html </li></ul><ul><li>...
Upcoming SlideShare
Loading in...5
×

Perl Modules

5,400

Published on

Published in: Technology, Business
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total Views
5,400
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide
  • Transcript of "Perl Modules"

    1. 1. How to write Perl Modules Some practical advice about how to build a perl module March 21, 2003 – Stanford Linear Accelerator Center Fabrizio Coccetti
    2. 2. Agenda <ul><li>What a Perl Module is </li></ul><ul><li>Why it is convenient to use Perl Modules </li></ul><ul><li>How to make Perl Modules </li></ul><ul><li>An example of a Perl Module </li></ul><ul><li>Preparing the package for shipping </li></ul>Perl Module are easy to make and handy to use
    3. 3. What a Perl Module is <ul><li>A Perl Module is a self-contained piece of [Perl] code that can be used by a Perl program (or by other Perl modules) </li></ul><ul><li>It is conceptually similar to: </li></ul><ul><ul><li>a C link library </li></ul></ul><ul><ul><li>a C++/Java class </li></ul></ul>
    4. 4. Why it is convenient to use PMs <ul><li>Installation is straightforward: perl Makefile.PL; make; make install </li></ul><ul><ul><ul><li>The Perl interpreter has a list of directories in which it searches for modules (global array @INC) </li></ul></ul></ul><ul><li>Installation is platform independent (Solaris, Linux, Windows, …) </li></ul><ul><li>Easy to use in Perl Programs: use IPEM::PingER </li></ul><ul><li>Easy to update both for the maintainers of a Perl Module and system administrators at remote sites </li></ul>Time saving technique !!
    5. 5. Before making a PM <ul><li>Chose an appropriate name for the PM </li></ul><ul><li>Each Perl Module has an unique name. Perl provides a hierarchal name space for modules, similar to the name space for Java classes. </li></ul><ul><li>Components of a module name are separated by double colons “::”. i.e. IEPM::PingER </li></ul>
    6. 6. Let’s start to make a Perl Module <ul><li>Create the Perl Module Tree: </li></ul><ul><ul><ul><li>-A omits the Autoloader code (best used by modules that define a large number of infrequently used subroutines) </li></ul></ul></ul><ul><ul><ul><li>-X omits XS elements (eXternal Subroutine, where eXternal means external to Perl, i.e. C) </li></ul></ul></ul><ul><ul><ul><li>-n specifies the name of the module </li></ul></ul></ul>$ h2xs -AX -n IEPM::PingER
    7. 7. What h2xs does for the developer <ul><li>Creates subdirs: ./IEPM/PingER/ </li></ul><ul><li>Creates the following files and the t/ dir: </li></ul><ul><ul><ul><li>Changes </li></ul></ul></ul><ul><ul><ul><li>Makefile.PL </li></ul></ul></ul><ul><ul><ul><li>MANIFEST (contains the list of all files in the package ) </li></ul></ul></ul><ul><ul><ul><li>README </li></ul></ul></ul><ul><ul><ul><li>t/ (test files) </li></ul></ul></ul><ul><ul><ul><li>PingER.pm (perl code goes here) </li></ul></ul></ul>
    8. 8. Some advice on writing the code of a PM <ul><li>A PM can use other PMs </li></ul><ul><li>use strict; (i.e. $IEPM::PingER::var) </li></ul><ul><li>use 5.6.1; </li></ul><ul><li>$VERSION=’1.03’; </li></ul><ul><li>@EXPORT = qw(ping_it); </li></ul><ul><li>sub ping_it {… … …} </li></ul><ul><li>1; </li></ul><ul><li>__END__ </li></ul>
    9. 9. How to use a PM in a Perl program <ul><li>Another way is to use @EXPORT_OK in the Perl Module: </li></ul><ul><li>@EXPORT_OK = qw(ping_it log_it); </li></ul><ul><li>And in the Perl Program </li></ul><ul><li>use IEPM::PingER qw(log_it) </li></ul>use IEPM::PingER; $answer = ping_it (host.domain.edu);
    10. 10. Use Carp in Perl Modules <ul><li>The Carp module allows to present error messages from the caller's perspective </li></ul>warn &quot;No hostname given&quot;; No hostname given at /usr/local/lib/perl5/site_perl/5.6/IEPM/PingER.pm line 143. carp &quot;No hostname given&quot;; No hostname given at perl_script.pl line 5. <ul><li>Recipe: </li></ul>use Carp; warn carp die croak
    11. 11. Example: the beginning of IEPM::PingER <ul><li>package IEPM::PingER; </li></ul><ul><li>use 5.008; </li></ul><ul><li>use strict; </li></ul><ul><li>use warnings; </li></ul><ul><li>use Carp; </li></ul><ul><li>require Exporter; </li></ul><ul><li>our @ISA = qw(Exporter); </li></ul><ul><li>use Time::CTime; </li></ul><ul><li>use DB_File; </li></ul><ul><li>our @EXPORT = qw(log_it ping_it); </li></ul><ul><li>our $VERSION = '1.03'; </li></ul>
    12. 12. Example: the subroutine ping_it <ul><li>PingER.PM </li></ul><ul><li>sub ping_it { </li></ul><ul><li>my ($ping_cmd,$dest_name,$dest_ip, $ping_interval, $ping_size, $ping_count) = @_ ; </li></ul><ul><li>… … … … </li></ul><ul><li>return ($time, $packets_sent, $packets_rcvd, $min, $avg, $max, @seqno, @pingtimes) ; </li></ul><ul><li>} </li></ul><ul><li>timeping.pl </li></ul><ul><li>use IEPM::PingER; </li></ul><ul><li>($time, $packets_sent, $packets_rcvd, $min, $avg, $max, $seqno, $pingtimes) = ping_it ($ping_cmd,$dest_name,$dest_ip, $ping_interval, 1000, $ping_count); </li></ul>
    13. 13. Preparing the package for shipping <ul><li>Prepare the package for shipping is straightforward: </li></ul><ul><li>$ perl Makefile.PL; make; make dist; </li></ul><ul><li>The commands above create the compressed archive: IEPM-PingER-1.03.tar.gz </li></ul>
    14. 14. References <ul><li>Perl Module Mechanics http:// world.std.com/~swmcd/steven/perl/module_mechanics.html </li></ul><ul><li>The Perl Module Library http://search.cpan.org/author/JHI/perl-5.8.0/pod/perlmodlib.pod </li></ul><ul><li>Perl Modules http://search.cpan.org/author/JHI/perl-5.8.0/pod/perlmod.pod </li></ul><ul><li>The Perl 5 Modules List http://www.cpan.org/modules/00modlist.long.html </li></ul><ul><li>Practical Perl Programming http://www.cs.cf.ac.uk/Dave/PERL/node131.html </li></ul><ul><li>Perlnewmod - preparing a new module for distribution http://www.perlpod.com/5.6.1/perlnewmod.html </li></ul>

    ×