📄 ch13_08.htm
字号:
<p>Simply give <tt class="literal">File::Basename</tt>, in your<tt class="literal">use</tt> declaration, an <em class="firstterm">importlist</em><a name="INDEX-938" /> showing exactly which function names itshould give you, and it'll supply those and no others. Here,we'll get nothing but <tt class="literal">basename</tt>:</p><blockquote><pre class="code">use File::Basename qw/ basename /;</pre></blockquote><p>And here, we'll ask for no new functions at all:</p><blockquote><pre class="code">use File::Basename qw/ /;</pre></blockquote><p>Why would you want to do that? Well, this directive tells Perl toload up <tt class="literal">File::Basename</tt>, just as before, but not to<em class="firstterm">import</em> any function names. Importing lets ususe the short, simple function names like <tt class="literal">basename</tt>and <tt class="literal">dirname</tt>. But even if we don't importthose names, we can still use the functions. When they're notimported, though, we have to call them by their full names:</p><blockquote><pre class="code">use File::Basename qw/ /; # import no function namesmy $betty = &dirname($wilma); # uses our own subroutine &dirname (not shown)my $name = "/usr/local/bin/perl";my $dirname = File::Basename::dirname $name; # dirname from the module</pre></blockquote><p>As you see, the full name of the <tt class="literal">dirname</tt> functionfrom the module is <tt class="literal">File::Basename::dirname</tt>. We canalways use the function's full name (once we've loadedthe module) whether we've imported the short name<tt class="literal">dirname</tt> or not.</p><p>Most of the time, you'll want to use a module's defaultimport list. But you can always override that with a list of yourown, if you want to leave out some of the default items. Anotherreason to supply your own list would be if you wanted to import somefunction not on the default list, since most modules include some(infrequently needed) functions that are not on the default importlist.</p><p>As you'd guess, some modules will, by default, import moresymbols than others. Each module's documentation should make itclear which symbols it imports, if any, but you are always free tooverride the default import list by specifying one of your own, justas we did with <tt class="literal">File::Basename</tt>. Supplying an emptylist imports no<a name="INDEX-939" /> symbols.<a name="INDEX-940" /> <a name="INDEX-941" /></p></div><a name="lperl3-CHP-13-SECT-8.3" /><div class="sect2"><h3 class="sect2">13.8.3. The File::Spec Module</h3><p><a name="INDEX-942" />Now you can find out a file'sbasename. That's useful, but you'll often want to putthat together with a directory name to get a full filename. Forexample, here we want to take a filename like<em class="filename">/home/rootbeer/ice-2.1.txt</em> and add a prefix tothe basename:</p><blockquote><pre class="code">use File::Basename;print "Please enter a filename: ";chomp(my $old_name = <STDIN>);my $dirname = dirname $old_name;my $basename = basename $old_name;$basename =~ s/^/not/; # Add a prefix to the basenamemy $new_name = "$dirname/$basename";rename($old_name, $new_name) or warn "Can't rename '$old_name' to '$new_name': $!";</pre></blockquote><p>Do you see the problem here? Once again, we're making theassumption that filenames will follow the Unix conventions and use aforward slash between the directory name and the basename.Fortunately, Perl comes with a module to help with this problem, too.</p><p>The <tt class="literal">File::Spec</tt> module is used for manipulating<em class="firstterm">filespecifications</em><a name="INDEX-943" />,which are the names of files, directories, and the other things thatare stored on filesystems. Like <tt class="literal">File::Basename</tt>, itunderstands what kind of system it's running on, and it choosesthe right set of rules every time. But unlike<tt class="literal">File::Basename</tt>, <tt class="literal">File::Spec</tt> isan object-oriented (often abbreviated "OO") module.</p><p>If you've never caught the fever of OO, don't let thatbother you. If you understand objects, that's great; you canuse this OO module. If you don't understand objects,that's okay, too. You just type the symbols as we show you, andit works just as if you knew what you were doing.</p><p>In this case, we learn from reading the documentation for<tt class="literal">File::Spec</tt> that we want to use a<em class="firstterm">method</em><a name="INDEX-944" />called <tt class="literal">catfile</tt>. What's a method?It's just a different kind of function, as far as we'reconcerned here. The difference is that you'll always call themethods from <tt class="literal">File::Spec</tt> with their full names,like this:</p><blockquote><pre class="code">use File::Spec;.. # Get the values for $dirname and $basename as above.my $new_name = File::Spec->catfile($dirname, $basename);rename($old_name, $new_name) or warn "Can't rename '$old_name' to '$new_name': $!";</pre></blockquote><p>As you can see, the full name of a method is the name of the module(called a<em class="firstterm">class</em><a name="INDEX-945" />,here), a <a name="INDEX-946" /><a name="INDEX-947" />small arrow, and the short nameof the method. It is important to use the small arrow, rather thanthe double-colon that we used with <tt class="literal">File::Basename</tt>.</p><p>Since we're calling the method by its full name, though, whatsymbols does the module import? None of them. That's normal forOO modules. So you don't have to worry about having asubroutine with the same name as one of the many methods of<tt class="literal">File::Spec</tt>.</p><p>Should you bother using modules like these? It's up to you, asalways. If you're sure your program will never be run anywherebut on a Unix machine, say, and you're sure you completelyunderstand the rules for filenames on Unix,<a href="#FOOTNOTE-303">[303]</a> then you may prefer to hardcode your assumptions intoyour programs. But these modules give you an easy way to make yourprograms more robust in less time -- and more<a name="INDEX-948" />portable at noextra<a name="INDEX-949" /> charge.<a name="INDEX-950" /></p><blockquote class="footnote"> <a name="FOOTNOTE-303" /><p>[303]If youdidn't know that filenames and directory names could containnewline characters, as we mentioned earlier in this section, then you<em class="emphasis">don't</em> know all the rules, do you? </p></blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch13_07.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="ch13_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">13.7. Changing Timestamps</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">13.9. Exercises</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 © 2002</a> O'Reilly & 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 + -