home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / perlglob.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  5.3 KB  |  210 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. perl -x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
  4. goto endofperl
  5. @rem ';
  6. #!perl -w
  7. #line 8
  8.  
  9. #
  10. # Documentation at the __END__
  11. #
  12.  
  13. package File::DosGlob;
  14.  
  15. unless (caller) {
  16.     $| = 1;
  17.     while (@ARGV) {
  18.     #
  19.     # We have to do this one by one for compatibility reasons.
  20.     # If an arg doesn't match anything, we are supposed to return
  21.     # the original arg.  I know, it stinks, eh?
  22.     #
  23.     my $arg = shift;
  24.     my @m = doglob(1,$arg);
  25.     print (@m ? join("\0", sort @m) : $arg);
  26.     print "\0" if @ARGV;
  27.     }
  28. }
  29.  
  30. sub doglob {
  31.     my $cond = shift;
  32.     my @retval = ();
  33.     #print "doglob: ", join('|', @_), "\n";
  34.   OUTER:
  35.     for my $arg (@_) {
  36.         local $_ = $arg;
  37.     my @matched = ();
  38.     my @globdirs = ();
  39.     my $head = '.';
  40.     my $sepchr = '/';
  41.     next OUTER unless defined $_ and $_ ne '';
  42.     # if arg is within quotes strip em and do no globbing
  43.     if (/^"(.*)"$/) {
  44.         $_ = $1;
  45.         if ($cond eq 'd') { push(@retval, $_) if -d $_ }
  46.         else              { push(@retval, $_) if -e $_ }
  47.         next OUTER;
  48.     }
  49.     if (m|^(.*)([\\/])([^\\/]*)$|) {
  50.         my $tail;
  51.         ($head, $sepchr, $tail) = ($1,$2,$3);
  52.         #print "div: |$head|$sepchr|$tail|\n";
  53.         push (@retval, $_), next OUTER if $tail eq '';
  54.         if ($head =~ /[*?]/) {
  55.         @globdirs = doglob('d', $head);
  56.         push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
  57.             next OUTER if @globdirs;
  58.         }
  59.         $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:$/;
  60.         $_ = $tail;
  61.     }
  62.     #
  63.     # If file component has no wildcards, we can avoid opendir
  64.     unless (/[*?]/) {
  65.         $head = '' if $head eq '.';
  66.         $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
  67.         $head .= $_;
  68.         if ($cond eq 'd') { push(@retval,$head) if -d $head }
  69.         else              { push(@retval,$head) if -e $head }
  70.         next OUTER;
  71.     }
  72.     opendir(D, $head) or next OUTER;
  73.     my @leaves = readdir D;
  74.     closedir D;
  75.     $head = '' if $head eq '.';
  76.     $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
  77.  
  78.     # escape regex metachars but not glob chars
  79.     s:([].+^\-\${}[|]):\\$1:g;
  80.     # and convert DOS-style wildcards to regex
  81.     s/\*/.*/g;
  82.     s/\?/.?/g;
  83.  
  84.     #print "regex: '$_', head: '$head'\n";
  85.     my $matchsub = eval 'sub { $_[0] =~ m|^' . $_ . '$|io }';
  86.     warn($@), next OUTER if $@;
  87.       INNER:
  88.     for my $e (@leaves) {
  89.         next INNER if $e eq '.' or $e eq '..';
  90.         next INNER if $cond eq 'd' and ! -d "$head$e";
  91.         push(@matched, "$head$e"), next INNER if &$matchsub($e);
  92.         #
  93.         # [DOS compatibility special case]
  94.         # Failed, add a trailing dot and try again, but only
  95.         # if name does not have a dot in it *and* pattern
  96.         # has a dot *and* name is shorter than 9 chars.
  97.         #
  98.         if (index($e,'.') == -1 and length($e) < 9
  99.             and index($_,'\\.') != -1) {
  100.         push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
  101.         }
  102.     }
  103.     push @retval, @matched if @matched;
  104.     }
  105.     return @retval;
  106. }
  107.  
  108. #
  109. # this can be used to override CORE::glob
  110. # by saying C<use File::DosGlob 'glob';>.
  111. #
  112. sub glob { doglob(1,@_) }
  113.  
  114. sub import {
  115.     my $pkg = shift;
  116.     my $callpkg = caller(0);
  117.     my $sym = shift;
  118.     *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym} if $sym eq 'glob';
  119. }
  120.  
  121. 1;
  122.  
  123. __END__
  124.  
  125. =head1 NAME
  126.  
  127. File::DosGlob - DOS like globbing and then some
  128.  
  129. perlglob.bat - a more capable perlglob.exe replacement
  130.  
  131. =head1 SYNOPSIS
  132.  
  133.     require 5.004;
  134.     use File::DosGlob 'glob';  # override CORE::glob
  135.     @perlfiles = glob  "..\\pe?l/*.p?";
  136.     print <..\\pe?l/*.p?>;
  137.     
  138.     # from the command line
  139.     > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
  140.     
  141.     > perlglob ../pe*/*p?
  142.  
  143. =head1 DESCRIPTION
  144.  
  145. A module that implements DOS-like globbing with a few enhancements.
  146. This file is also a portable replacement for perlglob.exe.  It
  147. is largely compatible with perlglob.exe (the M$ setargv.obj
  148. version) in all but one respect--it understands wildcards in
  149. directory components.
  150.  
  151. For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
  152. that it will find something like '..\lib\File/DosGlob.pm' alright).
  153. Note that all path components are case-insensitive, and that
  154. backslashes and forward slashes are both accepted, and preserved.
  155. You may have to double the backslashes if you are putting them in
  156. literally, due to double-quotish parsing of the pattern by perl.
  157.  
  158. When invoked as a program, it will print null-separated filenames
  159. to standard output.
  160.  
  161. While one may replace perlglob.exe with this, usage by overriding
  162. CORE::glob via importation should be much more efficient, because
  163. it avoids launching a separate process, and is therefore strongly
  164. recommended.
  165.  
  166. Extending it to csh patterns is left as an exercise to the reader.
  167.  
  168. =head1 EXPORTS (by request only)
  169.  
  170. glob()
  171.  
  172. =head1 BUGS
  173.  
  174. Should probably be built into the core, and needs to stop
  175. pandering to DOS habits.  Needs a dose of optimizium too.
  176.  
  177. =head1 AUTHOR
  178.  
  179. Gurusamy Sarathy <gsar@umich.edu>
  180.  
  181. =head1 HISTORY
  182.  
  183. =over 4
  184.  
  185. =item *
  186.  
  187. A few dir-vs-file optimizations result in glob importation being
  188. 10 times faster than using perlglob.exe, and using perlglob.bat is
  189. only twice as slow as perlglob.exe (GSAR 28-MAY-97)
  190.  
  191. =item *
  192.  
  193. Several cleanups prompted by lack of compatible perlglob.exe
  194. under Borland (GSAR 27-MAY-97)
  195.  
  196. =item *
  197.  
  198. Initial version (GSAR 20-FEB-97)
  199.  
  200. =back
  201.  
  202. =head1 SEE ALSO
  203.  
  204. perl
  205.  
  206. =cut
  207.  
  208. __END__
  209. :endofperl
  210.