blib/lib/FastGlob.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 175 CC 16 SCAR 39.7
CC
16
Cov
0%
glob
90.0
recurseglob
56.0
globtest
6.0
CRAP 53.2
1 #!/usr/local/bin/perl
2 package FastGlob;
3  
4 require 5.005;
5  
6 =head1 NAME
7  
8 FastGlob - A faster glob() implementation
9  
10 =head1 SYNOPSIS
11  
12 use FastGlob qw(glob);
13 @list = &glob('*.c');
14  
15 =head1 DESCRIPTION
16  
17 This module implements globbing in perl, rather than forking a csh.
18 This is faster than the built-in glob() call, and more robust (on
19 many platforms, csh chokes on C<echo *> if too many files are in the
20 directory.)
21  
22 There are several module-local variables that can be set for
23 alternate environments, they are listed below with their (UNIX-ish)
24 defaults.
25  
26 $FastGlob::dirsep = '/'; # directory path separator
27 $FastGlob::rootpat = '\A\Z'; # root directory prefix pattern
28 $FastGlob::curdir = '.'; # name of current directory in dir
29 $FastGlob::parentdir = '..'; # name of parent directory in dir
30 $FastGlob::hidedotfiles = 1; # hide filenames starting with .
31  
32 So for MS-DOS for example, you could set these to:
33  
34 $FastGlob::dirsep = '\\'; # directory path separator
35 $FastGlob::rootpat = '[A-Z]:'; # <Drive letter><colon> pattern
36 $FastGlob::curdir = '.'; # name of current directory in dir
37 $FastGlob::parentdir = '..'; # name of parent directory in dir
38 $FastGlob::hidedotfiles = 0; # hide filenames starting with .
39  
40 And for MacOS to:
41  
42 $FastGlob::dirsep = ':'; # directory path separator
43 $FastGlob::rootpat = '\A\Z'; # root directory prefix pattern
44 $FastGlob::curdir = '.'; # name of current directory in dir
45 $FastGlob::parentdir = '..'; # name of parent directory in dir
46 $FastGlob::hidedotfiles = 0; # hide filenames starting with .
47  
48 =head1 INSTALLATION
49  
50 Copy this module to the Perl 5 Library directory.
51  
52 =head1 COPYRIGHT
53  
54 Copyright (c) 1997-1999 Marc Mengel. All rights reserved.
55  
56 This library is free software; you can redistribute it and/or modify it under
57 the same terms as Perl itself.
58  
59 =head1 AUTHOR
60  
61 Marc Mengel E<lt>F<mengel@fnal.gov>E<gt>
62  
63 =head1 REVISIONS
64  
65 =over 4
66  
67 =item Brad Appleton E<lt>F<bradapp@enteract.com>E<gt> -- v1.2 Feb 1999
68  
69 Modified to use qr// (and some other minor speedups) and made callable
70 as a standalone script
71  
72 =item Marc Mengel E<lt>F<mengel@fnal.gov>E<gt> -- v1.3 Oct 2000
73  
74 Bugfixes for
75 empty components (e.g. C<foo//bar>), and
76 adjacent wildcards (e.g. x?? y** or x?*).
77  
78 =back
79  
80 =cut
81  
82 use Exporter ();
83 $VERSION = 1.2;
84 @ISA = qw(Exporter);
85 @EXPORT = qw(&glob);
86 @EXPORT_OK = qw(dirsep rootpat curdir parentidr hidedotfiles);
87  
88 use 5.004;
89 use strict; # be good
90 use vars qw($dirsep $rootpat $curdir $parentdir $hidedotfiles $verbose);
91  
92 # platform specifics
93  
94 $dirsep = '/';
95 $rootpat= '\A\Z';
96 $curdir = '.';
97 $parentdir = '..';
98 $hidedotfiles = 1;
99 $verbose = 0;
100  
101 #
102 # recursively wildcard expand a list of strings
103 #
104  
105 sub glob($) {
106  
107 my @res;
108 my $part;
109 my $found1;
110 my $out;
111 my $bracepat = qr(\{([^\{\}]*)\});
112  
113 # deal with {xxx,yyy,zzz}
114 @res = ();
115 $found1 = 1;
116 while ($found1) {
117 $found1 = 0;
118 for (@_) {
119 if ( m{$bracepat} ) {
120 foreach $part (split(',',$1)) {
121 $out = $_;
122 $out =~ s/$bracepat/$part/;
123 push(@res, $out);
124 }
125 $found1 = 1;
126 } else {
127 push(@res, $_);
128 }
129 }
130 @_ = @res;
131 @res = ();
132 }
133  
134  
135 for (@_) {
136 # check for and do tilde expansion
137 if ( /^\~([^${dirsep}]*)/ ) {
138 my $usr = $1;
139 my $usrdir = ( ($1 eq "") ? getpwuid($<) : getpwnam($usr) )[7];
140 if ($usrdir ne "" ) {
141 s/^\~\Q$usr\E/$usrdir/;
142 push(@res, $_);
143 }
144 } else {
145 push(@res, $_);
146 }
147 }
148 @_ = @res;
149 @res = ();
150  
151 for (@_) {
152 # if there's no wildcards, just return it
153 unless (/(^|[^\\])[*?\[\]{}]/) {
154 push (@res, $_);
155 next;
156 }
157  
158 # Make the glob into a regexp
159 # escape + , and |
160 s/([+.|])/\\$1/go;
161  
162 # handle * and ?
163 s/(?<!\\)(\*)/.*/go;
164 s/(?<!\\)(\?)/./go;
165  
166 # deal with dot files
167 if ( $hidedotfiles ) {
168 s/(\A|$dirsep)\.\*/$1(?:[^.].*)?/go;
169 s/(\A|$dirsep)\./$1\[\^.\]/go;
170 s/(\A|$dirsep)\[\^([^].]*)\]/$1\[\^\\.$2\]/go;
171 }
172  
173 # debugging
174 print "regexp is $_\n" if ($verbose);
175  
176 # now split it into directory components
177 my @comps = split($dirsep);
178  
179 if ( $comps[0] =~ /($rootpat)/ ) {
180 shift(@comps);
181 push(@res, &recurseglob( "$1$dirsep", "$1$dirsep" , @comps ));
182 }
183 else {
184 push(@res, &recurseglob( $curdir, '' , @comps ));
185 }
186 }
187 return sort(@res);
188 }
189  
190 sub recurseglob($ $ @) {
191 my($dir, $dirname, @comps) = @_;
192 my(@res) = ();
193 my($re, $anymatches, @names);
194  
195  
196 if ( @comps == 0 ) {
197 # bottom of recursion, just return the path
198 chop($dirname); # always has gratiutous trailing slash
199 @res = ($dirname);
200 } elsif ($comps[0] eq '') {
201 shift(@comps);
202 unshift(@res, &recurseglob( "$dir$dirsep",
203 "$dirname$dirsep",
204 @comps ));
205 } else {
206 $re = '\A' . shift(@comps) . '\Z';
207  
208 # slurp in the directory
209 opendir(HANDLE, $dir);
210 @names = readdir(HANDLE);
211 closedir(HANDLE);
212  
213 # look for matches, and if you find one, glob the rest of the
214 # components. We eval the loop so the regexp gets compiled in,
215 # making searches on large directories faster.
216 $anymatches = 0;
217 print "component re is qr($re)\n" if ($verbose);
218 my $regex = qr($re);
219 foreach (@names) {
220 print "considering |$_|\n" if ($verbose);
221 if ( m{$regex} ) {
222 if ( $#comps > -1 ) {
223 unshift(@res, &recurseglob( "$dir$dirsep$_",
224 "$dirname$_$dirsep",
225 @comps ));
226 } else {
227 unshift(@res, "$dirname$_" );
228 }
229 $anymatches = 1;
230 }
231 }
232 }
233 return @res;
234 }
235  
236 sub globtest(;$) {
237 my $fh = shift || \*ARGV;
238 my(@t0, @t1, $udiffm, $sdiffm, $udiffg, $sdiffg, @list1, @list2);
239 local($,);
240 my $res = 1;
241  
242 $, = " ";
243 while (<$fh>) {
244 chomp;
245 print "pattern: $_\n";
246  
247 @t0 = times();
248 @list1 = &glob($_);
249 @t1 = times();
250 $udiffm = ($t1[0] + $t1[2]) - ($t0[0] + $t0[2]);
251 $sdiffm = ($t1[1] + $t1[3]) - ($t0[1] + $t0[3]);
252  
253 @t0 = times();
254 @list2 = glob($_);
255 @t1 = times();
256 $udiffg = ($t1[0] + $t1[2]) - ($t0[0] + $t0[2]);
257 $sdiffg = ($t1[1] + $t1[3]) - ($t0[1] + $t0[3]);
258  
259 if ( join(' ',sort(@list1)) ne join(' ',sort(@list2))) {
260 print "XXX results mismatch:\n";
261 print @list1, "\n";
262 print @list2, "\n";
263 $res = 0;
264 } else {
265 print "results match:\n";
266 print @list1, "\n";
267 }
268 print "mine: [${udiffm}u\t${sdiffm}s]\n";
269 print "glob: [${udiffg}u\t${sdiffg}s]\n";
270 }
271 }
272  
273 unless (caller) {
274 if (globtest()) {
275 exit 0;
276 } else {
277 exit 1;
278 }
279 }
280  
281 1;
282 __END__