blib/lib/Mason/Util.pm

filter: Statementstmt 90.0% 136 / 151 Branchbran 65.0% 39 / 60 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 97.0% 33 / 34 Podpod 0.0% 0 / 19 totaltotal 78.1% 214 / 274 CC 36 SCAR 12.9
CC
36
Cov
82%
combine_similar_paths
8.0
write_file
6.7
checksum
6.0
CRAP 3.6
« blib/lib/Mason/t/ResolveURI.pm ↑ Summary blib/lib/Mason/PluginManager.pm »
1 package Mason::Util;
2 23 $Mason::Util::VERSION = '2.24';
3 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use Class::Unload;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Fcntl qw( :DEFAULT :seek );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use File::Spec::Functions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 23 our @EXPORT_OK =
17 qw(can_load catdir catfile checksum combine_similar_paths dump_one_line find_wanted first_index is_absolute json_encode json_decode mason_canon_path read_file taint_is_on touch_file trim uniq write_file);
18  
19 23 my $Fetch_Flags = O_RDONLY | O_BINARY;
20 23 my $Store_Flags = O_WRONLY | O_CREAT | O_BINARY;
21 23 my $File_Spec_Using_Unix = $File::Spec::ISA[0] eq 'File::Spec::Unix';
22  
23 # Map null, true and false to real Perl values
24 23 ▶if ( JSON->VERSION < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("JSON"->VERSION < 2) 0 23 23
25 0 $JSON::UnMapping = 1;
26 }
27  
28 1688 ▶sub can_load {
Subroutine can_load: called CC=1 SCAR=0.2
Pod undocumented
29  
30 # Load $class_name if possible. Return 1 if successful, 0 if it could not be
31 # found, and rethrow load error (other than not found).
32 #
33 1688 my ($class_name) = @_;
34  
35 1688 my $result;
36 1688 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1688 Class::Load::load_class($class_name);
38 1211 $result = 1;
39 }
40 477 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=14.2
41 477 ▶ if ( /Can\'t locate .* in \@INC/ && !/Compilation failed/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/Can\'t locate .* in \@INC/ and not /Compilation failed/) { } 477 0 477
Condition33%1/3
/Can\'t locate .* in \@INC/ and not /Compilation failed/
!ll&&!rl&&r
00477
MC/DC0%0/2
/Can\'t locate .* in \@INC/ and not /Compilation failed/
/Can\'t locate .* in \@INC/ not /Compilation failed/
Truth table33%1/3
A and not(B)
A= /Can\'t locate .* in \@INC/B= not /Compilation failed/
ABresult
0X0
100
111
42 477 $result = 0;
43 }
44 else {
45 0 die $_;
46 }
47 1688 };
48 1688 return $result;
49 }
50  
51 272 ▶sub catdir {
Subroutine catdir: called CC=2 SCAR=7.6
Pod undocumented
52 272 ▶ return $File_Spec_Using_Unix ? join( "/", @_ ) : File::Spec::Functions::catdir(@_);
Branch: 1 branch
BranchTrueFalseTotal
$File_Spec_Using_Unix ? : 272 0 272
53 }
54  
55 272 ▶sub catfile {
Subroutine catfile: called CC=2 SCAR=7.6
Pod undocumented
56 272 ▶ return $File_Spec_Using_Unix ? join( "/", @_ ) : File::Spec::Functions::catfile(@_);
Branch: 1 branch
BranchTrueFalseTotal
$File_Spec_Using_Unix ? : 272 0 272
57 }
58  
59 0 ▶sub checksum {
Subroutine checksum: not called CC=2 SCAR=17.9
Pod undocumented
60 0 my ($str) = @_;
61  
62 # Adler32 algorithm
63 0 my $s1 = 1;
64 0 my $s2 = 1;
65 0 for my $c ( unpack( "C*", $str ) ) {
66 0 $s1 = ( $s1 + $c ) % 65521;
67 0 $s2 = ( $s2 + $s1 ) % 65521;
68 }
69 0 return ( $s2 << 16 ) + $s1;
70 }
71  
72 # Convert /foo/bar.m, /foo/bar.pm, /foo.m, /foo.pm to
73 # /foo/bar.{m,pm}, /foo.{m,pm}. I have no idea why this takes
74 # so much code.
75 #
76 18 ▶sub combine_similar_paths {
Subroutine combine_similar_paths: called CC=8 SCAR=20.8
Pod undocumented
77 18 my @paths = @_;
78 18 my ( @final, $current_base, @current_exts );
79 18 foreach my $path (@paths) {
80 220 ▶ if ( my ( $base, $ext ) = ( $path =~ /^(.*)\.(.*)$/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $path =~ /^(.*)\.(.*)$/) { } 211 9 220
81 211 ▶ if ( defined($current_base) && $current_base ne $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_base and $current_base ne $base) { } 91 120 211
Condition100%3/3
defined $current_base and $current_base ne $base
!ll&&!rl&&r
1810291
MC/DC100%2/2
defined $current_base and $current_base ne $base
defined $current_base $current_base ne $base
Truth table100%3/3
A and B
A= defined $current_baseB= $current_base ne $base
ABresult
0X0
100
111
82 91 ▶ push(
Branch: 1 branch
BranchTrueFalseTotal
@current_exts == 1 ? : 5 86 91
83 @final,
84 "$current_base."
85 . (
86 ( @current_exts == 1 )
87 ? $current_exts[0]
88 : sprintf( '{%s}', join( ',', @current_exts ) )
89 )
90 );
91 91 @current_exts = ($ext);
92 }
93 else {
94 120 push( @current_exts, $ext );
95 }
96 211 $current_base = $base;
97 }
98 else {
99 9 push( @final, $path );
100 }
101 }
102 18 ▶ if ( defined($current_base) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_base) 18 0 18
103 18 ▶ push(
Branch: 1 branch
BranchTrueFalseTotal
@current_exts == 1 ? : 2 16 18
104 @final,
105 "$current_base."
106 . (
107 ( @current_exts == 1 )
108 ? $current_exts[0]
109 : sprintf( '{%s}', join( ',', @current_exts ) )
110 )
111 );
112 }
113 18 return @final;
114 }
115  
116 227 ▶sub delete_package {
Subroutine delete_package: called CC=1 SCAR=0.0
Pod undocumented
117 227 my $pkg = shift;
118 227 Class::Unload->unload($pkg);
119 }
120  
121 13 ▶sub dump_one_line {
Subroutine dump_one_line: called CC=1 SCAR=0.0
Pod undocumented
122 13 my ($value) = @_;
123  
124 13 return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0)->Terse(1)->Dump();
125 }
126  
127 # From File::Find::Wanted
128 6 ▶sub find_wanted {
Subroutine find_wanted: called CC=1 SCAR=0.0
Pod undocumented
129 6 my $func = shift;
130 6 my @files;
131  
132 6 local $_;
133 6 ▶ find( sub { push @files, $File::Find::name if &$func }, @_ );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &$func 14 10 24
134  
135 6 return @files;
136 }
137  
138 # From List::MoreUtils
139 468 ▶sub first_index (&@) {
Subroutine first_index: called CC=3 SCAR=11.0
Pod undocumented
140 468 my $f = shift;
141 468 for my $i ( 0 .. $#_ ) {
142 885 local *_ = \$_[$i];
143 885 ▶ return $i if $f->();
Branch: 1 branch
BranchTrueFalseTotal
if &$f() 47 838 885
144 }
145 421 return -1;
146 }
147  
148 1478 ▶sub is_absolute {
Subroutine is_absolute: called CC=1 SCAR=0.0
Pod undocumented
149 1478 my ($path) = @_;
150  
151 1478 return substr( $path, 0, 1 ) eq '/';
152 }
153  
154 # Maintain compatibility with both JSON 1 and 2. Borrowed from Data::Serializer::JSON.
155 #
156 14 ▶sub json_decode {
Subroutine json_decode: called CC=2 SCAR=7.2
Pod undocumented
157 14 my ($text) = @_;
158 14 ▶ return JSON->VERSION < 2 ? JSON->new->jsonToObj($text) : JSON->new->decode($text);
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->VERSION < 2 ? : 0 14 14
159 }
160  
161 6 ▶sub json_encode {
Subroutine json_encode: called CC=2 SCAR=7.2
Pod undocumented
162 6 my ($data) = @_;
163 6 ▶ return JSON->VERSION < 2 ? JSON->new->objToJson($data) : JSON->new->utf8->encode($data);
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->VERSION < 2 ? : 0 6 6
164 }
165  
166 1295 ▶sub mason_canon_path {
Subroutine mason_canon_path: called CC=5 SCAR=16.3
Pod undocumented
167  
168 # Like File::Spec::canonpath but with a few fixes.
169 #
170 1295 my $path = shift;
171 1295 $path =~ s|/+|/|g; # xx////yy -> xx/yy
172 1295 $path =~ s|(?:/\.)+/|/|g; # xx/././yy -> xx/yy
173 {
174 1295 ▶ $path =~ s|^(?:\./)+||s unless $path eq "./"; # ./xx -> xx
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "./" 1297 0 1297
175 1297 $path =~ s|^/(?:\.\./)+|/|s; # /../../xx -> xx
176 1297 ▶ $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "/" 1297 0 1297
177 1297 ▶ $path =~ s|/[^/]+/\.\.$|| && redo; # /xx/.. -> /
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s[/[^/]+/\.\.$][] and redo 0 1297 1297
178 1297 ▶ $path =~ s|[^/]+/\.\./|| && redo; # /xx/../yy -> /yy
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s[[^/]+/\.\./][] and redo 2 1295 1297
179 }
180 1295 return $path;
181 }
182  
183 241 ▶sub read_file {
Subroutine read_file: called CC=5 SCAR=17.1
Pod undocumented
184 241 my ($file) = @_;
185  
186 # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed
187 #
188 241 my $buf = "";
189 241 my $read_fh;
190 241 ▶ unless ( sysopen( $read_fh, $file, $Fetch_Flags ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $read_fh, $file, $Fetch_Flags) 0 241 241
191 0 croak "read_file '$file' - sysopen: $!";
192 }
193 241 my $size_left = -s $read_fh;
194 241 while (1) {
195 241 my $read_cnt = sysread( $read_fh, $buf, $size_left, length $buf );
196 241 ▶ if ( defined $read_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_cnt) { } 241 0 241
197 241 ▶ last if $read_cnt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read_cnt == 0 0 241 241
198 241 $size_left -= $read_cnt;
199 241 ▶ last if $size_left <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size_left <= 0 241 0 241
200 }
201 else {
202 0 croak "read_file '$file' - sysread: $!";
203 }
204 }
205 241 return $buf;
206 }
207  
208 1046 ▶sub taint_is_on {
Subroutine taint_is_on: called CC=2 SCAR=7.6
Pod undocumented
209 1046 ▶ return ${^TAINT} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
${^TAINT} ? : 0 1046 1046
210 }
211  
212 2 ▶sub touch_file {
Subroutine touch_file: called CC=2 SCAR=8.4
Pod undocumented
213 2 my ($file) = @_;
214 2 ▶ if ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 0 2 2
215 0 my $time = time;
216 0 utime( $time, $time, $file );
217 }
218 else {
219 2 write_file( $file, "" );
220 }
221 }
222  
223 885 ▶sub trim {
Subroutine trim: called CC=3 SCAR=11.0
Pod undocumented
224 885 my ($str) = @_;
225 885 ▶ if ( defined($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 848 37 885
226 848 for ($str) { s/^\s+//; s/\s+$// }
227 }
228 885 return $str;
229 }
230  
231 # From List::MoreUtils
232 1370 ▶sub uniq (@) {
Subroutine uniq: called CC=2 SCAR=6.9
Pod undocumented
233 1370 my %h;
234 1370 ▶ map { $h{$_}++ == 0 ? $_ : () } @_;
Branch: 1 branch
BranchTrueFalseTotal
$h{$_}++ == 0 ? : 1301 752 2053
235 }
236  
237 528 ▶sub write_file {
Subroutine write_file: called CC=6 SCAR=19.0
Pod undocumented
238 528 my ( $file, $data, $file_create_mode ) = @_;
239  
240 528 ▶ ($file) = $file =~ /^(.*)/s if taint_is_on(); # Untaint blindly
Branch: 1 branch
BranchTrueFalseTotal
if taint_is_on() 0 528 528
241 528 ▶ $file_create_mode = oct(666) if !defined($file_create_mode);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_create_mode 528 0 528
242  
243 # Fast spew, adapted from File::Slurp::write, with unnecessary options removed
244 #
245 {
246 528 my $write_fh;
247 528 ▶ unless ( sysopen( $write_fh, $file, $Store_Flags, $file_create_mode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $write_fh, $file, $Store_Flags, $file_create_mode) 0 528 528
248 0 croak "write_file '$file' - sysopen: $!";
249 }
250 528 my $size_left = length($data);
251 528 my $offset = 0;
252 528 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$size_left > 0 and do { do { my $write_cnt = syswrite($write_fh, $data, $size_left, $offset); unless (defined $write_cnt) { croak("write_file '${file}' - syswrite: $!"); } ; $size_left -= $write_cnt; $offset += $write_cnt } } 0 528 528
253 528 my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset );
254 528 ▶ unless ( defined $write_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $write_cnt) 0 528 528
255 0 croak "write_file '$file' - syswrite: $!";
256 }
257 528 $size_left -= $write_cnt;
258 528 $offset += $write_cnt;
259 } while ( $size_left > 0 );
260 528 truncate( $write_fh, sysseek( $write_fh, 0, SEEK_CUR ) )
261 }
262 }
263  
264 23 1;
« blib/lib/Mason/t/ResolveURI.pm ↑ Summary blib/lib/Mason/PluginManager.pm »