appb_05.htm

来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 328 行 · 第 1/2 页

HTM
328
字号
<html><head><title>Some Important Modules (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_04.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_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">B.5. Some Important Modules</h2><p>We describe some of the most important features<a href="#FOOTNOTE-403">[403]</a> of the most important modules<a href="#FOOTNOTE-404">[404]</a> in this section. These modules that we discusshere should generally be found on every machine that has Perl, exceptwhere mentioned. You can always get the latest ones from CPAN.</p><blockquote class="footnote"><a name="FOOTNOTE-403" /><p>[403]We're including here merely the most important featuresof each module; see the module's own documentation to learnmore.</p> </blockquote><blockquote class="footnote"><a name="FOOTNOTE-404" /><p>[404]To be sure, there are other important modules whose use is toocomplex for most readers of this book, typically because using themodule requires understanding Perl's references or objects.</p> </blockquote><a name="lperl3-APP-B-SECT-5.1" /><div class="sect2"><h3 class="sect2">B.5.1. The CGI and CGI_Lite Modules</h3><p>Many people use Perl to write programs that a web server will run,generally called <em class="emphasis">CGI program</em>s. The<tt class="literal">CGI</tt><a name="INDEX-1159" /> module comes with Perl, while the<tt class="literal">CGI_Lite</tt><a name="INDEX-1160" /> module is available separately fromCPAN. See <a href="appb_16.htm#lperl3-APP-B-SECT-16">Section B.16, "The Common Gateway Interface (CGI)"</a> later in thisappendix.</p></div><a name="lperl3-APP-B-SECT-5.2" /><div class="sect2"><h3 class="sect2">B.5.2. The Cwd Module</h3><p>Sometimes you need to know what the current working directory'sname is. (Well, you could often use"<tt class="literal">.</tt>", but maybe you need to save thename so that you can change back to this directory later.) The<tt class="literal">Cwd</tt><a name="INDEX-1161" /> module, which comes with Perl, providesthe <tt class="literal">cwd</tt><a name="INDEX-1162" /> function, which you can use todetermine the <a name="INDEX-1163" /> <a name="INDEX-1164" />current working directory.</p><blockquote><pre class="code">use Cwd;my $directory = cwd;</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.3" /><div class="sect2"><h3 class="sect2">B.5.3. The Fatal Module</h3><p>If you get tired of writing "<tt class="literal">or die</tt>"after every invocation of <tt class="literal">open</tt> or<tt class="literal">chdir</tt>, then maybe the<tt class="literal">Fatal</tt><a name="INDEX-1165" /> module is for you. Just tell it whichfunctions to work with, and those will be automatically checked forfailure, as if you'd written "<tt class="literal">ordie</tt>" and a suitable message after each one. Thiswon't affect such calls in someone else's package (thatis, code contained within a module you're using, for example),so don't use this to fix up poorly written code. It'sjust a timesaver, mostly for simple programs in which you don'tneed direct control over the error message itself. For example:</p><blockquote><pre class="code">use Fatal qw/ open chdir /;chdir '/home/merlyn';  # "or die" is now supplied automatically</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.4" /><div class="sect2"><h3 class="sect2">B.5.4. The File::Basename Module</h3><p>We covered this<a name="INDEX-1166" /><a name="INDEX-1167" />module in <a href="ch13_01.htm">Chapter 13, "Manipulating Files and Directories"</a>. It's primary uses are to portably pullthe basename or directory name from a full filename:</p><blockquote><pre class="code">use File::Basename;for (@ARGV) {  my $basename = basename $_;  my $dirname = dirname $_;  print "That's file $basename in directory $dirname.\n";}</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.5" /><div class="sect2"><h3 class="sect2">B.5.5. The File::Copy Module</h3><p>When you need to copy or move files, the<tt class="literal">File::Copy</tt><a name="INDEX-1168" /><a name="INDEX-1169" /> module is for you. (It'soften tempting to simply call a system program to do these things,but that's not portable.) This module provides the functions<tt class="literal">move</tt> and <tt class="literal">copy</tt>, which may beused much as the corresponding system programs would be used:</p><blockquote><pre class="code">use File::Copy;copy("source", "destination")  or die "Can't copy 'source' to 'destination': $!";</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.6" /><div class="sect2"><h3 class="sect2">B.5.6. The File::Spec Module</h3><p>When you need to manipulate a<a name="INDEX-1170" />filename (more formally calleda"<a name="INDEX-1171" />filespecification"), it's generally more portable andreliable to use the<tt class="literal">File::Spec</tt><a name="INDEX-1172" /> module than to do the work yourself fromPerl. For example, you can use the <tt class="literal">catfile</tt>function to put together a directory name and a filename to produce along filename (as we saw in <a href="ch13_01.htm">Chapter 13, "Manipulating Files and Directories"</a>), but youdon't have to know whether the system your program is runningon uses a forward slash or some other character to separate those. Oryou could use the <tt class="literal">curdir</tt> function to get the nameof the current directory ("<tt class="literal">.</tt>", on Unixsystems).</p><p>The <tt class="literal">File::Spec</tt> module is object-oriented, but youdon't need to understand objects to use it. Just call eachfunction ("method", really) by using<tt class="literal">File::Spec</tt> and a small arrow before thefunction's name, like this:</p><blockquote><pre class="code">use File::Spec;my $current_directory = File::Spec-&gt;curdir;opendir DOT, $current_directory  or die "Can't open current directory '$current_directory': $!";</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.7" /><div class="sect2"><h3 class="sect2">B.5.7. The Image::Size Module</h3><p>When you have an <a name="INDEX-1173" /><a name="INDEX-1174" />image file, you'll often wantto know what its height and width are. (This is handy for makingprograms that write HTML, if you wish for an IMG tag to indicate theimage's dimensions.) The<tt class="literal">Image::Size</tt><a name="INDEX-1175" /> module, which is available from CPAN,understands the common GIF, JFIF (JPEG), and PNG image types, andsome others. For example:</p><blockquote><pre class="code">use Image::Size;# Get the size of fred.pngmy($fred_height, $fred_width) = imgsize("fred.png");

⌨️ 快捷键说明

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