xref: /openbsd-src/gnu/usr.bin/perl/ext/File-Glob/Glob.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
143003dfeSmillertpackage File::Glob;
243003dfeSmillert
343003dfeSmillertuse strict;
4eac174f2Safresh1our($DEFAULT_FLAGS);
543003dfeSmillert
6898184e3Ssthenrequire XSLoader;
743003dfeSmillert
843003dfeSmillert# NOTE: The glob() export is only here for compatibility with 5.6.0.
943003dfeSmillert# csh_glob() should not be used directly, unless you know what you're doing.
1043003dfeSmillert
11eac174f2Safresh1our %EXPORT_TAGS = (
1243003dfeSmillert    'glob' => [ qw(
1343003dfeSmillert        GLOB_ABEND
1443003dfeSmillert        GLOB_ALPHASORT
1543003dfeSmillert        GLOB_ALTDIRFUNC
1643003dfeSmillert        GLOB_BRACE
1743003dfeSmillert        GLOB_CSH
1843003dfeSmillert        GLOB_ERR
1943003dfeSmillert        GLOB_ERROR
2043003dfeSmillert        GLOB_LIMIT
2143003dfeSmillert        GLOB_MARK
2243003dfeSmillert        GLOB_NOCASE
2343003dfeSmillert        GLOB_NOCHECK
2443003dfeSmillert        GLOB_NOMAGIC
2543003dfeSmillert        GLOB_NOSORT
2643003dfeSmillert        GLOB_NOSPACE
2743003dfeSmillert        GLOB_QUOTE
2843003dfeSmillert        GLOB_TILDE
2943003dfeSmillert        bsd_glob
3043003dfeSmillert    ) ],
3143003dfeSmillert);
32898184e3Ssthen$EXPORT_TAGS{bsd_glob} = [@{$EXPORT_TAGS{glob}}];
3343003dfeSmillert
34eac174f2Safresh1our @EXPORT_OK   = (@{$EXPORT_TAGS{'glob'}}, 'csh_glob');
35898184e3Ssthen
36*3d61058aSafresh1our $VERSION = '1.42';
3743003dfeSmillert
3843003dfeSmillertsub import {
3943003dfeSmillert    require Exporter;
40898184e3Ssthen    local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
41898184e3Ssthen    Exporter::import(grep {
42898184e3Ssthen        my $passthrough;
4391f110e0Safresh1        if ($_ eq ':case') {
4491f110e0Safresh1            $DEFAULT_FLAGS &= ~GLOB_NOCASE()
4591f110e0Safresh1        }
4691f110e0Safresh1        elsif ($_ eq ':nocase') {
4791f110e0Safresh1            $DEFAULT_FLAGS |= GLOB_NOCASE();
4891f110e0Safresh1        }
4991f110e0Safresh1        elsif ($_ eq ':globally') {
50898184e3Ssthen	    no warnings 'redefine';
5143003dfeSmillert	    *CORE::GLOBAL::glob = \&File::Glob::csh_glob;
5243003dfeSmillert	}
5391f110e0Safresh1        elsif ($_ eq ':bsd_glob') {
54898184e3Ssthen	    no strict; *{caller."::glob"} = \&bsd_glob_override;
5591f110e0Safresh1            $passthrough = 1;
5643003dfeSmillert	}
5791f110e0Safresh1	else {
58898184e3Ssthen            $passthrough = 1;
5943003dfeSmillert        }
60898184e3Ssthen        $passthrough;
61898184e3Ssthen    } @_);
6243003dfeSmillert}
6343003dfeSmillert
64898184e3SsthenXSLoader::load();
6543003dfeSmillert
6643003dfeSmillert$DEFAULT_FLAGS = GLOB_CSH();
67eac174f2Safresh1if ($^O =~ /^(?:MSWin32|VMS|os2|riscos)$/) {
6843003dfeSmillert    $DEFAULT_FLAGS |= GLOB_NOCASE();
6943003dfeSmillert}
7043003dfeSmillert
7143003dfeSmillert1;
7243003dfeSmillert__END__
7343003dfeSmillert
7443003dfeSmillert=head1 NAME
7543003dfeSmillert
7643003dfeSmillertFile::Glob - Perl extension for BSD glob routine
7743003dfeSmillert
7843003dfeSmillert=head1 SYNOPSIS
7943003dfeSmillert
80898184e3Ssthen  use File::Glob ':bsd_glob';
8143003dfeSmillert
8243003dfeSmillert  @list = bsd_glob('*.[ch]');
8343003dfeSmillert  $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
8443003dfeSmillert
8543003dfeSmillert  if (GLOB_ERROR) {
8643003dfeSmillert    # an error occurred reading $homedir
8743003dfeSmillert  }
8843003dfeSmillert
8943003dfeSmillert  ## override the core glob (CORE::glob() does this automatically
9043003dfeSmillert  ## by default anyway, since v5.6.0)
9143003dfeSmillert  use File::Glob ':globally';
9243003dfeSmillert  my @sources = <*.{c,h,y}>;
9343003dfeSmillert
9443003dfeSmillert  ## override the core glob, forcing case sensitivity
9543003dfeSmillert  use File::Glob qw(:globally :case);
9643003dfeSmillert  my @sources = <*.{c,h,y}>;
9743003dfeSmillert
9843003dfeSmillert  ## override the core glob forcing case insensitivity
9943003dfeSmillert  use File::Glob qw(:globally :nocase);
10043003dfeSmillert  my @sources = <*.{c,h,y}>;
10143003dfeSmillert
10243003dfeSmillert  ## glob on all files in home directory
10343003dfeSmillert  use File::Glob ':globally';
10443003dfeSmillert  my @sources = <~gnat/*>;
10543003dfeSmillert
10643003dfeSmillert=head1 DESCRIPTION
10743003dfeSmillert
10843003dfeSmillertThe glob angle-bracket operator C<< <> >> is a pathname generator that
10943003dfeSmillertimplements the rules for file name pattern matching used by Unix-like shells
11043003dfeSmillertsuch as the Bourne shell or C shell.
11143003dfeSmillert
11243003dfeSmillertFile::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is
11343003dfeSmillerta superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2").
11443003dfeSmillertbsd_glob() takes a mandatory C<pattern> argument, and an optional
11543003dfeSmillertC<flags> argument, and returns a list of filenames matching the
11643003dfeSmillertpattern, with interpretation of the pattern modified by the C<flags>
11743003dfeSmillertvariable.
11843003dfeSmillert
11943003dfeSmillertSince v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob().
12043003dfeSmillertNote that they don't share the same prototype--CORE::glob() only accepts
12143003dfeSmillerta single argument.  Due to historical reasons, CORE::glob() will also
12243003dfeSmillertsplit its argument on whitespace, treating it as multiple patterns,
123898184e3Ssthenwhereas bsd_glob() considers them as one pattern.  But see C<:bsd_glob>
124898184e3Ssthenunder L</EXPORTS>, below.
12543003dfeSmillert
12643003dfeSmillert=head2 META CHARACTERS
12743003dfeSmillert
12843003dfeSmillert  \       Quote the next metacharacter
12943003dfeSmillert  []      Character class
13043003dfeSmillert  {}      Multiple pattern
13143003dfeSmillert  *       Match any string of characters
13243003dfeSmillert  ?       Match any single character
13343003dfeSmillert  ~       User name home directory
13443003dfeSmillert
13543003dfeSmillertThe metanotation C<a{b,c,d}e> is a shorthand for C<abe ace ade>.  Left to
13643003dfeSmillertright order is preserved, with results of matches being sorted separately
13743003dfeSmillertat a low level to preserve this order.  As a special case C<{>, C<}>, and
13843003dfeSmillertC<{}> are passed undisturbed.
13943003dfeSmillert
140898184e3Ssthen=head2 EXPORTS
141898184e3Ssthen
142898184e3SsthenSee also the L</POSIX FLAGS> below, which can be exported individually.
143898184e3Ssthen
144898184e3Ssthen=head3 C<:bsd_glob>
145898184e3Ssthen
146898184e3SsthenThe C<:bsd_glob> export tag exports bsd_glob() and the constants listed
147898184e3Ssthenbelow.  It also overrides glob() in the calling package with one that
148898184e3Ssthenbehaves like bsd_glob() with regard to spaces (the space is treated as part
149898184e3Ssthenof a file name), but supports iteration in scalar context; i.e., it
150898184e3Ssthenpreserves the core function's feature of returning the next item each time
151898184e3Ssthenit is called.
152898184e3Ssthen
153898184e3Ssthen=head3 C<:glob>
154898184e3Ssthen
155898184e3SsthenThe C<:glob> tag, now discouraged, is the old version of C<:bsd_glob>.  It
156898184e3Ssthenexports the same constants and functions, but its glob() override does not
157898184e3Ssthensupport iteration; it returns the last file name in scalar context.  That
158898184e3Ssthenmeans this will loop forever:
159898184e3Ssthen
160898184e3Ssthen    use File::Glob ':glob';
161898184e3Ssthen    while (my $file = <* copy.txt>) {
162898184e3Ssthen	...
163898184e3Ssthen    }
164898184e3Ssthen
165898184e3Ssthen=head3 C<bsd_glob>
166898184e3Ssthen
167898184e3SsthenThis function, which is included in the two export tags listed above,
1689f11ffb7Safresh1takes one or two arguments.  The first is the glob pattern.  The
1699f11ffb7Safresh1second, if given, is a set of flags ORed together.  The available
1709f11ffb7Safresh1flags and the default set of flags are listed below under L</POSIX FLAGS>.
1719f11ffb7Safresh1
1729f11ffb7Safresh1Remember that to use the named constants for flags you must import
1739f11ffb7Safresh1them, for example with C<:bsd_glob> described above.  If not imported,
1749f11ffb7Safresh1and C<use strict> is not in effect, then the constants will be
1759f11ffb7Safresh1treated as bareword strings, which won't do what you what.
1769f11ffb7Safresh1
177898184e3Ssthen
178898184e3Ssthen=head3 C<:nocase> and C<:case>
179898184e3Ssthen
180898184e3SsthenThese two export tags globally modify the default flags that bsd_glob()
181898184e3Ssthenand, except on VMS, Perl's built-in C<glob> operator use.  C<GLOB_NOCASE>
182898184e3Ssthenis turned on or off, respectively.
183898184e3Ssthen
184898184e3Ssthen=head3 C<csh_glob>
185898184e3Ssthen
186898184e3SsthenThe csh_glob() function can also be exported, but you should not use it
187898184e3Ssthendirectly unless you really know what you are doing.  It splits the pattern
188898184e3Sstheninto words and feeds each one to bsd_glob().  Perl's own glob() function
189898184e3Ssthenuses this internally.
190898184e3Ssthen
19143003dfeSmillert=head2 POSIX FLAGS
19243003dfeSmillert
193*3d61058aSafresh1If no flags argument is given then C<GLOB_CSH> is set, and on VMS and
1949f11ffb7Safresh1Windows systems, C<GLOB_NOCASE> too.  Otherwise the flags to use are
1959f11ffb7Safresh1determined solely by the flags argument.  The POSIX defined flags are:
19643003dfeSmillert
19743003dfeSmillert=over 4
19843003dfeSmillert
19943003dfeSmillert=item C<GLOB_ERR>
20043003dfeSmillert
20143003dfeSmillertForce bsd_glob() to return an error when it encounters a directory it
20243003dfeSmillertcannot open or read.  Ordinarily bsd_glob() continues to find matches.
20343003dfeSmillert
20443003dfeSmillert=item C<GLOB_LIMIT>
20543003dfeSmillert
20643003dfeSmillertMake bsd_glob() return an error (GLOB_NOSPACE) when the pattern expands
20743003dfeSmillertto a size bigger than the system constant C<ARG_MAX> (usually found in
20843003dfeSmillertlimits.h).  If your system does not define this constant, bsd_glob() uses
20943003dfeSmillertC<sysconf(_SC_ARG_MAX)> or C<_POSIX_ARG_MAX> where available (in that
21043003dfeSmillertorder).  You can inspect these values using the standard C<POSIX>
21143003dfeSmillertextension.
21243003dfeSmillert
21343003dfeSmillert=item C<GLOB_MARK>
21443003dfeSmillert
21543003dfeSmillertEach pathname that is a directory that matches the pattern has a slash
21643003dfeSmillertappended.
21743003dfeSmillert
21843003dfeSmillert=item C<GLOB_NOCASE>
21943003dfeSmillert
22043003dfeSmillertBy default, file names are assumed to be case sensitive; this flag
22143003dfeSmillertmakes bsd_glob() treat case differences as not significant.
22243003dfeSmillert
22343003dfeSmillert=item C<GLOB_NOCHECK>
22443003dfeSmillert
22543003dfeSmillertIf the pattern does not match any pathname, then bsd_glob() returns a list
22643003dfeSmillertconsisting of only the pattern.  If C<GLOB_QUOTE> is set, its effect
22743003dfeSmillertis present in the pattern returned.
22843003dfeSmillert
22943003dfeSmillert=item C<GLOB_NOSORT>
23043003dfeSmillert
23143003dfeSmillertBy default, the pathnames are sorted in ascending ASCII order; this
23243003dfeSmillertflag prevents that sorting (speeding up bsd_glob()).
23343003dfeSmillert
23443003dfeSmillert=back
23543003dfeSmillert
23643003dfeSmillertThe FreeBSD extensions to the POSIX standard are the following flags:
23743003dfeSmillert
23843003dfeSmillert=over 4
23943003dfeSmillert
24043003dfeSmillert=item C<GLOB_BRACE>
24143003dfeSmillert
24243003dfeSmillertPre-process the string to expand C<{pat,pat,...}> strings like csh(1).
24343003dfeSmillertThe pattern '{}' is left unexpanded for historical reasons (and csh(1)
24443003dfeSmillertdoes the same thing to ease typing of find(1) patterns).
24543003dfeSmillert
24643003dfeSmillert=item C<GLOB_NOMAGIC>
24743003dfeSmillert
24843003dfeSmillertSame as C<GLOB_NOCHECK> but it only returns the pattern if it does not
24943003dfeSmillertcontain any of the special characters "*", "?" or "[".  C<NOMAGIC> is
25043003dfeSmillertprovided to simplify implementing the historic csh(1) globbing
25143003dfeSmillertbehaviour and should probably not be used anywhere else.
25243003dfeSmillert
25343003dfeSmillert=item C<GLOB_QUOTE>
25443003dfeSmillert
25543003dfeSmillertUse the backslash ('\') character for quoting: every occurrence of a
25643003dfeSmillertbackslash followed by a character in the pattern is replaced by that
25743003dfeSmillertcharacter, avoiding any special interpretation of the character.
25843003dfeSmillert(But see below for exceptions on DOSISH systems).
25943003dfeSmillert
26043003dfeSmillert=item C<GLOB_TILDE>
26143003dfeSmillert
26243003dfeSmillertExpand patterns that start with '~' to user name home directories.
26343003dfeSmillert
26443003dfeSmillert=item C<GLOB_CSH>
26543003dfeSmillert
26643003dfeSmillertFor convenience, C<GLOB_CSH> is a synonym for
26743003dfeSmillertC<GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT>.
26843003dfeSmillert
26943003dfeSmillert=back
27043003dfeSmillert
27143003dfeSmillertThe POSIX provided C<GLOB_APPEND>, C<GLOB_DOOFFS>, and the FreeBSD
27243003dfeSmillertextensions C<GLOB_ALTDIRFUNC>, and C<GLOB_MAGCHAR> flags have not been
27343003dfeSmillertimplemented in the Perl version because they involve more complex
27443003dfeSmillertinteraction with the underlying C structures.
27543003dfeSmillert
27643003dfeSmillertThe following flag has been added in the Perl implementation for
27743003dfeSmillertcsh compatibility:
27843003dfeSmillert
27943003dfeSmillert=over 4
28043003dfeSmillert
28143003dfeSmillert=item C<GLOB_ALPHASORT>
28243003dfeSmillert
28343003dfeSmillertIf C<GLOB_NOSORT> is not in effect, sort filenames is alphabetical
28443003dfeSmillertorder (case does not matter) rather than in ASCII order.
28543003dfeSmillert
28643003dfeSmillert=back
28743003dfeSmillert
28843003dfeSmillert=head1 DIAGNOSTICS
28943003dfeSmillert
29043003dfeSmillertbsd_glob() returns a list of matching paths, possibly zero length.  If an
29143003dfeSmillerterror occurred, &File::Glob::GLOB_ERROR will be non-zero and C<$!> will be
29243003dfeSmillertset.  &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred,
29343003dfeSmillertor one of the following values otherwise:
29443003dfeSmillert
29543003dfeSmillert=over 4
29643003dfeSmillert
29743003dfeSmillert=item C<GLOB_NOSPACE>
29843003dfeSmillert
29943003dfeSmillertAn attempt to allocate memory failed.
30043003dfeSmillert
30143003dfeSmillert=item C<GLOB_ABEND>
30243003dfeSmillert
30343003dfeSmillertThe glob was stopped because an error was encountered.
30443003dfeSmillert
30543003dfeSmillert=back
30643003dfeSmillert
30743003dfeSmillertIn the case where bsd_glob() has found some matching paths, but is
30843003dfeSmillertinterrupted by an error, it will return a list of filenames B<and>
30943003dfeSmillertset &File::Glob::ERROR.
31043003dfeSmillert
31143003dfeSmillertNote that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
31243003dfeSmillertby not considering C<ENOENT> and C<ENOTDIR> as errors - bsd_glob() will
31343003dfeSmillertcontinue processing despite those errors, unless the C<GLOB_ERR> flag is
31443003dfeSmillertset.
31543003dfeSmillert
31643003dfeSmillertBe aware that all filenames returned from File::Glob are tainted.
31743003dfeSmillert
31843003dfeSmillert=head1 NOTES
31943003dfeSmillert
32043003dfeSmillert=over 4
32143003dfeSmillert
32243003dfeSmillert=item *
32343003dfeSmillert
32443003dfeSmillertIf you want to use multiple patterns, e.g. C<bsd_glob("a* b*")>, you should
32543003dfeSmillertprobably throw them in a set as in C<bsd_glob("{a*,b*}")>.  This is because
32643003dfeSmillertthe argument to bsd_glob() isn't subjected to parsing by the C shell.
32743003dfeSmillertRemember that you can use a backslash to escape things.
32843003dfeSmillert
32943003dfeSmillert=item *
33043003dfeSmillert
33143003dfeSmillertOn DOSISH systems, backslash is a valid directory separator character.
33243003dfeSmillertIn this case, use of backslash as a quoting character (via GLOB_QUOTE)
33343003dfeSmillertinterferes with the use of backslash as a directory separator.  The
33443003dfeSmillertbest (simplest, most portable) solution is to use forward slashes for
33543003dfeSmillertdirectory separators, and backslashes for quoting.  However, this does
33643003dfeSmillertnot match "normal practice" on these systems.  As a concession to user
33743003dfeSmillertexpectation, therefore, backslashes (under GLOB_QUOTE) only quote the
33843003dfeSmillertglob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself.
33943003dfeSmillertAll other backslashes are passed through unchanged.
34043003dfeSmillert
34143003dfeSmillert=item *
34243003dfeSmillert
34343003dfeSmillertWin32 users should use the real slash.  If you really want to use
34443003dfeSmillertbackslashes, consider using Sarathy's File::DosGlob, which comes with
34543003dfeSmillertthe standard Perl distribution.
34643003dfeSmillert
34743003dfeSmillert=back
34843003dfeSmillert
34943003dfeSmillert=head1 SEE ALSO
35043003dfeSmillert
35143003dfeSmillertL<perlfunc/glob>, glob(3)
35243003dfeSmillert
35343003dfeSmillert=head1 AUTHOR
35443003dfeSmillert
35543003dfeSmillertThe Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>,
35643003dfeSmillertand is released under the artistic license.  Further modifications were
35743003dfeSmillertmade by Greg Bacon E<lt>gbacon@cs.uah.eduE<gt>, Gurusamy Sarathy
35843003dfeSmillertE<lt>gsar@activestate.comE<gt>, and Thomas Wegner
35943003dfeSmillertE<lt>wegner_thomas@yahoo.comE<gt>.  The C glob code has the
36043003dfeSmillertfollowing copyright:
36143003dfeSmillert
36243003dfeSmillertCopyright (c) 1989, 1993 The Regents of the University of California.
36343003dfeSmillertAll rights reserved.
36443003dfeSmillert
36543003dfeSmillertThis code is derived from software contributed to Berkeley by
36643003dfeSmillertGuido van Rossum.
36743003dfeSmillert
36843003dfeSmillertRedistribution and use in source and binary forms, with or without
36943003dfeSmillertmodification, are permitted provided that the following conditions
37043003dfeSmillertare met:
37143003dfeSmillert
372b8851fccSafresh1=over 4
373b8851fccSafresh1
374b8851fccSafresh1=item 1.
375b8851fccSafresh1
376b8851fccSafresh1Redistributions of source code must retain the above copyright
37743003dfeSmillertnotice, this list of conditions and the following disclaimer.
378b8851fccSafresh1
379b8851fccSafresh1=item 2.
380b8851fccSafresh1
381b8851fccSafresh1Redistributions in binary form must reproduce the above copyright
38243003dfeSmillertnotice, this list of conditions and the following disclaimer in the
38343003dfeSmillertdocumentation and/or other materials provided with the distribution.
384b8851fccSafresh1
385b8851fccSafresh1=item 3.
386b8851fccSafresh1
387b8851fccSafresh1Neither the name of the University nor the names of its contributors
38843003dfeSmillertmay be used to endorse or promote products derived from this software
38943003dfeSmillertwithout specific prior written permission.
39043003dfeSmillert
391b8851fccSafresh1=back
392b8851fccSafresh1
393898184e3SsthenTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
39443003dfeSmillertANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39543003dfeSmillertIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39643003dfeSmillertARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39743003dfeSmillertFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39843003dfeSmillertDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39943003dfeSmillertOR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40043003dfeSmillertHOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40143003dfeSmillertLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40243003dfeSmillertOUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40343003dfeSmillertSUCH DAMAGE.
40443003dfeSmillert
40543003dfeSmillert=cut
406