12.8. Preparing a Module for DistributionProblemYou want to prepare your module in standard distribution format so you can easily send your module to a friend. Better yet, you plan to contribute your module to CPAN so everyone can use it. SolutionIt's best to start with Perl's standard h2xs tool. Let's say you want to make a Planets module or an Astronomy::Orbits module. You'd type: % h2xs -XA -n Planets % h2xs -XA -n Astronomy::Orbits These commands make subdirectories called . /Planets/ and . /Astronomy/Orbits/ respectively, where you will find all the components you need to get you started. The -n flag names the module you want to make, -X suppresses creation of XS (external subroutine) components, and -A means the module won't use the AutoLoader. DiscussionWriting modules is easy - once you know how. Writing a proper module is like filling out a legal contract: it's full of places to initial, sign, and date exactly right. If you miss any, it's not valid. Instead of hiring a contract lawyer, you can get a quick start on writing modules using the h2xs program. This tool gives you a skeletal module file with all the right parts filled in, and it also gives you the other files needed to correctly install your module and its documentation or to bundle it up for inclusion in CPAN or sending off to a friend. h2xs is something of a misnomer because XS is Perl's external subroutine interface for linking with C or C ++. But the h2xs tool is also extremely convenient for preparing a distribution even when you aren't using the XS interface.
Let's look at one of the modules file that
h2xs
has made. Because the module is to be called Astronomy::Orbits, the user will specify not package Astronomy::Orbits;
This sets the package - the default prefix - on all global identifiers (variables, functions, filehandles, etc.) in the file. Therefore a variable like
As we said in the Introduction, you must not make the mistake of saying If you plan to use autoloading, described in Recipe 12.10 , omit the -A flag to h2xs , which produces lines like this: require Exporter; require AutoLoader; @ISA = qw(Exporter AutoLoader); If your module is bilingual in Perl and C as described in Recipe 12.15 , omit the -X flag to h2xs to produce lines like this: require Exporter; require DynaLoader; @ISA = qw(Exporter DynaLoader); Following this is the Exporter's variables as explained in Recipe 12.1 . If you're writing an object-oriented module as described in Chapter 13 , you probably won't use the Exporter at all.
That's all there is for setup. Now, write your module code. When you're ready to ship it off, use the % make dist This will leave you with a file whose name is something like Astronomy-Orbits-1.03.tar.Z . To register as a CPAN developer, check out http://www.perl.com/CPAN/modules/04pause.html . See Alsohttp://www.perl.com/CPAN to find a mirror near you and directions for submission; h2xs (1); the documentation for the standard Exporter, AutoLoader, AutoSplit, and ExtUtils::MakeMaker modules, also found in Chapter 7 of Programming Perl |
|