4 # Documentation at the __END__
12 #print "doglob: ", join('|', @_), "\n";
20 next OUTER unless defined $_ and $_ ne '';
21 # if arg is within quotes strip em and do no globbing
24 if ($cond eq 'd') { push(@retval, $_) if -d $_ }
25 else { push(@retval, $_) if -e $_ }
28 # wildcards with a drive prefix such as h:*.pm must be changed
29 # to h:./*.pm to expand correctly
30 if (m|^([A-Za-z]:)[^/\\]|s) {
31 substr($_,0,2) = $1 . "./";
33 if (m|^(.*)([\\/])([^\\/]*)\z|s) {
35 ($head, $sepchr, $tail) = ($1,$2,$3);
36 #print "div: |$head|$sepchr|$tail|\n";
37 push (@retval, $_), next OUTER if $tail eq '';
38 if ($head =~ /[*?]/) {
39 @globdirs = doglob('d', $head);
40 push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
41 next OUTER if @globdirs;
43 $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:\z/s;
47 # If file component has no wildcards, we can avoid opendir
49 $head = '' if $head eq '.';
50 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
52 if ($cond eq 'd') { push(@retval,$head) if -d $head }
53 else { push(@retval,$head) if -e $head }
56 opendir(D, $head) or next OUTER;
57 my @leaves = readdir D;
59 $head = '' if $head eq '.';
60 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
62 # escape regex metachars but not glob chars
63 s:([].+^\-\${}[|]):\\$1:g;
64 # and convert DOS-style wildcards to regex
68 #print "regex: '$_', head: '$head'\n";
69 my $matchsub = eval 'sub { $_[0] =~ m|^' . $_ . '\\z|ios }';
70 warn($@), next OUTER if $@;
73 next INNER if $e eq '.' or $e eq '..';
74 next INNER if $cond eq 'd' and ! -d "$head$e";
75 push(@matched, "$head$e"), next INNER if &$matchsub($e);
77 # [DOS compatibility special case]
78 # Failed, add a trailing dot and try again, but only
79 # if name does not have a dot in it *and* pattern
80 # has a dot *and* name is shorter than 9 chars.
82 if (index($e,'.') == -1 and length($e) < 9
83 and index($_,'\\.') != -1) {
84 push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
87 push @retval, @matched if @matched;
93 # this can be used to override CORE::glob in a specific
94 # package by saying C<use File::DosGlob 'glob';> in that
98 # 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};
150 my $callpkg = ($sym =~ s/^GLOBAL_//s ? 'CORE::GLOBAL' : caller(0));
151 *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym} if $sym eq 'glob';
160 File::DosGlob - DOS like globbing and then some
166 # override CORE::glob in current package
167 use File::DosGlob 'glob';
169 # override CORE::glob in ALL packages (use with extreme caution!)
170 use File::DosGlob 'GLOBAL_glob';
172 @perlfiles = glob "..\\pe?l/*.p?";
173 print <..\\pe?l/*.p?>;
175 # from the command line (overrides only in main::)
176 > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
180 A module that implements DOS-like globbing with a few enhancements.
181 It is largely compatible with perlglob.exe (the M$ setargv.obj
182 version) in all but one respect--it understands wildcards in
183 directory components.
185 For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
186 that it will find something like '..\lib\File/DosGlob.pm' alright).
187 Note that all path components are case-insensitive, and that
188 backslashes and forward slashes are both accepted, and preserved.
189 You may have to double the backslashes if you are putting them in
190 literally, due to double-quotish parsing of the pattern by perl.
192 Spaces in the argument delimit distinct patterns, so
193 C<glob('*.exe *.dll')> globs all filenames that end in C<.exe>
194 or C<.dll>. If you want to put in literal spaces in the glob
195 pattern, you can escape them with either double quotes, or backslashes.
196 e.g. C<glob('c:/"Program Files"/*/*.dll')>, or
197 C<glob('c:/Program\ Files/*/*.dll')>. The argument is tokenized using
198 C<Text::ParseWords::parse_line()>, so see L<Text::ParseWords> for details
199 of the quoting rules used.
201 Extending it to csh patterns is left as an exercise to the reader.
203 =head1 EXPORTS (by request only)
209 Should probably be built into the core, and needs to stop
210 pandering to DOS habits. Needs a dose of optimizium too.
214 Gurusamy Sarathy <gsar@activestate.com>
222 Support for globally overriding glob() (GSAR 3-JUN-98)
226 Scalar context, independent iterator context fixes (GSAR 15-SEP-97)
230 A few dir-vs-file optimizations result in glob importation being
231 10 times faster than using perlglob.exe, and using perlglob.bat is
232 only twice as slow as perlglob.exe (GSAR 28-MAY-97)
236 Several cleanups prompted by lack of compatible perlglob.exe
237 under Borland (GSAR 27-MAY-97)
241 Initial version (GSAR 20-FEB-97)