4 # Documentation at the __END__
13 # We have to do this one by one for compatibility reasons.
14 # If an arg doesn't match anything, we are supposed to return
15 # the original arg. I know, it stinks, eh?
18 my @m = doglob(1,$arg);
19 print (@m ? join("\0", sort @m) : $arg);
27 #print "doglob: ", join('|', @_), "\n";
35 next OUTER unless defined $_ and $_ ne '';
36 # if arg is within quotes strip em and do no globbing
39 if ($cond eq 'd') { push(@retval, $_) if -d $_ }
40 else { push(@retval, $_) if -e $_ }
43 if (m|^(.*)([\\/])([^\\/]*)$|) {
45 ($head, $sepchr, $tail) = ($1,$2,$3);
46 #print "div: |$head|$sepchr|$tail|\n";
47 push (@retval, $_), next OUTER if $tail eq '';
48 if ($head =~ /[*?]/) {
49 @globdirs = doglob('d', $head);
50 push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
51 next OUTER if @globdirs;
53 $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:$/;
57 # If file component has no wildcards, we can avoid opendir
59 $head = '' if $head eq '.';
60 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
62 if ($cond eq 'd') { push(@retval,$head) if -d $head }
63 else { push(@retval,$head) if -e $head }
66 opendir(D, $head) or next OUTER;
67 my @leaves = readdir D;
69 $head = '' if $head eq '.';
70 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
72 # escape regex metachars but not glob chars
73 s:([].+^\-\${}[|]):\\$1:g;
74 # and convert DOS-style wildcards to regex
78 #print "regex: '$_', head: '$head'\n";
79 my $matchsub = eval 'sub { $_[0] =~ m|^' . $_ . '$|io }';
80 warn($@), next OUTER if $@;
83 next INNER if $e eq '.' or $e eq '..';
84 next INNER if $cond eq 'd' and ! -d "$head$e";
85 push(@matched, "$head$e"), next INNER if &$matchsub($e);
87 # [DOS compatibility special case]
88 # Failed, add a trailing dot and try again, but only
89 # if name does not have a dot in it *and* pattern
90 # has a dot *and* name is shorter than 9 chars.
92 if (index($e,'.') == -1 and length($e) < 9
93 and index($_,'\\.') != -1) {
94 push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
97 push @retval, @matched if @matched;
103 # this can be used to override CORE::glob in a specific
104 # package by saying C<use File::DosGlob 'glob';> in that
108 # context (keyed by second cxix arg provided by core)
116 # glob without args defaults to $_
117 $pat = $_ unless defined $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};
148 my $callpkg = caller(0);
150 *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym}
151 if defined($sym) and $sym eq 'glob';
160 File::DosGlob - DOS like globbing and then some
162 perlglob.bat - a more capable perlglob.exe replacement
168 # override CORE::glob in current package
169 use File::DosGlob 'glob';
171 @perlfiles = glob "..\\pe?l/*.p?";
172 print <..\\pe?l/*.p?>;
174 # from the command line (overrides only in main::)
175 > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
177 > perlglob ../pe*/*p?
181 A module that implements DOS-like globbing with a few enhancements.
182 This file is also a portable replacement for perlglob.exe. It
183 is largely compatible with perlglob.exe (the M$ setargv.obj
184 version) in all but one respect--it understands wildcards in
185 directory components.
187 For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
188 that it will find something like '..\lib\File/DosGlob.pm' alright).
189 Note that all path components are case-insensitive, and that
190 backslashes and forward slashes are both accepted, and preserved.
191 You may have to double the backslashes if you are putting them in
192 literally, due to double-quotish parsing of the pattern by perl.
194 When invoked as a program, it will print null-separated filenames
197 While one may replace perlglob.exe with this, usage by overriding
198 CORE::glob via importation should be much more efficient, because
199 it avoids launching a separate process, and is therefore strongly
200 recommended. Note that it is currently possible to override
201 builtins like glob() only on a per-package basis, not "globally".
202 Thus, every namespace that wants to override glob() must explicitly
203 request the override. See L<perlsub>.
205 Extending it to csh patterns is left as an exercise to the reader.
207 =head1 EXPORTS (by request only)
213 Should probably be built into the core, and needs to stop
214 pandering to DOS habits. Needs a dose of optimizium too.
218 Gurusamy Sarathy <gsar@umich.edu>
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)