📄 ch12_02.htm
字号:
<html><head><title>Globbing (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 & 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="ch12_01.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="ch12_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">12.2. Globbing</h2><p><a name="INDEX-858" />Normally,the shell expands any filename patterns on each command line into thematching filenames. This is called <em class="emphasis">globbing</em>. Forexample, if you give a filename pattern of <tt class="literal">*.pm</tt> tothe <i class="command">echo</i><a name="INDEX-859" /> command, the shell expands this list toa list of names that match:</p><blockquote><pre class="code">$ <tt class="userinput"><b>echo *.pm</b></tt>barney.pm dino.pm fred.pm wilma.pm$</pre></blockquote><p>The <em class="emphasis">echo</em> command doesn't have to knowanything about expanding <tt class="literal">*.pm</tt>, because the shellhas already expanded it. This works even for your Perl programs:</p><blockquote><pre class="code">$ <tt class="userinput"><b>cat >show-args</b></tt><tt class="userinput"><b>foreach $arg (@ARGV) {</b></tt><tt class="userinput"><b> print "one arg is $arg\n";</b></tt><tt class="userinput"><b>}</b></tt><tt class="userinput"><b>^D</b></tt>$ <tt class="userinput"><b>perl show-args *.pm</b></tt>one arg is barney.pmone arg is dino.pmone arg is fred.pmone arg is wilma.pm$</pre></blockquote><p>Note that <i class="command">show-args</i> didn't need to knowanything about globbing -- the names were already expanded in<tt class="literal">@ARGV</tt>.</p><p>But sometimes we end up with a pattern like <tt class="literal">*.pm</tt>inside our Perl program. Can we expand this pattern into the matchingfilenames without working very hard? Sure -- just use the<tt class="literal">glob</tt><a name="INDEX-860" /> operator:</p><blockquote><pre class="code">my @all_files = glob "*";my @pm_files = glob "*.pm";</pre></blockquote><p>Here, <tt class="literal">@all_files</tt> gets all the files in the currentdirectory, alphabetically sorted, and not including the filesbeginning with a period, just like the shell. And<tt class="literal">@pm_files</tt> gets the same list as we got before byusing <tt class="literal">*.pm</tt> on the command line.</p><p>In fact, anything you can say on the command line, you can also putas the (single) argument to <tt class="literal">glob</tt>, includingmultiple patterns separated by spaces:</p><blockquote><pre class="code">my @all_files_including_dot = glob ".* *";</pre></blockquote><p>Here, we've included an additional "dot star"parameter to get the filenames that begin with a dot as well as theones that don't. Please note that the space between these twoitems inside the quoted string is significant, as it separates twodifferent items to be globbed.<a href="#FOOTNOTE-276">[276]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-276" /><p>[276]Windows users may beaccustomed to using a glob of *.* to mean "all files".But that actually means "all files with a dot in theirnames," even in Perl on Windows.</p> </blockquote><p>The reason this works exactly as the shell works is that prior toPerl Version 5.6, the <tt class="literal">glob</tt> operator simplycalled <em class="emphasis">/bin/csh</em><a href="#FOOTNOTE-277">[277]</a> behind the scenes to perform the expansion. Because ofthis, globs were time-consuming and could break in large directories,or in some other cases. Conscientious Perl hackers avoided globbingin favor of directory handles, which will be discussed in<a href="ch12_04.htm#lperl3-CHP-12-SECT-4">Section 12.4, "Directory Handles"</a> later in this chapter. However, ifyou're using a modern version of Perl, you should no longer beconcerned about such things.</p><blockquote class="footnote"> <a name="FOOTNOTE-277" /><p>[277]Or it will calla valid substitute if a C-shell wasn't available.</p></blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch12_01.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="ch12_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">12. Directory Operations</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">12.3. An Alternate Syntax for Globbing</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 + -