📄 ch32_22.htm
字号:
<html><head><title>File::Glob (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="File::Glob"><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="ch32_21.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_23.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">32.22. File::Glob</h2><p><blockquote><pre class="programlisting">use File::Glob ':glob'; # Override glob built-in.@list = <*.[Cchy]>; # Now uses POSIX glob, not csh glob.use File::Glob qw(:glob csh_glob);@sources = bsd_glob("*.{C,c,h,y,pm,xs}", GLOB_CSH);@sources = csh_glob("*.{C,c,h,y,pm,xs}"); # (same thing)use File::Glob ':glob';# call glob with extra arguments$homedir = bsd_glob('~jrhacker', GLOB_TILDE | GLOB_ERR);if (GLOB_ERROR) { # An error occurred expanding the home directory.}</pre></blockquote>The <tt class="literal">File::Glob</tt> module's <tt class="literal">bsd_glob</tt> function implements the <em class="emphasis">glob</em>(3)routine from the C library. An optional second argument containsflags governing additional matching properties. The <tt class="literal">:glob</tt> importtag imports both the function and the necessary flags.</p><p>The module also implements a <tt class="literal">csh_glob</tt> function. This is whatthe built-in Perl <tt class="literal">glob</tt> and <em class="replaceable">GLOBPAT</em> fileglobbingoperators really call. Calling <tt class="literal">csh_glob</tt> is (mostly) like calling<tt class="literal">bsd_glob</tt> this way:<blockquote><pre class="programlisting">bsd_glob(@_ ? $_[0] : $_, GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE);</pre></blockquote>If you import the <tt class="literal">:glob</tt> tag, then all calls to the built-infileglobbing operators in the current package will really call themodule's <tt class="literal">bsd_glob</tt> function instead of its <tt class="literal">csh_glob</tt> function. Onereason you might want to do this is that, although <tt class="literal">bsd_glob</tt> handlespatterns with whitespace in them correctly, <tt class="literal">csh_glob</tt> handles them, um, in the historical fashion. Old scripts would write <tt class="literal"><*.c *.h></tt> to glob both of those. Neither function is bothered by whitespacein the actual filenames, however.</p><p>The <tt class="literal">bsd_glob</tt> function takes an argument containing the fileglobbingpattern (not a regular expression pattern) plus an optional flagsargument. Filenames with a leading dot are not matched unlessspecifically requested. The return value is influenced by the flags inthe second argument, which should be bitwise ORed together:<a href="#FOOTNOTE-3">[3]</a></p><blockquote class="footnote"><a name="FOOTNOTE-3"></a><p>[3]Due to restrictions in the syntax of the built-in <tt class="literal">glob</tt> operator,you may need to call the function as <tt class="literal">bsd_glob</tt> ifyou want to pass it the second argument.</p></blockquote><dl><dt><b><tt class="literal">GLOB_BRACE</tt></b></dt><dd><p>Preprocess the string to expand <tt class="literal">{pat,pat,...}</tt> strings as <em class="emphasis">csh</em>(1)would. The pattern <tt class="literal">{}</tt> is left unexpanded for historical reasons,mostly to ease typing of <em class="emphasis">find</em>(1) patterns.</p></dd><dt><b><tt class="literal">GLOB_CSH</tt></b></dt><dd><p>Synonym for <tt class="literal">GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE</tt>.</p></dd><dt><b><tt class="literal">GLOB_ERR</tt></b></dt><dd><p>Return an error when <tt class="literal">bsd_glob</tt> encounters a directory itcannot open or read. Ordinarily, <tt class="literal">bsd_glob</tt> skips over theerror, looking for more matches.</p></dd><dt><b><tt class="literal">GLOB_MARK</tt></b></dt><dd><p>Return values that are directories with a slash appended.</p></dd><dt><b><tt class="literal">GLOB_NOCASE</tt></b></dt><dd><p>By default, filenames are case sensitive; this flagmakes <tt class="literal">bsd_glob</tt> treat case differences as insignificant.(But see below for exceptions on MS-DOSish systems).</p></dd><dt><b><tt class="literal">GLOB_NOCHECK</tt></b></dt><dd><p>If the pattern does not match any pathname, then makes <tt class="literal">bsd_glob</tt> return alist consisting of only the pattern, as <em class="emphasis">/bin/sh</em> does. If<tt class="literal">GLOB_QUOTE</tt> is set, its effect is present in the pattern returned.</p></dd><dt><b><tt class="literal">GLOB_NOMAGIC</tt></b></dt><dd><p>Same as <tt class="literal">GLOB_NOCHECK</tt> but it only returns the pattern if it does notcontain any of the special characters <tt class="literal">*</tt>, <tt class="literal">?</tt> or <tt class="literal">[</tt>. <tt class="literal">NOMAGIC</tt> isprovided to simplify implementing the historic <em class="emphasis">csh</em>(1) globbingbehavior and should probably not be used anywhere else.</p></dd><dt><b><tt class="literal">GLOB_NOSORT</tt></b></dt><dd><p>By default, the pathnames are sorted in ascending order (usingnormal character comparisons irrespective of locale setting).This flag prevents that sorting for a small increase in speed.</p></dd><dt><b><tt class="literal">GLOB_QUOTE</tt></b></dt><dd><p>Use the backslash character <tt class="literal">\</tt> for quoting: every occurrence of abackslash followed by a character in the pattern is replaced by thatcharacter, avoiding any special interpretation of the character.(But see below for exceptions on MS-DOSish systems).</p></dd><dt><b><tt class="literal">GLOB_TILDE</tt></b></dt><dd><p>Allow patterns whose first path component is<tt class="literal">~</tt><em class="replaceable">USER</em>. If <em class="replaceable">USER</em> is omitted, the tilde by itself(or followed by a slash) represents the current user's homedirectory.</p></dd></dl><p></p><p>The <tt class="literal">bsd_glob</tt> function returns a (possibly empty)list of matching paths, which will be tainted if that matters to yourprogram. On error, <tt class="literal">GLOB_ERROR</tt> will be true and<tt class="literal">$!</tt> (<tt class="literal">$OS_ERROR</tt>) will be set tothe standard system error. <tt class="literal">GLOB_ERROR</tt> isguaranteed to be false if no error occurred, and to be either<tt class="literal">GLOB_ABEND</tt> or <tt class="literal">GLOB_NOSPACE</tt>otherwise. (<tt class="literal">GLOB_ABEND</tt> means that the<tt class="literal">bsd_glob</tt> was stopped due to some error,<tt class="literal">GLOB_NOSPACE</tt> because it ran out of memory.) If<tt class="literal">bsd_glob</tt> had already found some matching paths whenthe error occurred, it returns the list of filenames found so far,<em class="emphasis">and also sets</em><tt class="literal">GLOB_ERROR</tt>.Note that this implementation of <tt class="literal">bsd_glob</tt> variesfrom most others by not considering <tt class="literal">ENOENT</tt> and<tt class="literal">ENOTDIR</tt> as terminating error conditions. Instead,it continues processing despite those errors, unless the<tt class="literal">GLOB_ERR</tt> flag is set.</p><p>If no flag argument is supplied, your system's defaults are followed,meaning that filenames differing only in case are indistinguishablefrom one another on VMS, OS/2, old Mac OS (but not Mac OS X), andMicrosoft systems (but not when Perl was built with Cygwin). If yousupply any flags at all and still want this behavior, then you mustinclude <tt class="literal">GLOB_NOCASE</tt> in the flags. Whatever system you're on, youcan change your defaults up front by importing the <tt class="literal">:case</tt> or <tt class="literal">:nocase</tt>flags.</p><p>On MS-DOSish systems, the backslash is a valid directory separatorcharacter.<a href="#FOOTNOTE-4">[4]</a> In this case, use ofbackslash as a quoting character (via <tt class="literal">GLOB_QUOTE</tt>) interferes withthe use of backslash as a directory separator. The best (simplest,most portable) solution is to use slashes for directory separators,backslashes for quoting. However, this does not match some users'expectations, so backslashes (under <tt class="literal">GLOB_QUOTE</tt>) quote only the globmetacharacters <tt class="literal">[</tt>, <tt class="literal">]</tt>, <tt class="literal">{</tt>, <tt class="literal">}</tt>, <tt class="literal">-</tt>, <tt class="literal">~</tt>, and <tt class="literal">\</tt> itself.All other backslashes are passed through unchanged, if you can manageto get them by Perl's own backslash quoting in strings. It may take asmany as four backslashes to finally match one in the filesystem. Thisis so completely insane that even MS-DOSish users should stronglyconsider using slashes. If you really want to use backslashes,look into the standard <tt class="literal">File::DosGlob</tt> module, as it mightbe more to your liking than Unix-flavored fileglobbing.</p><blockquote class="footnote"><a name="FOOTNOTE-4"></a><p>[4] Although technically, so is a slash--atleast as far as those kernels and syscalls are concerned; commandshells are remarkably less enlightened.</p></blockquote><!-- 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="ch32_21.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="ch32_23.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.21. File::Find</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">32.23. File::Spec</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 © 2001</a> O'Reilly & 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 + -