4 #Can't use string ("main::glob") as a symbol ref while "strict refs" in use at /usr/lib/perl5/5.005/File/DosGlob.pm line 191.
7 # Documentation at the __END__
10 package File::DosGlob;
12 our $VERSION = '1.00';
18 #print "doglob: ", join('|', @_), "\n";
26 next OUTER unless defined $pat and $pat ne '';
27 # if arg is within quotes strip em and do no globbing
28 if ($pat =~ /^"(.*)"\z/s) {
30 if ($cond eq 'd') { push(@retval, $pat) if -d $pat }
31 else { push(@retval, $pat) if -e $pat }
34 # wildcards with a drive prefix such as h:*.pm must be changed
35 # to h:./*.pm to expand correctly
36 if ($pat =~ m|^([A-Za-z]:)[^/\\]|s) {
37 substr($_,0,2) = $1 . "./";
39 if ($pat =~ m|^(.*)([\\/])([^\\/]*)\z|s) {
40 ($head, $sepchr, $tail) = ($1,$2,$3);
41 #print "div: |$head|$sepchr|$tail|\n";
42 push (@retval, $pat), next OUTER if $tail eq '';
43 if ($head =~ /[*?]/) {
44 @globdirs = doglob('d', $head);
45 push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
46 next OUTER if @globdirs;
48 $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:\z/s;
52 # If file component has no wildcards, we can avoid opendir
53 unless ($pat =~ /[*?]/) {
54 $head = '' if $head eq '.';
55 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
57 if ($cond eq 'd') { push(@retval,$head) if -d $head }
58 else { push(@retval,$head) if -e $head }
61 opendir(D, $head) or next OUTER;
62 my @leaves = readdir D;
64 $head = '' if $head eq '.';
65 $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
67 # escape regex metachars but not glob chars
68 $pat =~ s:([].+^\-\${}[|]):\\$1:g;
69 # and convert DOS-style wildcards to regex
73 #print "regex: '$pat', head: '$head'\n";
74 my $matchsub = sub { $_[0] =~ m|^$pat\z|is };
77 next INNER if $e eq '.' or $e eq '..';
78 next INNER if $cond eq 'd' and ! -d "$head$e";
79 push(@matched, "$head$e"), next INNER if &$matchsub($e);
81 # [DOS compatibility special case]
82 # Failed, add a trailing dot and try again, but only
83 # if name does not have a dot in it *and* pattern
84 # has a dot *and* name is shorter than 9 chars.
86 if (index($e,'.') == -1 and length($e) < 9
87 and index($pat,'\\.') != -1) {
88 push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
91 push @retval, @matched if @matched;
97 # this can be used to override CORE::glob in a specific
98 # package by saying C<use File::DosGlob 'glob';> in that
102 # context (keyed by second cxix arg provided by core)
110 # glob without args defaults to $_
111 $pat = $_ unless defined $pat;
115 require Text::ParseWords;
116 @pat = Text::ParseWords::parse_line('\s+',0,$pat);
122 # Mike Mestnik: made to do abc{1,2,3} == abc1 abc2 abc3.
123 # abc3 will be the original {3} (and drop the {}).
124 # abc1 abc2 will be put in @appendpat.
125 # This was just the esiest way, not nearly the best.
129 # There must be a "," I.E. abc{efg} is not what we want.
130 while ( /^(.*)(?<!\\)\{(.*?)(?<!\\)\,.*?(?<!\\)\}(.*)$/ ) {
131 my ($start, $match, $end) = ($1, $2, $3);
132 #print "Got: \n\t$start\n\t$match\n\t$end\n";
133 my $tmp = "$start$match$end";
134 while ( $tmp =~ s/^(.*?)(?<!\\)\{(?:.*(?<!\\)\,)?(.*\Q$match\E.*?)(?:(?<!\\)\,.*)?(?<!\\)\}(.*)$/$1$2$3/ ) {
135 #print "Striped: $tmp\n";
136 # these expanshions will be preformed by the original,
137 # when we call REHASH.
139 push @appendpat, ("$tmp");
140 s/^\Q$start\E(?<!\\)\{\Q$match\E(?<!\\)\,/$start\{/;
141 if ( /^\Q$start\E(?<!\\)\{(?!.*?(?<!\\)\,.*?\Q$end\E$)(.*)(?<!\\)\}\Q$end\E$/ ) {
143 #print "GOT: \n\t$start\n\t$match\n\t$end\n\n";
144 $_ = "$start$match$end";
147 #print "Sould have "GOT" vs "Got"!\n";
148 #FIXME: There should be checking for this.
149 # How or what should be done about failure is beond me.
151 if ( $#appendpat != -1
154 #FIXME: Max loop, no way! :")
166 #print join ("\n", @pat). "\n";
168 # assume global context if not provided one
169 $cxix = '_G_' unless defined $cxix;
170 $iter{$cxix} = 0 unless exists $iter{$cxix};
172 # if we're just beginning, do it all first
173 if ($iter{$cxix} == 0) {
174 $entries{$cxix} = [doglob(1,@pat)];
177 # chuck it all out, quick or slow
180 return @{delete $entries{$cxix}};
183 if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
184 return shift @{$entries{$cxix}};
187 # return undef for EOL
189 delete $entries{$cxix};
202 my $callpkg = ($sym =~ s/^GLOBAL_//s ? 'CORE::GLOBAL' : caller(0));
203 *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym} if $sym eq 'glob';
212 File::DosGlob - DOS like globbing and then some
218 # override CORE::glob in current package
219 use File::DosGlob 'glob';
221 # override CORE::glob in ALL packages (use with extreme caution!)
222 use File::DosGlob 'GLOBAL_glob';
224 @perlfiles = glob "..\\pe?l/*.p?";
225 print <..\\pe?l/*.p?>;
227 # from the command line (overrides only in main::)
228 > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
232 A module that implements DOS-like globbing with a few enhancements.
233 It is largely compatible with perlglob.exe (the M$ setargv.obj
234 version) in all but one respect--it understands wildcards in
235 directory components.
237 For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
238 that it will find something like '..\lib\File/DosGlob.pm' alright).
239 Note that all path components are case-insensitive, and that
240 backslashes and forward slashes are both accepted, and preserved.
241 You may have to double the backslashes if you are putting them in
242 literally, due to double-quotish parsing of the pattern by perl.
244 Spaces in the argument delimit distinct patterns, so
245 C<glob('*.exe *.dll')> globs all filenames that end in C<.exe>
246 or C<.dll>. If you want to put in literal spaces in the glob
247 pattern, you can escape them with either double quotes, or backslashes.
248 e.g. C<glob('c:/"Program Files"/*/*.dll')>, or
249 C<glob('c:/Program\ Files/*/*.dll')>. The argument is tokenized using
250 C<Text::ParseWords::parse_line()>, so see L<Text::ParseWords> for details
251 of the quoting rules used.
253 Extending it to csh patterns is left as an exercise to the reader.
255 =head1 EXPORTS (by request only)
261 Should probably be built into the core, and needs to stop
262 pandering to DOS habits. Needs a dose of optimizium too.
266 Gurusamy Sarathy <gsar@activestate.com>
274 Support for globally overriding glob() (GSAR 3-JUN-98)
278 Scalar context, independent iterator context fixes (GSAR 15-SEP-97)
282 A few dir-vs-file optimizations result in glob importation being
283 10 times faster than using perlglob.exe, and using perlglob.bat is
284 only twice as slow as perlglob.exe (GSAR 28-MAY-97)
288 Several cleanups prompted by lack of compatible perlglob.exe
289 under Borland (GSAR 27-MAY-97)
293 Initial version (GSAR 20-FEB-97)