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

📄 ch30_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>A Tour of the Perl Library (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="A Tour of the Perl Library"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch30_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch30_01.htm">Chapter 30: The Standard Perl Library</a></td><td align="right" valign="top" width="172"><a href="ch31_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">30.2. A Tour of the Perl Library</h2><p>You'll save an enormous amount of time if you make the effort tofamiliarize yourself with the standard Perl library, because there's noreason to reinvent those particular wheels.  You should be aware,however, that this collection contains a wide range of material.Although some libraries may be extremely helpful, others might becompletely irrelevant to your needs.  For example, if you're onlywriting in 100% pure Perl, those modules that support the dynamicloading of C and C++ extensions aren't going to help you much.</p><p>Perl expects to find library modules somewhere in its library"include" path, <tt class="literal">@INC</tt>. This array specifiesthe ordered list of directories Perl searcheswhen you load in some library code using the keywords <tt class="literal">do</tt>,<tt class="literal">require</tt>, or <tt class="literal">use</tt>.  You can easily list out those directoriesby calling Perl with the <span class="option">-V</span> switch for Very Verbose Version information,or with this simple code:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -le "print foreach @INC"</b></tt>/usr/libdata/perl5/sparc-openbsd/5.00503/usr/local/libdata/perl5/sparc-openbsd/5.00503/usr/libdata/perl5/usr/local/libdata/perl5/usr/local/libdata/perl5/site_perl/sparc-openbsd/usr/libdata/perl5/site_perl/sparc-openbsd/usr/local/libdata/perl5/site_perl/usr/libdata/perl5/site_perl.</pre></blockquote>That's only one sample of possible output.  Every installation of Perluses its own paths.  The important thing is that, although contentswill vary depending upon your vendor's and your site's installationpolicy, you can rely upon all standard libraries being installed withPerl.  If you want to find out where a file was actually loaded from,consult the <tt class="literal">%INC</tt> variable.  For a module file, youcan find exactly where Perl is getting it from with this command:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perldoc -l <em class="replaceable">MODULE</em></b></tt></pre></blockquote>If you look through the directories in <tt class="literal">@INC</tt> andtheir subdirectories, you'll find several different kinds of filesinstalled.  Most have names ending in <em class="emphasis">.pm</em>, butsome end in <em class="emphasis">.pl</em>, <em class="emphasis">.ph</em>,<em class="emphasis">.al</em>, or <em class="emphasis">.so</em>.  The ones thatmost interest you are the first set, because a suffix of<em class="emphasis">.pm</em> indicates that the file is a proper Perlmodule.  More on those in a minute.</p><p>The few files you see there ending in <em class="emphasis">.pl</em> arethose old Perl libraries we mentioned earlier.  They are included forcompatibility with ancient releases of Perl from the 80s and early90s.  Because of this, Perl code that worked back in, say, 1990,should continue to behave properly without any fuss even if you have amodern version of Perl installed.  When writing new code that makesuse of the standard Perl library, you should always elect to use the<em class="emphasis">.pm</em> version over any <em class="emphasis">.pl</em>,where possible.  That's because modules don't pollute your namespacethe way many of the old <em class="emphasis">.pl</em> files do.</p><p>One note on the use of the <em class="emphasis">.pl</em> extension: itmeans Perl library, not Perl program.  Although<em class="emphasis">.pl</em> is sometimes used to identify Perl programson web servers that need to distinguish executable programs fromstatic content in the same directory, we suggest that you use a suffixof <em class="emphasis">.plx</em> instead to indicate an executable Perlprogram.  (Similar advice holds for operating systems that chooseinterpreters based on filename extensions.)</p><p>Files with extensions of <em class="emphasis">.al</em> are small pieces oflarger modules will be automatically loaded when you use theirparent <em class="emphasis">.pm</em> file.  If you build your module layoutusing the standard <em class="emphasis">h2xs</em>tool that comes with Perl (and if you haven't used Perl's <span class="option">-A</span>flag), the<tt class="literal">make install</tt> procedure will use the<tt class="literal">AutoLoader</tt> module to create theselittle <em class="emphasis">.al</em> files for you.</p><p>The <em class="emphasis">.ph</em> files were made by the standard<em class="emphasis">h2ph</em> program, a somewhat aging but stilloccasionally necessary tool that does its best to translate Cpreprocessor directives into Perl.  The resulting<em class="emphasis">.ph</em> files contain constants sometimes needed bylow-level functions like <tt class="literal">ioctl</tt>,<tt class="literal">fcntl</tt>, or <tt class="literal">syscall</tt>.  (Nowadaysmost of these values are more conveniently and portably available instandard modules such as the <tt class="literal">POSIX</tt>,<tt class="literal">Errno</tt>, <tt class="literal">Fcntl</tt>, or<tt class="literal">Socket</tt> modules.)  See<em class="emphasis">perlinstall</em> for how to install these optional butsometimes important components.</p><p>One last file extension you might encounter while poking around is<em class="emphasis">.so</em> (or whatever your system uses for sharedlibraries).  These <em class="emphasis">.so</em> files areplatform-dependent portions of extension modules.  Originally writtenin C or C++, these modules have been compiled into dynamicallyrelocatable object code.  The end user doesn't need to be aware oftheir existence, however, because the module interface hides them.When the user code says <tt class="literal">require Module</tt> or<tt class="literal">use Module</tt>, Perl loads<em class="emphasis">Module.pm</em> and executes it, which lets the modulepull in any other necessary pieces, such as<em class="emphasis">Module.so</em> or any autoloaded<em class="emphasis">.al</em> components.  In fact, the module could loadanything it jolly well pleases, including 582 other modules.  It coulddownload all of CPAN if it felt like it, and maybe the last two yearsof <em class="emphasis">freshmeat.net</em> archives.</p><p>A module is not just a static chunk of code in Perl.  It's an activeagent that figures out how to implement an interface on your behalf.It may follow all the standard conventions, or it may not.  It's allowed todo anything to warp the meaning of the rest of your program, up to andincluding translating the rest of your program into SPITBOL.  This sortof chicanery is considered perfectly fair as long as it's welldocumented.  When you use such a Perl module, you're agreeing to <em class="emphasis">its</em> contract, not a standard contract written by Perl.</p><p>So you'd best read the fine print.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch30_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch31_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">30.1. Library Science</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">31. Pragmatic Modules</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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