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