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

📄 appb_04.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Extending Perl's Functionality (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#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="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appb_03.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="appb_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">B.4. Extending Perl's Functionality</h2><p>One of the most common pieces of good advice heard in the Perldiscussion forums is that you shouldn't reinvent the wheel.Other folks have written code that you can put to use. The mostfrequent way to add to what<a name="INDEX-1146" />Perl can do is by using a library ormodule. Many of these come with Perl, while others are available fromCPAN. Of course, you can even write your own libraries and modules.</p><a name="lperl3-APP-B-SECT-4.1" /><div class="sect2"><h3 class="sect2">B.4.1. Libraries</h3><p>Many programming languages offer support for<a name="INDEX-1147" />libraries much asPerl does. Libraries are collections of (mostly) subroutines for agiven purpose. In modern Perl, though, it's more common to usemodules than libraries.</p></div><a name="lperl3-APP-B-SECT-4.2" /><div class="sect2"><h3 class="sect2">B.4.2. Modules</h3><p><a name="INDEX-1148" />Amodule is a "smart library". A module will typicallyoffer a collection of subroutines that act as if they were built infunctions, for the most part. Modules are smart in that they keeptheir details in a separate package, only importing what you request.This keeps a module from stomping on your code's symbols.</p><p>Although many useful modules are written in pure Perl, others arewritten using a language like C. For example, the MD5 algorithm issort of like a high-powered checksum.<a href="#FOOTNOTE-401">[401]</a> It uses a lot of low-levelbit-twiddling that could be done in Perl, but hundreds of times moreslowly;<a href="#FOOTNOTE-402">[402]</a> it's an algorithm that wasdesigned to be efficiently implemented in C. So, the<tt class="literal">Digest::MD5</tt> module is made to use the compiled Ccode. When you use that module, it's as if your Perl had abuilt in function to calculate MD5 digests.</p><blockquote class="footnote"> <a name="FOOTNOTE-401" /><p>[401]It's notreally a checksum, but that's good enough for thisexplanation.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-402" /><p>[402]The module <tt class="literal">Digest::Perl::MD5</tt>is a pure Perl implementation of the MD5 algorithm.Although your mileage may vary, we found it to be about 280 timesslower than the <tt class="literal">Digest::MD5</tt> module on one sampledataset. Remember that many of the bit-twiddling operations in the Calgorithm compile down to a <em class="emphasis">single</em> machineinstruction; thus, entire lines of code can take a mere handful ofclock cycles to run. Perl is fast, but let's not beunrealistic. </p> </blockquote></div><a name="lperl3-APP-B-SECT-4.3" /><div class="sect2"><h3 class="sect2">B.4.3. Finding and Installing Modules</h3><p>Maybe your system already has the module you need. But how can youfind out which modules are installed? You can use the program<i class="command">inside</i>,<a name="INDEX-1149" /><a name="INDEX-1150" />which should be available for download from <a name="INDEX-1151" />CPAN in the directory<a href="http://www.cpan.org/authors/id/P/PH/PHOENIX/">http://www.cpan.org/authors/id/P/PH/PHOENIX/</a>.</p><p>If none of the modules already available on your system suits yourneeds, you can search for Perl modules on <a name="INDEX-1152" /> <a name="INDEX-1153" />CPAN at<a name="INDEX-1154" /><a href="http://search.cpan.org/">http://search.cpan.org/</a>. To install a moduleon your system, see the<tt class="literal">perlmodinstall</tt><a name="INDEX-1155" /> manpage.</p><p>When using a module, you'll generally put the required<tt class="literal">use</tt><a name="INDEX-1156" /> directives at the top of yourprogram. That makes it easy for someone who is installing yourprogram on a new system to see at a glance which modules it needs.</p></div><a name="lperl3-APP-B-SECT-4.4" /><div class="sect2"><h3 class="sect2">B.4.4. Writing Your Own Modules</h3><p>In the rare case that there's no module to do what you need, anadvanced programmer can write a new one, either in Perl or in anotherlanguage (often C). See the<tt class="literal">perlmod</tt><a name="INDEX-1157" /> <a name="INDEX-1158" /> and <tt class="literal">perlmodlib</tt>manpages for more information.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appb_03.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="appb_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">B.3. Packages</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">B.5. Some Important Modules</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><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 + -