📄 perlnewmod.1
字号:
Let's now see how it's done..Sh "Step-by-step: Preparing the ground".IX Subsection "Step-by-step: Preparing the ground"Before we even start scraping out the code, there are a few things we'llwant to do in advance..IP "Look around" 3.IX Item "Look around"Dig into a bunch of modules to see how they're written. I'd suggeststarting with Text::Tabs, since it's in the standardlibrary and is nice and simple, and then looking at something a littlemore complex like File::Copy. For object orientedcode, \f(CW\*(C`WWW::Mechanize\*(C'\fR or the \f(CW\*(C`Email::*\*(C'\fR modules provide some goodexamples..SpThese should give you an overall feel for how modules are laid out andwritten..IP "Check it's new" 3.IX Item "Check it's new"There are a lot of modules on \s-1CPAN\s0, and it's easy to miss one that'ssimilar to what you're planning on contributing. Have a good ploughthrough the <http://search.cpan.org> and make sure you're not the onereinventing the wheel!.IP "Discuss the need" 3.IX Item "Discuss the need"You 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 your module will have, consider sending out feelerson the \f(CW\*(C`comp.lang.perl.modules\*(C'\fR newsgroup, or as a last resort, ask themodules list at \f(CW\*(C`modules@perl.org\*(C'\fR. Remember that this is a closed listwith a very long turn-around time \- be prepared to wait a good while fora response from them..IP "Choose a name" 3.IX Item "Choose a name"Perl modules included on \s-1CPAN\s0 have a naming hierarchy you should try tofit in with. See perlmodlib for more details on how this works, andbrowse around \s-1CPAN\s0 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..IP "Check again" 3.IX Item "Check again"While you're doing that, make really sure you haven't missed a modulesimilar to the one you're about to write..SpWhen 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..Sh "Step-by-step: Making the module".IX Subsection "Step-by-step: Making the module".IP "Start with \fImodule-starter\fR or \fIh2xs\fR" 3.IX Item "Start with module-starter or h2xs"The \fImodule-starter\fR utility is distributed as part of theModule::Starter \s-1CPAN\s0 package. It creates a directorywith stubs of all the necessary files to start a new module, accordingto recent \*(L"best practice\*(R" for module development, and is invoked fromthe command line, thus:.Sp.Vb 2\& module\-starter \-\-module=Foo::Bar \e\& \-\-author="Your Name" \-\-email=yourname@cpan.org.Ve.SpIf you do not wish to install the Module::Starterpackage from \s-1CPAN\s0, \fIh2xs\fR is an older tool, originally intended for thedevelopment of \s-1XS\s0 modules, which comes packaged with the Perldistribution..SpA typical invocation of h2xs for a pure Perl module is:.Sp.Vb 1\& h2xs \-AX \-\-skip\-exporter \-\-use\-new\-tests \-n Foo::Bar.Ve.SpThe \f(CW\*(C`\-A\*(C'\fR omits the Autoloader code, \f(CW\*(C`\-X\*(C'\fR omits \s-1XS\s0 elements,\&\f(CW\*(C`\-\-skip\-exporter\*(C'\fR omits the Exporter code, \f(CW\*(C`\-\-use\-new\-tests\*(C'\fR sets up amodern testing environment, and \f(CW\*(C`\-n\*(C'\fR specifies the name of the module..IP "Use strict and warnings" 3.IX Item "Use strict and 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?.IP "Use Carp" 3.IX Item "Use Carp"The 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:.Sp.Vb 1\& warn "No hostname given";.Ve.Spthe user will see something like this:.Sp.Vb 2\& No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm\& line 123..Ve.Spwhich looks like your module is doing something wrong. Instead, you wantto put the blame on the user, and say this:.Sp.Vb 1\& No hostname given at bad_code, line 10..Ve.SpYou do this by using Carp and replacing your \f(CW\*(C`warn\*(C'\fRs with\&\f(CW\*(C`carp\*(C'\fRs. If you need to \f(CW\*(C`die\*(C'\fR, say \f(CW\*(C`croak\*(C'\fR instead. However, keep\&\f(CW\*(C`warn\*(C'\fR and \f(CW\*(C`die\*(C'\fR in place for your sanity checks \- where it really isyour module at fault..IP "Use Exporter \- wisely!" 3.IX Item "Use Exporter - wisely!"Exporter gives you a standard way of exporting symbols andsubroutines from your module into the caller's namespace. For instance,saying \f(CW\*(C`use Net::Acme qw(&frob)\*(C'\fR would import the \f(CW\*(C`frob\*(C'\fR subroutine..SpThe package variable \f(CW@EXPORT\fR will determine which symbols will getexported when the caller simply says \f(CW\*(C`use Net::Acme\*(C'\fR \- you will hardlyever want to put anything in there. \f(CW@EXPORT_OK\fR, on the other hand,specifies which symbols you're willing to export. If you do want toexport a bunch of symbols, use the \f(CW%EXPORT_TAGS\fR and define a standardexport set \- look at Exporter for more details..IP "Use plain old documentation" 3.IX Item "Use plain old documentation"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.\&\f(CW\*(C`module\-starter\*(C'\fR or \f(CW\*(C`h2xs\*(C'\fR will provide a stub for you to fill in; ifyou're not sure about the format, look at perlpod for anintroduction. Provide a good synopsis of how your module is used incode, a description, and then notes on the syntax and function of theindividual subroutines or methods. Use Perl comments for developer notesand \s-1POD\s0 for end-user notes..IP "Write tests" 3.IX Item "Write tests"You'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 \s-1CPAN\s0, a host of testers will build your module and sendyou the results of the tests. Again, \f(CW\*(C`module\-starter\*(C'\fR and \f(CW\*(C`h2xs\*(C'\fRprovide a test framework which you can extend \- you should do somethingmore than just checking your module will compile.Test::Simple and Test::More are goodplaces to start when writing a test suite..IP "Write the \s-1README\s0" 3.IX Item "Write the README"If you're uploading to \s-1CPAN\s0, the automated gremlins will extract the\&\s-1README\s0 file and place that in your \s-1CPAN\s0 directory. It'll also appear inthe main \fIby-module\fR and \fIby-category\fR 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..Sh "Step-by-step: Distributing your module".IX Subsection "Step-by-step: Distributing your module".IP "Get a \s-1CPAN\s0 user \s-1ID\s0" 3.IX Item "Get a CPAN user ID"Every developer publishing modules on \s-1CPAN\s0 needs a \s-1CPAN\s0 \s-1ID\s0. Visit\&\f(CW\*(C`http://pause.perl.org/\*(C'\fR, select \*(L"Request \s-1PAUSE\s0 Account\*(R", and wait foryour request to be approved by the \s-1PAUSE\s0 administrators..ie n .IP """perl Makefile.PL; make test; make dist""" 3.el .IP "\f(CWperl Makefile.PL; make test; make dist\fR" 3.IX Item "perl Makefile.PL; make test; make dist"Once again, \f(CW\*(C`module\-starter\*(C'\fR or \f(CW\*(C`h2xs\*(C'\fR has done all the work for you.They produce the standard \f(CW\*(C`Makefile.PL\*(C'\fR you see when you download andinstall modules, and this produces a Makefile with a \f(CW\*(C`dist\*(C'\fR target..SpOnce you've ensured that your module passes its own tests \- always agood thing to make sure \- you can \f(CW\*(C`make dist\*(C'\fR, and the Makefile willhopefully produce you a nice tarball of your module, ready for upload..IP "Upload the tarball" 3.IX Item "Upload the tarball"The email you got when you received your \s-1CPAN\s0 \s-1ID\s0 will tell you how tolog in to \s-1PAUSE\s0, the Perl Authors Upload SErver. From the menus there,you can upload your module to \s-1CPAN\s0..IP "Announce to the modules list" 3.IX Item "Announce to the modules list"Once uploaded, it'll sit unnoticed in your author directory. If you wantit connected to the rest of the \s-1CPAN\s0, you'll need to go to \*(L"RegisterNamespace\*(R" on \s-1PAUSE\s0. Once registered, your module will appear in theby-module and by-category listings on \s-1CPAN\s0..IP "Announce to clpa" 3.IX Item "Announce to clpa"If you have a burning desire to tell the world about your release, postan announcement to the moderated \f(CW\*(C`comp.lang.perl.announce\*(C'\fR newsgroup..IP "Fix bugs!" 3.IX 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....SH "AUTHOR".IX Header "AUTHOR"Simon Cozens, \f(CW\*(C`simon@cpan.org\*(C'\fR.PPUpdated by Kirrily \*(L"Skud\*(R" Robert, \f(CW\*(C`skud@cpan.org\*(C'\fR.SH "SEE ALSO".IX Header "SEE ALSO"perlmod, perlmodlib, perlmodinstall, h2xs, strict,Carp, Exporter, perlpod, Test::Simple, Test::MoreExtUtils::MakeMaker, Module::Build, Module::Starterhttp://www.cpan.org/ , Ken Williams' tutorial on building your ownmodule at http://mathforum.org/~ken/perl_modules.html
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -