4 # Documentation at the __END__
15 #print "doglob: ", join('|', @_), "\n";
23 next OUTER unless defined $pat and $pat ne '';
24 # if arg is within quotes strip em and do no globbing
25 if ($pat =~ /^"(.*)"\z/s) {
27 if ($cond eq 'd') { push(@retval, $pat) if -d $pat }
28 else { push(@retval, $pat) if -e $pat }
31 # wildcards with a drive prefix such as h:*.pm must be changed
32 # to h:./*.pm to expand correctly
33 if ($pat =~ m|^([A-Za-z]:)[^/\\]|s) {
34 substr($_,0,2) = $1 . "./";
36 if ($pat =~ m|^(.*)([\\/])([^\\/]*)\z|s) {
37 ($head, $sepchr, $tail) = ($1,$2,$3);
38 #print "div: |$head|$sepchr|$tail|\n";
39 push (@retval, $pat), next OUTER if $tail eq '';
40 if ($head =~ /[*?]/) {
41 @globdirs = doglob('d', $head);
42 push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
43 next OUTER if @globdirs;
45 $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:\z/s;
49 # If file component has no wildcards, we can avoid opendir
50 unless ($pat =~ /[*?]/) {
51 $head = '' if $head eq '.';
52 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
54 if ($cond eq 'd') { push(@retval,$head) if -d $head }
55 else { push(@retval,$head) if -e $head }
58 opendir(D, $head) or next OUTER;
59 my @leaves = readdir D;
61 $head = '' if $head eq '.';
62 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
64 # escape regex metachars but not glob chars
65 $pat =~ s:([].+^\-\${}[|]):\\$1:g;
66 # and convert DOS-style wildcards to regex
70 #print "regex: '$pat', head: '$head'\n";
71 my $matchsub = sub { $_[0] =~ m|^$pat\z|is };
74 next INNER if $e eq '.' or $e eq '..';
75 next INNER if $cond eq 'd' and ! -d "$head$e";
76 push(@matched, "$head$e"), next INNER if &$matchsub($e);
78 # [DOS compatibility special case]
79 # Failed, add a trailing dot and try again, but only
80 # if name does not have a dot in it *and* pattern
81 # has a dot *and* name is shorter than 9 chars.
83 if (index($e,'.') == -1 and length($e) < 9
84 and index($pat,'\\.') != -1) {
85 push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
88 push @retval, @matched if @matched;
94 # this can be used to override CORE::glob in a specific
95 # package by saying C<use File::DosGlob 'glob';> in that
99 # context (keyed by second cxix arg provided by core)
107 # glob without args defaults to $_
108 $pat = $_ unless defined $pat;
112 require Text::ParseWords;
113 @pat = Text::ParseWords::parse_line('\s+',0,$pat);
119 # assume global context if not provided one
120 $cxix = '_G_' unless defined $cxix;
121 $iter{$cxix} = 0 unless exists $iter{$cxix};
123 # if we're just beginning, do it all first
124 if ($iter{$cxix} == 0) {
125 $entries{$cxix} = [doglob(1,@pat)];
128 # chuck it all out, quick or slow
131 return @{delete $entries{$cxix}};
134 if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
135 return shift @{$entries{$cxix}};
138 # return undef for EOL
140 delete $entries{$cxix};
153 my $callpkg = ($sym =~ s/^GLOBAL_//s ? 'CORE::GLOBAL' : caller(0));
154 *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym} if $sym eq 'glob';
163 File::DosGlob - DOS like globbing and then some
169 # override CORE::glob in current package
170 use File::DosGlob 'glob';
172 # override CORE::glob in ALL packages (use with extreme caution!)
173 use File::DosGlob 'GLOBAL_glob';
175 @perlfiles = glob "..\\pe?l/*.p?";
176 print <..\\pe?l/*.p?>;
178 # from the command line (overrides only in main::)
179 > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
183 A module that implements DOS-like globbing with a few enhancements.
184 It is largely compatible with perlglob.exe (the M$ setargv.obj
185 version) in all but one respect--it understands wildcards in
186 directory components.
188 For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
189 that it will find something like '..\lib\File/DosGlob.pm' alright).
190 Note that all path components are case-insensitive, and that
191 backslashes and forward slashes are both accepted, and preserved.
192 You may have to double the backslashes if you are putting them in
193 literally, due to double-quotish parsing of the pattern by perl.
195 Spaces in the argument delimit distinct patterns, so
196 C<glob('*.exe *.dll')> globs all filenames that end in C<.exe>
197 or C<.dll>. If you want to put in literal spaces in the glob
198 pattern, you can escape them with either double quotes, or backslashes.
199 e.g. C<glob('c:/"Program Files"/*/*.dll')>, or
200 C<glob('c:/Program\ Files/*/*.dll')>. The argument is tokenized using
201 C<Text::ParseWords::parse_line()>, so see L<Text::ParseWords> for details
202 of the quoting rules used.
204 Extending it to csh patterns is left as an exercise to the reader.
206 =head1 EXPORTS (by request only)
212 Should probably be built into the core, and needs to stop
213 pandering to DOS habits. Needs a dose of optimizium too.
217 Gurusamy Sarathy <gsar@activestate.com>
225 Support for globally overriding glob() (GSAR 3-JUN-98)
229 Scalar context, independent iterator context fixes (GSAR 15-SEP-97)
233 A few dir-vs-file optimizations result in glob importation being
234 10 times faster than using perlglob.exe, and using perlglob.bat is
235 only twice as slow as perlglob.exe (GSAR 28-MAY-97)
239 Several cleanups prompted by lack of compatible perlglob.exe
240 under Borland (GSAR 27-MAY-97)
244 Initial version (GSAR 20-FEB-97)