perlnewmod.pod
来自「MSYS在windows下模拟了一个类unix的终端」· POD 代码 · 共 283 行
POD
283 行
=head1 NAMEperlnewmod - preparing a new module for distribution=head1 DESCRIPTIONThis document gives you some suggestions about how to go about writingPerl modules, preparing them for distribution, and making them availablevia CPAN.One of the things that makes Perl really powerful is the fact that Perlhackers tend to want to share the solutions to problems they've faced,so you and I don't have to battle with the same problem again.The main way they do this is by abstracting the solution into a Perlmodule. If you don't know what one of these is, the rest of thisdocument isn't going to be much use to you. You're also missing out onan awful lot of useful code; consider having a look at L<perlmod>,L<perlmodlib> and L<perlmodinstall> before coming back here.When you've found that there isn't a module available for what you'retrying to do, and you've had to write the code yourself, considerpackaging up the solution into a module and uploading it to CPAN so thatothers can benefit.=head2 WarningWe're going to primarily concentrate on Perl-only modules here, ratherthan XS modules. XS modules serve a rather different purpose, andyou should consider different things before distributing them - thepopularity of the library you are gluing, the portability to otheroperating systems, and so on. However, the notes on preparing the Perlside of the module and packaging and distributing it will apply equallywell to an XS module as a pure-Perl one.=head2 What should I make into a module?You should make a module out of any code that you think is going to beuseful to others. Anything that's likely to fill a hole in the communallibrary and which someone else can slot directly into their program. Anypart of your code which you can isolate and extract and plug intosomething else is a likely candidate.Let's take an example. Suppose you're reading in data from a localformat into a hash-of-hashes in Perl, turning that into a tree, walkingthe tree and then piping each node to an Acme Transmogrifier Server.Now, quite a few people have the Acme Transmogrifier, and you've had towrite something to talk the protocol from scratch - you'd almostcertainly want to make that into a module. The level at which you pitchit is up to you: you might want protocol-level modules analogous toL<Net::SMTP|Net::SMTP> which then talk to higher level modules analogousto L<Mail::Send|Mail::Send>. The choice is yours, but you do want to geta module out for that server protocol.Nobody else on the planet is going to talk your local data format, so wecan ignore that. But what about the thing in the middle? Building treestructures from Perl variables and then traversing them is a nice,general problem, and if nobody's already written a module that doesthat, you might want to modularise that code too.So hopefully you've now got a few ideas about what's good to modularise.Let's now see how it's done.=head2 Step-by-step: Preparing the groundBefore we even start scraping out the code, there are a few things we'llwant to do in advance.=over 3=item Look aroundDig into a bunch of modules to see how they're written. I'd suggeststarting with L<Text::Tabs|Text::Tabs>, since it's in the standardlibrary and is nice and simple, and then looking at something likeL<Time::Zone|Time::Zone>, L<File::Copy|File::Copy> and then some of theC<Mail::*> modules if you're planning on writing object oriented code.These should give you an overall feel for how modules are laid out andwritten.=item Check it's newThere are a lot of modules on CPAN, and it's easy to miss one that'ssimilar to what you're planning on contributing. Have a good ploughthrough the modules list and the F<by-module> directories, and make sureyou're not the one reinventing the wheel!=item Discuss the needYou might love it. You might feel that everyone else needs it. But theremight not actually be any real demand for it out there. If you're unsureabout the demand you're module will have, consider sending out feelerson the C<comp.lang.perl.modules> newsgroup, or as a last resort, ask themodules list at C<modules@perl.org>. Remember that this is a closed listwith a very long turn-around time - be prepared to wait a good while fora response from them.=item Choose a namePerl modules included on CPAN have a naming hierarchy you should try tofit in with. See L<perlmodlib> for more details on how this works, andbrowse around CPAN and the modules list to get a feel of it. At the veryleast, remember this: modules should be title capitalised, (This::Thing)fit in with a category, and explain their purpose succinctly.=item Check againWhile you're doing that, make really sure you haven't missed a modulesimilar to the one you're about to write.When you've got your name sorted out and you're sure that your module iswanted and not currently available, it's time to start coding.=back=head2 Step-by-step: Making the module=over 3=item Start with F<h2xs>Originally a utility to convert C header files into XS modules,L<h2xs|h2xs> has become a useful utility for churning out skeletons forPerl-only modules as well. If you don't want to use theL<Autoloader|Autoloader> which splits up big modules into smallersubroutine-sized chunks, you'll say something like this: h2xs -AX -n Net::AcmeThe C<-A> omits the Autoloader code, C<-X> omits XS elements, and C<-n>specifies the name of the module.=item Use L<strict|strict> and L<warnings|warnings>A module's code has to be warning and strict-clean, since you can'tguarantee the conditions that it'll be used under. Besides, you wouldn'twant to distribute code that wasn't warning or strict-clean anyway,right?=item Use L<Carp|Carp>The L<Carp|Carp> module allows you to present your error messages fromthe caller's perspective; this gives you a way to signal a problem withthe caller and not your module. For instance, if you say this: warn "No hostname given";the user will see something like this: No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm line 123.which looks like your module is doing something wrong. Instead, you wantto put the blame on the user, and say this: No hostname given at bad_code, line 10.You do this by using L<Carp|Carp> and replacing your C<warn>s withC<carp>s. If you need to C<die>, say C<croak> instead. However, keepC<warn> and C<die> in place for your sanity checks - where it really isyour module at fault.=item Use L<Exporter|Exporter> - wisely!C<h2xs> provides stubs for L<Exporter|Exporter>, which gives you astandard way of exporting symbols and subroutines from your module intothe caller's namespace. For instance, saying C<use Net::Acme qw(&frob)>would import the C<frob> subroutine.The package variable C<@EXPORT> will determine which symbols will getexported when the caller simply says C<use Net::Acme> - you will hardlyever want to put anything in there. C<@EXPORT_OK>, on the other hand,specifies which symbols you're willing to export. If you do want toexport a bunch of symbols, use the C<%EXPORT_TAGS> and define a standardexport set - look at L<Exporter> for more details.=item Use L<plain old documentation|perlpod>The work isn't over until the paperwork is done, and you're going toneed to put in some time writing some documentation for your module.C<h2xs> will provide a stub for you to fill in; if you're not sure aboutthe format, look at L<perlpod> for an introduction. Provide a goodsynopsis of how your module is used in code, a description, and thennotes on the syntax and function of the individual subroutines ormethods. Use Perl comments for developer notes and POD for end-usernotes.=item Write testsYou're encouraged to create self-tests for your module to ensure it'sworking as intended on the myriad platforms Perl supports; if you uploadyour module to CPAN, a host of testers will build your module and sendyou the results of the tests. Again, C<h2xs> provides a test frameworkwhich you can extend - you should do something more than just checkingyour module will compile.=item Write the READMEIf you're uploading to CPAN, the automated gremlins will extract theREADME file and place that in your CPAN directory. It'll also appear inthe main F<by-module> and F<by-category> directories if you make it ontothe modules list. It's a good idea to put here what the module actuallydoes in detail, and the user-visible changes since the last release.=back=head2 Step-by-step: Distributing your module=over 3=item Get a CPAN user IDEvery developer publishing modules on CPAN needs a CPAN ID. See theinstructions at C<http://www.cpan.org/modules/04pause.html> (orequivalent on your nearest mirror) to find out how to do this.=item C<perl Makefile.PL; make test; make dist>Once again, C<h2xs> has done all the work for you. It produces thestandard C<Makefile.PL> you'll have seen when you downloaded andinstalls modules, and this produces a Makefile with a C<dist> target.Once you've ensured that your module passes its own tests - always agood thing to make sure - you can C<make dist>, and the Makefile willhopefully produce you a nice tarball of your module, ready for upload.=item Upload the tarballThe email you got when you received your CPAN ID will tell you how tolog in to PAUSE, the Perl Authors Upload SErver. From the menus there,you can upload your module to CPAN.=item Announce to the modules listOnce uploaded, it'll sit unnoticed in your author directory. If you wantit connected to the rest of the CPAN, you'll need to tell the moduleslist about it. The best way to do this is to email them a line in thestyle of the modules list, like this: Net::Acme bdpO Interface to Acme Frobnicator servers FOOBAR ^ ^^^^ ^ ^ | |||| Module description Your ID | |||| | |||\- Interface: (O)OP, (r)eferences, (h)ybrid, (f)unctions | ||| | ||\-- Language: (p)ure Perl, C(+)+, (h)ybrid, (C), (o)ther | || Module |\--- Support: (d)eveloper, (m)ailing list, (u)senet, (n)one Name | \---- Maturity: (i)dea, (c)onstructions, (a)lpha, (b)eta, (R)eleased, (M)ature, (S)tandardplus a description of the module and why you think it should beincluded. If you hear nothing back, that means your module willprobably appear on the modules list at the next update. Don't trysubscribing to C<modules@perl.org>; it's not another mailing list. Justhave patience.=item Announce to clpaIf you have a burning desire to tell the world about your release, postan announcement to the moderated C<comp.lang.perl.announce> newsgroup.=item Fix bugs!Once you start accumulating users, they'll send you bug reports. Ifyou're lucky, they'll even send you patches. Welcome to the joys ofmaintaining a software project...=back=head1 AUTHORSimon Cozens, C<simon@cpan.org>=head1 SEE ALSOL<perlmod>, L<perlmodlib>, L<perlmodinstall>, L<h2xs>, L<strict>,L<Carp>, L<Exporter>, L<perlpod>, L<Test>, L<ExtUtils::MakeMaker>,http://www.cpan.org/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?