|
Helping ordinary people create extraordinary websites! |
Building Perl projects with MakeMakerBy Sean Dague2005-05-01
Your First MakeMaker Project (make) This sample MakeMaker project is inspired by postings on the perl5-porters mailing list by Colin McMillen concerning the Net::Ping modules. Ping is a very useful tool for determining whether or not a machine is accepting packets, but often you want to know more than that, like whether a Web server is actually responding on that machine. Also, in an era of ICMP attacks, many hosts have actually disabled returning standard ping packets, so it no longer is a useful indication of network connectivity. This project will be to build a program pingwww, which will look and act a lot like ping, but will actually be sending HTTP requests, and testing whether or not the Web server in question is functioning. This project will contain a core module, Net::Ping::HTTP, which will implement the guts of what is happening, and pingwww, a program that provides a user interface to Net::Ping::HTTP. Writing the Makefile.PL I will begin by writing the Makefile.PL. At first it has only the most basic entries in it. Makefile.PL for Net::Ping::HTTP, take 1 use ExtUtils::MakeMaker;This specifies that the name of the project is Net::Ping::HTTP, and that the $VERSION variable contained within lib/Net/Ping/HTTP.pm will be considered the authoritative version of the project. Now I have a Makefile.PL, and one other file in the project. It is now time to create the MANIFEST, so that the build process will work correctly. The first pass at a MANIFEST will look as follows: MANIFEST, take 1 MANIFESTRemember, every time new files are added to the project, an entry for each of them must also be added to the MANIFEST file.Net::Ping::HTTP The code for this module will be a simple layer on top of LWP::UserAgent, which is included in the libwww-perl package. Many flavors of Linux and UNIX include this library in their distribution by default. Also, if you have installed ActiveState Perl for Windows, you already have LWP::UserAgent. However, if you are unfortunate enough to be on an operating system that doesn't provide this module by default, you may still download it from CPAN (see Resources). The strategy for Net::Ping::HTTP is simple. The HTTP protocol supports a method HEAD, which is designed to return only HTTP headers, but no content. Net::Ping::HTTP will send an HTTP HEAD request for the root document of the Web server you are attempting to ping, and will return the status code that is returned by that Web server. In the case where the Web server is available, this status code should be 200 (though in rare cases it might be a 30x or 40x code as well). Here is a first pass at Net::Ping::HTTP, including the POD documentation for the module: Net::Ping::HTTP library package Net::Ping::HTTP;Net::Ping::HTTP uses both LWP::UserAgent, and HTTP::Request, and will not work without them. Instead of having a user install this module, then have it break because the dependencies aren't there, the module can specify which other modules it requires to run. These modules can be added as requirements to the Makefile.PL. Their existence will be verified before the master Makefile is generated. The format of this new variable for Makefile.PL is as follows: Makefile.PL prerequisites 'PREREQ_PM' => {
The code works with the version of LWP::UserAgent and HTTP::Request that I have installed on my workstation, so I will prereq those versions in my module. To determine those versions, I can run the following commands on the command line:
Finding module versions perl -MLWP::UserAgent -e 'print $LWP::UserAgent::VERSION' The -M flag from perl loads that Module, the -e flag runs the following string as the perl script. With the information collected, the Makefile.PL looks as follows: Makefile.PL take 2 use ExtUtils::MakeMaker;pingwww The core library for the project is not complete. For it to be useful, there must exist a program that exploits its functionality and provides a user interface. This program will be called pingwww and will be placed in the bin/ directory of the project tree. Two features of the ping command that would be nice to simulate in pingwww are the ability to specify the number of times ping sends packets out to the server before it exits, and a synchronous display of how long each request took to respond. To add these features, the program will need two additional modules that were not required for Net::Ping::HTTP. These modules are Getopt::Std, which parses command line options, and Time::HiRes, which provides microsecond resolution timing. Getopt::Std is part of the core Perl distribution, so there is no need to add it as a PREREQ_PM, however Time::HiRes is not (though it appears that it will be part of the standard library as of Perl 5.8). Time::HiRes is hence added to the PREREQ_PM variable in Makefile.PL. Here is the first pass at pingwww: pingwww program #!/usr/bin/perlBecause EXE_FILES includes pingwww, some interesting events happen during installation, which I will discuss later. The MANIFEST file must also contain an entry for the new file. The new MANIFEST file is shown below: Manifest, take 2 MANIFEST Tutorial Pages: » The Module that Makes Makefiles and Much More » Programming Products vs. Programs » Anatomy of a MakeMaker Project » Your First MakeMaker Project (make) » Building Test Cases (make test) » Installation (make install) » Distributing Your Code (make dist) » Conclusion » Resources First published by IBM DeveloperWorks
|
|