ch12_03.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 100 行
HTM
100 行
<html><head><title>An Alternate Syntax for 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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">12.3. An Alternate Syntax for Globbing</h2><p>Although we use the term globbing freely, and we talk about the<tt class="literal">glob</tt> operator, you might not see the word<tt class="literal">glob</tt> in very many of the programs that useglobbing. Why not? Well, most legacy code was written before the<tt class="literal">glob</tt> operator was given a name. Instead, it wascalled up by the <a name="INDEX-861" /> <a name="INDEX-862" />angle-bracket syntax, similar to readingfrom a filehandle:</p><blockquote><pre class="code">my @all_files = <*>; ## exactly the same as my @all_files = glob "*";</pre></blockquote><p>The value between the angle brackets is interpolated similar to adouble-quoted string, which means that Perl variables are expanded totheir current Perl values before being globbed:</p><blockquote><pre class="code">my $dir = "/etc";my @dir_files = <$dir/* $dir/.*>;</pre></blockquote><p>Here, we've fetched all the non-dot and dot files from thedesignated directory, because <tt class="literal">$dir</tt> has beenexpanded to its current value.</p><p>So, if using angle brackets means both filehandle reading and<a name="INDEX-863" /><a name="INDEX-864" />globbing, how does Perl decide whichof the two operators to use? Well, a filehandle has to be a Perlidentifier. So if the item between the angle brackets is strictly aPerl identifier, it's a filehandle read; otherwise, it'sa globbing operation. For example:</p><blockquote><pre class="code">my @files = <FRED/*>; ## a globmy @lines = <FRED>; ## a filehandle readmy $name = "FRED";my @files = <$name/*>; ## a glob</pre></blockquote><p>The one exception is if the contents are a simple scalar variable(not an element of a hash or array), then it's an<em class="firstterm">indirect filehandleread</em><a name="INDEX-865" /><em class="emphasis">,<a href="#FOOTNOTE-278">[278]</a></em> where the variable contents give the name ofthe filehandle to be read:</p><blockquote class="footnote"> <a name="FOOTNOTE-278" /><p>[278]If the indirecthandle is a text string, then it's subject to the"symbolic reference" test that is forbidden under<tt class="literal">use strict</tt>. However, the indirect handle mightalso be a typeglob or reference to an IO object, and then it wouldwork even under <tt class="literal">use strict</tt>.</p></blockquote><blockquote><pre class="code">my $name = "FRED";my @lines = <$name>; ## an indirect filehandle read of FRED handle</pre></blockquote><p>Determining whether it's a glob or a filehandle read is made atcompile time, and thus it is independent of the content of thevariables.</p><p>If you want, you can get the operation of an indirect filehandle readusing the <tt class="literal">readline</tt> operator,<a href="#FOOTNOTE-279">[279]</a> which alsomakes it clearer:</p><blockquote class="footnote"> <a name="FOOTNOTE-279" /><p>[279]Ifyou're using Perl 5.005 or later.</p> </blockquote><blockquote><pre class="code">my $name = "FRED";my @lines = readline FRED; ## read from FREDmy @lines = readline $name; ## read from FRED</pre></blockquote><p>But the <tt class="literal">readline</tt><a name="INDEX-866" /> operator is rarely used, as indirectfilehandle reads are uncommon and are generally performed against asimple scalar variable anyway.<a name="INDEX-867" /></p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch12_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">12.2. Globbing</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.4. Directory Handles</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 + =
减小字号Ctrl + -
显示快捷键?