Glob.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package File::Glob;
  2. use strict;
  3. our($VERSION, @ISA, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS, $DEFAULT_FLAGS);
  4. require XSLoader;
  5. @ISA = qw(Exporter);
  6. # NOTE: The glob() export is only here for compatibility with 5.6.0.
  7. # csh_glob() should not be used directly, unless you know what you're doing.
  8. %EXPORT_TAGS = (
  9. 'glob' => [ qw(
  10. GLOB_ABEND
  11. GLOB_ALPHASORT
  12. GLOB_ALTDIRFUNC
  13. GLOB_BRACE
  14. GLOB_CSH
  15. GLOB_ERR
  16. GLOB_ERROR
  17. GLOB_LIMIT
  18. GLOB_MARK
  19. GLOB_NOCASE
  20. GLOB_NOCHECK
  21. GLOB_NOMAGIC
  22. GLOB_NOSORT
  23. GLOB_NOSPACE
  24. GLOB_QUOTE
  25. GLOB_TILDE
  26. bsd_glob
  27. glob
  28. ) ],
  29. );
  30. $EXPORT_TAGS{bsd_glob} = [@{$EXPORT_TAGS{glob}}];
  31. pop @{$EXPORT_TAGS{bsd_glob}}; # no "glob"
  32. @EXPORT_OK = (@{$EXPORT_TAGS{'glob'}}, 'csh_glob');
  33. $VERSION = '1.24';
  34. sub import {
  35. require Exporter;
  36. local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
  37. Exporter::import(grep {
  38. my $passthrough;
  39. if ($_ eq ':case') {
  40. $DEFAULT_FLAGS &= ~GLOB_NOCASE()
  41. }
  42. elsif ($_ eq ':nocase') {
  43. $DEFAULT_FLAGS |= GLOB_NOCASE();
  44. }
  45. elsif ($_ eq ':globally') {
  46. no warnings 'redefine';
  47. *CORE::GLOBAL::glob = \&File::Glob::csh_glob;
  48. }
  49. elsif ($_ eq ':bsd_glob') {
  50. no strict; *{caller."::glob"} = \&bsd_glob_override;
  51. $passthrough = 1;
  52. }
  53. else {
  54. $passthrough = 1;
  55. }
  56. $passthrough;
  57. } @_);
  58. }
  59. XSLoader::load();
  60. $DEFAULT_FLAGS = GLOB_CSH();
  61. if ($^O =~ /^(?:MSWin32|VMS|os2|dos|riscos)$/) {
  62. $DEFAULT_FLAGS |= GLOB_NOCASE();
  63. }
  64. # File::Glob::glob() is deprecated because its prototype is different from
  65. # CORE::glob() (use bsd_glob() instead)
  66. sub glob {
  67. splice @_, 1; # no flags
  68. goto &bsd_glob;
  69. }
  70. 1;
  71. __END__
  72. =head1 NAME
  73. File::Glob - Perl extension for BSD glob routine
  74. =head1 SYNOPSIS
  75. use File::Glob ':bsd_glob';
  76. @list = bsd_glob('*.[ch]');
  77. $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
  78. if (GLOB_ERROR) {
  79. # an error occurred reading $homedir
  80. }
  81. ## override the core glob (CORE::glob() does this automatically
  82. ## by default anyway, since v5.6.0)
  83. use File::Glob ':globally';
  84. my @sources = <*.{c,h,y}>;
  85. ## override the core glob, forcing case sensitivity
  86. use File::Glob qw(:globally :case);
  87. my @sources = <*.{c,h,y}>;
  88. ## override the core glob forcing case insensitivity
  89. use File::Glob qw(:globally :nocase);
  90. my @sources = <*.{c,h,y}>;
  91. ## glob on all files in home directory
  92. use File::Glob ':globally';
  93. my @sources = <~gnat/*>;
  94. =head1 DESCRIPTION
  95. The glob angle-bracket operator C<< <> >> is a pathname generator that
  96. implements the rules for file name pattern matching used by Unix-like shells
  97. such as the Bourne shell or C shell.
  98. File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is
  99. a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2").
  100. bsd_glob() takes a mandatory C<pattern> argument, and an optional
  101. C<flags> argument, and returns a list of filenames matching the
  102. pattern, with interpretation of the pattern modified by the C<flags>
  103. variable.
  104. Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob().
  105. Note that they don't share the same prototype--CORE::glob() only accepts
  106. a single argument. Due to historical reasons, CORE::glob() will also
  107. split its argument on whitespace, treating it as multiple patterns,
  108. whereas bsd_glob() considers them as one pattern. But see C<:bsd_glob>
  109. under L</EXPORTS>, below.
  110. =head2 META CHARACTERS
  111. \ Quote the next metacharacter
  112. [] Character class
  113. {} Multiple pattern
  114. * Match any string of characters
  115. ? Match any single character
  116. ~ User name home directory
  117. The metanotation C<a{b,c,d}e> is a shorthand for C<abe ace ade>. Left to
  118. right order is preserved, with results of matches being sorted separately
  119. at a low level to preserve this order. As a special case C<{>, C<}>, and
  120. C<{}> are passed undisturbed.
  121. =head2 EXPORTS
  122. See also the L</POSIX FLAGS> below, which can be exported individually.
  123. =head3 C<:bsd_glob>
  124. The C<:bsd_glob> export tag exports bsd_glob() and the constants listed
  125. below. It also overrides glob() in the calling package with one that
  126. behaves like bsd_glob() with regard to spaces (the space is treated as part
  127. of a file name), but supports iteration in scalar context; i.e., it
  128. preserves the core function's feature of returning the next item each time
  129. it is called.
  130. =head3 C<:glob>
  131. The C<:glob> tag, now discouraged, is the old version of C<:bsd_glob>. It
  132. exports the same constants and functions, but its glob() override does not
  133. support iteration; it returns the last file name in scalar context. That
  134. means this will loop forever:
  135. use File::Glob ':glob';
  136. while (my $file = <* copy.txt>) {
  137. ...
  138. }
  139. =head3 C<bsd_glob>
  140. This function, which is included in the two export tags listed above,
  141. takes one or two arguments. The first is the glob pattern. The second is
  142. a set of flags ORed together. The available flags are listed below under
  143. L</POSIX FLAGS>. If the second argument is omitted, C<GLOB_CSH> (or
  144. C<GLOB_CSH|GLOB_NOCASE> on VMS and DOSish systems) is used by default.
  145. =head3 C<:nocase> and C<:case>
  146. These two export tags globally modify the default flags that bsd_glob()
  147. and, except on VMS, Perl's built-in C<glob> operator use. C<GLOB_NOCASE>
  148. is turned on or off, respectively.
  149. =head3 C<csh_glob>
  150. The csh_glob() function can also be exported, but you should not use it
  151. directly unless you really know what you are doing. It splits the pattern
  152. into words and feeds each one to bsd_glob(). Perl's own glob() function
  153. uses this internally.
  154. =head2 POSIX FLAGS
  155. The POSIX defined flags for bsd_glob() are:
  156. =over 4
  157. =item C<GLOB_ERR>
  158. Force bsd_glob() to return an error when it encounters a directory it
  159. cannot open or read. Ordinarily bsd_glob() continues to find matches.
  160. =item C<GLOB_LIMIT>
  161. Make bsd_glob() return an error (GLOB_NOSPACE) when the pattern expands
  162. to a size bigger than the system constant C<ARG_MAX> (usually found in
  163. limits.h). If your system does not define this constant, bsd_glob() uses
  164. C<sysconf(_SC_ARG_MAX)> or C<_POSIX_ARG_MAX> where available (in that
  165. order). You can inspect these values using the standard C<POSIX>
  166. extension.
  167. =item C<GLOB_MARK>
  168. Each pathname that is a directory that matches the pattern has a slash
  169. appended.
  170. =item C<GLOB_NOCASE>
  171. By default, file names are assumed to be case sensitive; this flag
  172. makes bsd_glob() treat case differences as not significant.
  173. =item C<GLOB_NOCHECK>
  174. If the pattern does not match any pathname, then bsd_glob() returns a list
  175. consisting of only the pattern. If C<GLOB_QUOTE> is set, its effect
  176. is present in the pattern returned.
  177. =item C<GLOB_NOSORT>
  178. By default, the pathnames are sorted in ascending ASCII order; this
  179. flag prevents that sorting (speeding up bsd_glob()).
  180. =back
  181. The FreeBSD extensions to the POSIX standard are the following flags:
  182. =over 4
  183. =item C<GLOB_BRACE>
  184. Pre-process the string to expand C<{pat,pat,...}> strings like csh(1).
  185. The pattern '{}' is left unexpanded for historical reasons (and csh(1)
  186. does the same thing to ease typing of find(1) patterns).
  187. =item C<GLOB_NOMAGIC>
  188. Same as C<GLOB_NOCHECK> but it only returns the pattern if it does not
  189. contain any of the special characters "*", "?" or "[". C<NOMAGIC> is
  190. provided to simplify implementing the historic csh(1) globbing
  191. behaviour and should probably not be used anywhere else.
  192. =item C<GLOB_QUOTE>
  193. Use the backslash ('\') character for quoting: every occurrence of a
  194. backslash followed by a character in the pattern is replaced by that
  195. character, avoiding any special interpretation of the character.
  196. (But see below for exceptions on DOSISH systems).
  197. =item C<GLOB_TILDE>
  198. Expand patterns that start with '~' to user name home directories.
  199. =item C<GLOB_CSH>
  200. For convenience, C<GLOB_CSH> is a synonym for
  201. C<GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT>.
  202. =back
  203. The POSIX provided C<GLOB_APPEND>, C<GLOB_DOOFFS>, and the FreeBSD
  204. extensions C<GLOB_ALTDIRFUNC>, and C<GLOB_MAGCHAR> flags have not been
  205. implemented in the Perl version because they involve more complex
  206. interaction with the underlying C structures.
  207. The following flag has been added in the Perl implementation for
  208. csh compatibility:
  209. =over 4
  210. =item C<GLOB_ALPHASORT>
  211. If C<GLOB_NOSORT> is not in effect, sort filenames is alphabetical
  212. order (case does not matter) rather than in ASCII order.
  213. =back
  214. =head1 DIAGNOSTICS
  215. bsd_glob() returns a list of matching paths, possibly zero length. If an
  216. error occurred, &File::Glob::GLOB_ERROR will be non-zero and C<$!> will be
  217. set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred,
  218. or one of the following values otherwise:
  219. =over 4
  220. =item C<GLOB_NOSPACE>
  221. An attempt to allocate memory failed.
  222. =item C<GLOB_ABEND>
  223. The glob was stopped because an error was encountered.
  224. =back
  225. In the case where bsd_glob() has found some matching paths, but is
  226. interrupted by an error, it will return a list of filenames B<and>
  227. set &File::Glob::ERROR.
  228. Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
  229. by not considering C<ENOENT> and C<ENOTDIR> as errors - bsd_glob() will
  230. continue processing despite those errors, unless the C<GLOB_ERR> flag is
  231. set.
  232. Be aware that all filenames returned from File::Glob are tainted.
  233. =head1 NOTES
  234. =over 4
  235. =item *
  236. If you want to use multiple patterns, e.g. C<bsd_glob("a* b*")>, you should
  237. probably throw them in a set as in C<bsd_glob("{a*,b*}")>. This is because
  238. the argument to bsd_glob() isn't subjected to parsing by the C shell.
  239. Remember that you can use a backslash to escape things.
  240. =item *
  241. On DOSISH systems, backslash is a valid directory separator character.
  242. In this case, use of backslash as a quoting character (via GLOB_QUOTE)
  243. interferes with the use of backslash as a directory separator. The
  244. best (simplest, most portable) solution is to use forward slashes for
  245. directory separators, and backslashes for quoting. However, this does
  246. not match "normal practice" on these systems. As a concession to user
  247. expectation, therefore, backslashes (under GLOB_QUOTE) only quote the
  248. glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself.
  249. All other backslashes are passed through unchanged.
  250. =item *
  251. Win32 users should use the real slash. If you really want to use
  252. backslashes, consider using Sarathy's File::DosGlob, which comes with
  253. the standard Perl distribution.
  254. =back
  255. =head1 SEE ALSO
  256. L<perlfunc/glob>, glob(3)
  257. =head1 AUTHOR
  258. The Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>,
  259. and is released under the artistic license. Further modifications were
  260. made by Greg Bacon E<lt>gbacon@cs.uah.eduE<gt>, Gurusamy Sarathy
  261. E<lt>gsar@activestate.comE<gt>, and Thomas Wegner
  262. E<lt>wegner_thomas@yahoo.comE<gt>. The C glob code has the
  263. following copyright:
  264. Copyright (c) 1989, 1993 The Regents of the University of California.
  265. All rights reserved.
  266. This code is derived from software contributed to Berkeley by
  267. Guido van Rossum.
  268. Redistribution and use in source and binary forms, with or without
  269. modification, are permitted provided that the following conditions
  270. are met:
  271. 1. Redistributions of source code must retain the above copyright
  272. notice, this list of conditions and the following disclaimer.
  273. 2. Redistributions in binary form must reproduce the above copyright
  274. notice, this list of conditions and the following disclaimer in the
  275. documentation and/or other materials provided with the distribution.
  276. 3. Neither the name of the University nor the names of its contributors
  277. may be used to endorse or promote products derived from this software
  278. without specific prior written permission.
  279. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
  280. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  281. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  282. ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  283. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  284. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  285. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  286. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  287. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  288. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  289. SUCH DAMAGE.
  290. =cut