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 if (m|^(.*)([\\/])([^\\/]*)$|) {
30 ($head, $sepchr, $tail) = ($1,$2,$3);
31 #print "div: |$head|$sepchr|$tail|\n";
32 push (@retval, $_), next OUTER if $tail eq '';
33 if ($head =~ /[*?]/) {
34 @globdirs = doglob('d', $head);
35 push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
36 next OUTER if @globdirs;
38 $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:$/;
42 # If file component has no wildcards, we can avoid opendir
44 $head = '' if $head eq '.';
45 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
47 if ($cond eq 'd') { push(@retval,$head) if -d $head }
48 else { push(@retval,$head) if -e $head }
51 opendir(D, $head) or next OUTER;
52 my @leaves = readdir D;
54 $head = '' if $head eq '.';
55 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
57 # escape regex metachars but not glob chars
58 s:([].+^\-\${}[|]):\\$1:g;
59 # and convert DOS-style wildcards to regex
63 #print "regex: '$_', head: '$head'\n";
64 my $matchsub = eval 'sub { $_[0] =~ m|^' . $_ . '$|io }';
65 warn($@), next OUTER if $@;
68 next INNER if $e eq '.' or $e eq '..';
69 next INNER if $cond eq 'd' and ! -d "$head$e";
70 push(@matched, "$head$e"), next INNER if &$matchsub($e);
72 # [DOS compatibility special case]
73 # Failed, add a trailing dot and try again, but only
74 # if name does not have a dot in it *and* pattern
75 # has a dot *and* name is shorter than 9 chars.
77 if (index($e,'.') == -1 and length($e) < 9
78 and index($_,'\\.') != -1) {
79 push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
82 push @retval, @matched if @matched;
88 # this can be used to override CORE::glob in a specific
89 # package by saying C<use File::DosGlob 'glob';> in that
93 # context (keyed by second cxix arg provided by core)
102 # glob without args defaults to $_
103 $pat = $_ unless defined $pat;
107 require Text::ParseWords;
108 @pat = Text::ParseWords::parse_line('\s+',0,$pat);
114 # assume global context if not provided one
115 $cxix = '_G_' unless defined $cxix;
116 $iter{$cxix} = 0 unless exists $iter{$cxix};
118 # if we're just beginning, do it all first
119 if ($iter{$cxix} == 0) {
120 $entries{$cxix} = [doglob(1,@pat)];
123 # chuck it all out, quick or slow
126 return @{delete $entries{$cxix}};
129 if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
130 return shift @{$entries{$cxix}};
133 # return undef for EOL
135 delete $entries{$cxix};
145 my $callpkg = ($sym =~ s/^GLOBAL_// ? 'CORE::GLOBAL' : caller(0));
146 *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym} if $sym eq 'glob';
155 File::DosGlob - DOS like globbing and then some
161 # override CORE::glob in current package
162 use File::DosGlob 'glob';
164 # override CORE::glob in ALL packages (use with extreme caution!)
165 use File::DosGlob 'GLOBAL_glob';
167 @perlfiles = glob "..\\pe?l/*.p?";
168 print <..\\pe?l/*.p?>;
170 # from the command line (overrides only in main::)
171 > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
175 A module that implements DOS-like globbing with a few enhancements.
176 It is largely compatible with perlglob.exe (the M$ setargv.obj
177 version) in all but one respect--it understands wildcards in
178 directory components.
180 For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
181 that it will find something like '..\lib\File/DosGlob.pm' alright).
182 Note that all path components are case-insensitive, and that
183 backslashes and forward slashes are both accepted, and preserved.
184 You may have to double the backslashes if you are putting them in
185 literally, due to double-quotish parsing of the pattern by perl.
187 Spaces in the argument delimit distinct patterns, so
188 C<glob('*.exe *.dll')> globs all filenames that end in C<.exe>
189 or C<.dll>. If you want to put in literal spaces in the glob
190 pattern, you can escape them with either double quotes, or backslashes.
191 e.g. C<glob('c:/"Program Files"/*/*.dll')>, or
192 C<glob('c:/Program\ Files/*/*.dll')>. The argument is tokenized using
193 C<Text::ParseWords::parse_line()>, so see L<Text::ParseWords> for details
194 of the quoting rules used.
196 Extending it to csh patterns is left as an exercise to the reader.
198 =head1 EXPORTS (by request only)
204 Should probably be built into the core, and needs to stop
205 pandering to DOS habits. Needs a dose of optimizium too.
209 Gurusamy Sarathy <gsar@activestate.com>
217 Support for globally overriding glob() (GSAR 3-JUN-98)
221 Scalar context, independent iterator context fixes (GSAR 15-SEP-97)
225 A few dir-vs-file optimizations result in glob importation being
226 10 times faster than using perlglob.exe, and using perlglob.bat is
227 only twice as slow as perlglob.exe (GSAR 28-MAY-97)
231 Several cleanups prompted by lack of compatible perlglob.exe
232 under Borland (GSAR 27-MAY-97)
236 Initial version (GSAR 20-FEB-97)