⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12_09.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 12.8. Preparing a Module for Distribution (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:41:37Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"><LINKREL="prev"HREF="ch12_08.htm"TITLE="12.7. Keeping Your Own Module Directory"><LINKREL="next"HREF="ch12_10.htm"TITLE="12.9. Speeding Module Loading with SelfLoader"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_08.htm"TITLE="12.7. Keeping Your Own Module Directory"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.7. Keeping Your Own Module Directory"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_10.htm"TITLE="12.9. Speeding Module Loading with SelfLoader"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.9. Speeding Module Loading with SelfLoader"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch12-pgfId-1000005721">12.8. Preparing a Module for Distribution</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1000005724">Problem<ACLASS="indexterm"NAME="ch12-idx-1000005723-0"></A></A></H3><PCLASS="para">You 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.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1000005727">Solution</A></H3><PCLASS="para">It's best to start with Perl's standard <EMCLASS="emphasis">h2xs</EM><ACLASS="indexterm"NAME="ch12-idx-1000005728-0"></A> tool. Let's say you want to make a Planets module or an Astronomy::Orbits module. You'd type:</P><PRECLASS="programlisting">% h2xs -XA -n Planets% h2xs -XA -n Astronomy::Orbits</PRE><PCLASS="para">These commands make subdirectories called <EMCLASS="emphasis">.  /Planets/</EM> and <EMCLASS="emphasis">.  /Astronomy/Orbits/</EM> respectively, where you will find all the components you need to get you started. The <BCLASS="emphasis.bold">-n</B> flag names the module you want to make, <BCLASS="emphasis.bold">-X</B> suppresses creation of XS (external subroutine) components, and <BCLASS="emphasis.bold">-A</B> means the module won't use the AutoLoader.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1000005734">Discussion</A></H3><PCLASS="para">Writing modules is easy &nbsp;-  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 <EMCLASS="emphasis">h2xs</EM> 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.</P><PCLASS="para"><EMCLASS="emphasis">h2xs</EM> is something of a misnomer because <ACLASS="indexterm"NAME="ch12-idx-1000005736-0"></A>XS is Perl's external subroutine interface for linking with C or C ++. But the <EMCLASS="emphasis">h2xs</EM> tool is also extremely convenient for preparing a distribution even when you aren't using the XS interface.</P><PCLASS="para">Let's look at one of the modules file that <EMCLASS="emphasis">h2xs</EM> has made. Because the module is to be called Astronomy::Orbits, the user will specify not <CODECLASS="literal">use</CODE> <CODECLASS="literal">Orbits</CODE> but rather <CODECLASS="literal">use</CODE> <CODECLASS="literal">Astronomy::Orbits</CODE>. Therefore an extra <EMCLASS="emphasis">Astronomy</EM> subdirectory is made, in which an <EMCLASS="emphasis">Orbits</EM> directory is placed. Here is the first and perhaps most important line of <EMCLASS="emphasis">Orbit.pm</EM>:</P><PRECLASS="programlisting">package Astronomy::Orbits;</PRE><PCLASS="para">This sets the package &nbsp;-  the default prefix &nbsp;-  on all global identifiers (variables, functions, filehandles, etc.) in the file. Therefore a variable like <CODECLASS="literal">@ISA</CODE> is really the global variable <CODECLASS="literal">@Astronomy::Orbits::ISA</CODE>.</P><PCLASS="para">As we said in the Introduction, you must not make the mistake of saying <CODECLASS="literal">package</CODE> <CODECLASS="literal">Orbits</CODE> because it's in the file <EMCLASS="emphasis">Orbits.pm</EM>. The <CODECLASS="literal">package</CODE> statement in the module must be exactly match the target of the <CODECLASS="literal">use</CODE> or <CODECLASS="literal">require</CODE> statement, which means the leading directory portion needs to be there and the characters' case must be the same. Furthermore, it must be installed in an <EMCLASS="emphasis">Astronomy</EM> subdirectory. The <EMCLASS="emphasis">h2xs</EM> command will set this all up properly, including the installation rule in the Makefile. But if you're doing this by hand, you must keep this in mind. See <ACLASS="xref"HREF="ch12_02.htm"TITLE="Defining a Module's Interface">Recipe 12.1</A> for that.</P><PCLASS="para">If you plan to use autoloading, described in <ACLASS="xref"HREF="ch12_11.htm"TITLE="Speeding Up Module Loading with Autoloader">Recipe 12.10</A>, omit the <BCLASS="emphasis.bold">-A</B> flag to <EMCLASS="emphasis">h2xs</EM>, which produces lines like this:</P><PRECLASS="programlisting">require Exporter;require AutoLoader;@ISA = qw(Exporter AutoLoader);</PRE><PCLASS="para">If your module is bilingual in Perl and C as described in <ACLASS="xref"HREF="ch12_16.htm"TITLE="Using h2xs to Make a Module with C Code">Recipe 12.15</A>, omit the <BCLASS="emphasis.bold">-X</B> flag to <EMCLASS="emphasis">h2xs</EM> to produce lines like this:</P><PRECLASS="programlisting">require Exporter;require DynaLoader;@ISA = qw(Exporter DynaLoader);</PRE><PCLASS="para">Following this is the Exporter's variables as explained in <ACLASS="xref"HREF="ch12_02.htm"TITLE="Defining a Module's Interface">Recipe 12.1</A>. If you're writing an object-oriented module as described in <ACLASS="xref"HREF="ch13_01.htm"TITLE="Classes, Objects, and Ties">Chapter 13</A>, you probably won't use the Exporter at all.</P><PCLASS="para">That's all there is for setup. Now, write your module code. When you're ready to ship it off, use the <CODECLASS="literal">make</CODE> <CODECLASS="literal">dist</CODE> directive from your shell to bundle it all up into a tar archive for easy distribution. (The name of the <EMCLASS="emphasis">make</EM> program may vary from system to system.)</P><PRECLASS="programlisting">% make dist</PRE><PCLASS="para">This will leave you with a file whose name is something like <EMCLASS="emphasis">Astronomy-Orbits-1.03.tar.Z</EM>.</P><PCLASS="para">To register as a <ACLASS="indexterm"NAME="ch12-idx-1000005769-0"></A>CPAN developer, check out <ACLASS="systemitem.url"HREF="http://www.perl.com/CPAN/modules/04pause.html">http://www.perl.com/CPAN/modules/04pause.html</A>.<ACLASS="indexterm"NAME="ch12-idx-1000005771-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005771-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1000005773">See Also</A></H3><PCLASS="para"><ACLASS="systemitem.url"HREF="http://www.perl.com/CPAN">http://www.perl.com/CPAN</A> to find a mirror near you and directions for submission; <ICLASS="filename">h2xs </I>(1); the documentation for the standard Exporter, AutoLoader, AutoSplit, and ExtUtils::MakeMaker modules, also found in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_08.htm"TITLE="12.7. Keeping Your Own Module Directory"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.7. Keeping Your Own Module Directory"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_10.htm"TITLE="12.9. Speeding Module Loading with SelfLoader"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.9. Speeding Module Loading with SelfLoader"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">12.7. Keeping Your Own Module Directory</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">12.9. Speeding Module Loading with SelfLoader</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -