blib/lib/FastGlob.pm

filter: Statementstmt 92.1% 82 / 89 Branchbran 66.6% 20 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 85.0% 108 / 127 CC 21 SCAR 23.9
CC
21
Cov
86%
glob
15.3
recurseglob
8.2
BEGIN
1.0
CRAP 10.9
↑ Summary
1 #!/usr/local/bin/perl
2 package FastGlob;
3  
4 1 require 5.005;
5  
6 # ABSTRACT: A faster glob() implementation
7  
8 BEGIN {
9 1 ▶ our $VERSION = '1.4'; # VERSION: generated by DZP::OurPkgVersion
Subroutine BEGIN: called CC=1 SCAR=0.0
10 }
11  
12  
13  
14 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 @ISA = qw(Exporter);
17 1 @EXPORT = qw(&glob);
18 1 @EXPORT_OK = qw(dirsep rootpat curdir parentidr hidedotfiles);
19  
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict; # be good
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # platform specifics
24  
25 1 our $dirsep = '/';
26 1 our $rootpat= '\A\Z';
27 1 our $curdir = '.';
28 1 our $parentdir = '..';
29 1 our $hidedotfiles = 1;
30 1 our $verbose = 0;
31  
32 #
33 # recursively wildcard expand a list of strings
34 #
35  
36 sub glob($) {
37  
38 18 ▶ my @res;
Subroutine glob: called CC=14 SCAR=27.3
Pod undocumented
39 my $part;
40 18 my $found1;
41 18 my $out;
42 18 my $bracepat = qr(\{([^\{\}]*)\});
43  
44 # deal with {xxx,yyy,zzz}
45 18 @res = ();
46 18 $found1 = 1;
47 18 ▶ while ($found1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found1) 22 18 40
48 22 $found1 = 0;
49 22 for (@_) {
50 29 ▶ if ( m{$bracepat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$bracepat/) { } 5 24 29
51 5 foreach $part (split(',',$1)) {
52 11 $out = $_;
53 11 $out =~ s/$bracepat/$part/;
54 11 push(@res, $out);
55 }
56 5 $found1 = 1;
57 } else {
58 24 push(@res, $_);
59 }
60 }
61 22 @_ = @res;
62 22 @res = ();
63 }
64  
65  
66 18 for (@_) {
67 # check for and do tilde expansion
68 24 ▶ if ( /^\~([^${dirsep}]*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\~([^$FastGlob::dirsep]*)/) { } 0 24 24
69 0 my $usr = $1;
70 0 ▶ my $usrdir = ( ($1 eq "") ? getpwuid($<) : getpwnam($usr) )[7];
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '' ? : 0 0 0
71 0 ▶ if ($usrdir ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usrdir ne "") 0 0 0
72 0 s/^\~\Q$usr\E/$usrdir/;
73 0 push(@res, $_);
74 }
75 } else {
76 24 push(@res, $_);
77 }
78 }
79 18 @_ = @res;
80 18 @res = ();
81  
82 18 for (@_) {
83 # if there's no wildcards, just return it
84 24 ▶ unless (/(^|[^\\])[*?\[\]{}]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/(^|[^\\])[*?\[\]{}]/) 5 19 24
85 5 push (@res, $_);
86 5 next;
87 }
88  
89 # Make the glob into a regexp
90 # escape + , and |
91 19 s/([+.|])/\\$1/go;
92  
93 # handle * and ?
94 19 s/(?<!\\)(\*)/.*/go;
95 19 s/(?<!\\)(\?)/./go;
96  
97 # deal with dot files
98 19 ▶ if ( $hidedotfiles ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FastGlob::hidedotfiles) 19 0 19
99 19 s/(\A|$dirsep)\.\*/$1(?:[^.].*)?/go;
100 19 s/(\A|$dirsep)\./$1\[\^.\]/go;
101 19 s/(\A|$dirsep)\[\^([^].]*)\]/$1\[\^\\.$2\]/go;
102 }
103  
104 # debugging
105 19 ▶ print "regexp is $_\n" if ($verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $FastGlob::verbose 0 19 19
106  
107 # now split it into directory components
108 19 my @comps = split($dirsep);
109  
110 19 ▶ if ( $comps[0] =~ /($rootpat)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comps[0] =~ /($FastGlob::rootpat)/) { } 3 16 19
111 3 shift(@comps);
112 3 push(@res, &recurseglob( "$1$dirsep", "$1$dirsep" , @comps ));
113 }
114 else {
115 16 push(@res, &recurseglob( $curdir, '' , @comps ));
116 }
117 }
118 18 return sort(@res);
119 }
120  
121 sub recurseglob($ $ @) {
122 29 ▶ my($dir, $dirname, @comps) = @_;
Subroutine recurseglob: called CC=8 SCAR=21.0
Pod undocumented
123 29 my(@res) = ();
124 29 my($re, $anymatches, @names);
125  
126  
127 29 ▶ if ( @comps == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@comps == 0) { } 0 29 29
elsif ($comps[0] eq '') { } 1 28 29
128 # bottom of recursion, just return the path
129 0 chop($dirname); # always has gratiutous trailing slash
130 0 @res = ($dirname);
131 } elsif ($comps[0] eq '') {
132 1 shift(@comps);
133 1 unshift(@res, &recurseglob( "$dir$dirsep",
134 "$dirname$dirsep",
135 @comps ));
136 } else {
137 28 $re = '\A' . shift(@comps) . '\Z';
138  
139 # slurp in the directory
140 28 opendir(HANDLE, $dir);
141 28 @names = readdir(HANDLE);
142 28 closedir(HANDLE);
143  
144 # look for matches, and if you find one, glob the rest of the
145 # components. We eval the loop so the regexp gets compiled in,
146 # making searches on large directories faster.
147 28 $anymatches = 0;
148 28 ▶ print "component re is qr($re)\n" if ($verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $FastGlob::verbose 0 28 28
149 28 my $regex = qr($re);
150 28 foreach (@names) {
151 508 ▶ print "considering |$_|\n" if ($verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $FastGlob::verbose 0 508 508
152 508 ▶ if ( m{$regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$regex/) 52 456 508
153 52 ▶ if ( $#comps > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#comps > -1) { } 9 43 52
154 9 unshift(@res, &recurseglob( "$dir$dirsep$_",
155 "$dirname$_$dirsep",
156 @comps ));
157 } else {
158 43 unshift(@res, "$dirname$_" );
159 }
160 52 $anymatches = 1;
161 }
162 }
163 }
164 29 return @res;
165 }
166  
167 1 1;
168  
169 __END__
↑ Summary