blib/lib/Mason/Util.pm

filter: Statementstmt 38.4% 58 / 151 Branchbran 11.6% 7 / 60 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 41.1% 14 / 34 Podpod 0.0% 0 / 19 totaltotal 28.8% 79 / 274 SCAR 61.8
CC
36
Cov
30%
combine_similar_paths
72.0
mason_canon_path
30.0
read_file
30.0
CRAP 481.4
↑ Summary blib/lib/Mason/Plugin/LvalueAttributes/Interp.pm »
1 package Mason::Util;
2 1 $Mason::Util::VERSION = '2.23';
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Class::Unload;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Fcntl qw( :DEFAULT :seek );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec::Functions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 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 1 my $Fetch_Flags = O_RDONLY | O_BINARY;
20 1 my $Store_Flags = O_WRONLY | O_CREAT | O_BINARY;
21 1 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 1 ▶if ( JSON->VERSION < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("JSON"->VERSION < 2) 0 1 1
25 0 $JSON::UnMapping = 1;
26 }
27  
28 sub can_load {
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 0 ▶ my ($class_name) = @_;
Subroutine can_load: not called CC=1 SCAR=6.9
Pod undocumented
34  
35 0 my $result;
36 try {
37 0 ▶ Class::Load::load_class($class_name);
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 $result = 1;
39 }
40 catch {
41 0 ▶ if ( /Can\'t locate .* in \@INC/ && !/Compilation failed/ ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (/Can\'t locate .* in \@INC/ and not /Compilation failed/) { } 0 0 0
Condition0%0/3
/Can\'t locate .* in \@INC/ and not /Compilation failed/
!ll&&!rl&&r
000
MC/DC0%0/2
/Can\'t locate .* in \@INC/ and not /Compilation failed/
/Can\'t locate .* in \@INC/ not /Compilation failed/
Truth table0%0/3
A and not(B)
A= /Can\'t locate .* in \@INC/B= not /Compilation failed/
ABresult
0X0
100
111
42 0 $result = 0;
43 }
44 else {
45 0 die $_;
46 }
47 0 };
48 0 return $result;
49 }
50  
51 sub catdir {
52 0 ▶ return $File_Spec_Using_Unix ? join( "/", @_ ) : File::Spec::Functions::catdir(@_);
Subroutine catdir: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$File_Spec_Using_Unix ? : 0 0 0
53 }
54  
55 sub catfile {
56 0 ▶ return $File_Spec_Using_Unix ? join( "/", @_ ) : File::Spec::Functions::catfile(@_);
Subroutine catfile: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$File_Spec_Using_Unix ? : 0 0 0
57 }
58  
59 sub checksum {
60 0 ▶ my ($str) = @_;
Subroutine checksum: not called CC=2 SCAR=17.9
Pod undocumented
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 sub combine_similar_paths {
77 0 ▶ my @paths = @_;
Subroutine combine_similar_paths: not called CC=8 SCAR=42.8
Pod undocumented
78 0 my ( @final, $current_base, @current_exts );
79 0 foreach my $path (@paths) {
80 0 ▶ if ( my ( $base, $ext ) = ( $path =~ /^(.*)\.(.*)$/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $path =~ /^(.*)\.(.*)$/) { } 0 0 0
81 0 ▶ if ( defined($current_base) && $current_base ne $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_base and $current_base ne $base) { } 0 0 0
Condition0%0/3
defined $current_base and $current_base ne $base
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current_base and $current_base ne $base
defined $current_base $current_base ne $base
Truth table0%0/3
A and B
A= defined $current_baseB= $current_base ne $base
ABresult
0X0
100
111
82 0 ▶ push(
Branch: 1 branch
BranchTrueFalseTotal
@current_exts == 1 ? : 0 0 0
83 @final,
84 "$current_base."
85 . (
86 ( @current_exts == 1 )
87 ? $current_exts[0]
88 : sprintf( '{%s}', join( ',', @current_exts ) )
89 )
90 );
91 0 @current_exts = ($ext);
92 }
93 else {
94 0 push( @current_exts, $ext );
95 }
96 0 $current_base = $base;
97 }
98 else {
99 0 push( @final, $path );
100 }
101 }
102 0 ▶ if ( defined($current_base) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_base) 0 0 0
103 0 ▶ push(
Branch: 1 branch
BranchTrueFalseTotal
@current_exts == 1 ? : 0 0 0
104 @final,
105 "$current_base."
106 . (
107 ( @current_exts == 1 )
108 ? $current_exts[0]
109 : sprintf( '{%s}', join( ',', @current_exts ) )
110 )
111 );
112 }
113 0 return @final;
114 }
115  
116 sub delete_package {
117 0 ▶ my $pkg = shift;
Subroutine delete_package: not called CC=1 SCAR=6.9
Pod undocumented
118 0 Class::Unload->unload($pkg);
119 }
120  
121 sub dump_one_line {
122 0 ▶ my ($value) = @_;
Subroutine dump_one_line: not called CC=1 SCAR=6.9
Pod undocumented
123  
124 0 return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0)->Terse(1)->Dump();
125 }
126  
127 # From File::Find::Wanted
128 sub find_wanted {
129 0 ▶ my $func = shift;
Subroutine find_wanted: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my @files;
131  
132 0 local $_;
133 0 ▶ find( sub { push @files, $File::Find::name if &$func }, @_ );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if &$func 0 0 0
134  
135 0 return @files;
136 }
137  
138 # From List::MoreUtils
139 sub first_index (&@) {
140 0 ▶ my $f = shift;
Subroutine first_index: not called CC=3 SCAR=24.8
Pod undocumented
141 0 for my $i ( 0 .. $#_ ) {
142 0 local *_ = \$_[$i];
143 0 ▶ return $i if $f->();
Branch: 1 branch
BranchTrueFalseTotal
if &$f() 0 0 0
144 }
145 0 return -1;
146 }
147  
148 sub is_absolute {
149 0 ▶ my ($path) = @_;
Subroutine is_absolute: not called CC=1 SCAR=6.9
Pod undocumented
150  
151 0 return substr( $path, 0, 1 ) eq '/';
152 }
153  
154 # Maintain compatibility with both JSON 1 and 2. Borrowed from Data::Serializer::JSON.
155 #
156 sub json_decode {
157 0 ▶ my ($text) = @_;
Subroutine json_decode: not called CC=2 SCAR=17.9
Pod undocumented
158 0 ▶ return JSON->VERSION < 2 ? JSON->new->jsonToObj($text) : JSON->new->decode($text);
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->VERSION < 2 ? : 0 0 0
159 }
160  
161 sub json_encode {
162 0 ▶ my ($data) = @_;
Subroutine json_encode: not called CC=2 SCAR=17.9
Pod undocumented
163 0 ▶ return JSON->VERSION < 2 ? JSON->new->objToJson($data) : JSON->new->utf8->encode($data);
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->VERSION < 2 ? : 0 0 0
164 }
165  
166 sub mason_canon_path {
167  
168 # Like File::Spec::canonpath but with a few fixes.
169 #
170 0 ▶ my $path = shift;
Subroutine mason_canon_path: not called CC=5 SCAR=34.0
Pod undocumented
171 0 $path =~ s|/+|/|g; # xx////yy -> xx/yy
172 0 $path =~ s|(?:/\.)+/|/|g; # xx/././yy -> xx/yy
173 {
174 0 ▶ $path =~ s|^(?:\./)+||s unless $path eq "./"; # ./xx -> xx
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "./" 0 0 0
175 0 $path =~ s|^/(?:\.\./)+|/|s; # /../../xx -> xx
176 0 ▶ $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "/" 0 0 0
177 0 ▶ $path =~ s|/[^/]+/\.\.$|| && redo; # /xx/.. -> /
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s[/[^/]+/\.\.$][] and redo 0 0 0
178 0 ▶ $path =~ s|[^/]+/\.\./|| && redo; # /xx/../yy -> /yy
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s[[^/]+/\.\./][] and redo 0 0 0
179 }
180 0 return $path;
181 }
182  
183 sub read_file {
184 0 ▶ my ($file) = @_;
Subroutine read_file: not called CC=5 SCAR=34.0
Pod undocumented
185  
186 # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed
187 #
188 0 my $buf = "";
189 0 my $read_fh;
190 0 ▶ unless ( sysopen( $read_fh, $file, $Fetch_Flags ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $read_fh, $file, $Fetch_Flags) 0 0 0
191 0 croak "read_file '$file' - sysopen: $!";
192 }
193 0 my $size_left = -s $read_fh;
194 0 while (1) {
195 0 my $read_cnt = sysread( $read_fh, $buf, $size_left, length $buf );
196 0 ▶ if ( defined $read_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_cnt) { } 0 0 0
197 0 ▶ last if $read_cnt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read_cnt == 0 0 0 0
198 0 $size_left -= $read_cnt;
199 0 ▶ last if $size_left <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size_left <= 0 0 0 0
200 }
201 else {
202 0 croak "read_file '$file' - sysread: $!";
203 }
204 }
205 0 return $buf;
206 }
207  
208 sub taint_is_on {
209 1 ▶ return ${^TAINT} ? 1 : 0;
Subroutine taint_is_on: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
${^TAINT} ? : 0 1 1
210 }
211  
212 sub touch_file {
213 0 ▶ my ($file) = @_;
Subroutine touch_file: not called CC=2 SCAR=17.9
Pod undocumented
214 0 ▶ if ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 0 0 0
215 0 my $time = time;
216 0 utime( $time, $time, $file );
217 }
218 else {
219 0 write_file( $file, "" );
220 }
221 }
222  
223 sub trim {
224 0 ▶ my ($str) = @_;
Subroutine trim: not called CC=3 SCAR=24.8
Pod undocumented
225 0 ▶ if ( defined($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 0 0 0
226 0 for ($str) { s/^\s+//; s/\s+$// }
227 }
228 0 return $str;
229 }
230  
231 # From List::MoreUtils
232 sub uniq (@) {
233 0 ▶ my %h;
Subroutine uniq: not called CC=2 SCAR=17.9
Pod undocumented
234 0 ▶ map { $h{$_}++ == 0 ? $_ : () } @_;
Branch: 1 branch
BranchTrueFalseTotal
$h{$_}++ == 0 ? : 0 0 0
235 }
236  
237 sub write_file {
238 1 ▶ my ( $file, $data, $file_create_mode ) = @_;
Subroutine write_file: called CC=6 SCAR=19.0
Pod undocumented
239  
240 1 ▶ ($file) = $file =~ /^(.*)/s if taint_is_on(); # Untaint blindly
Branch: 1 branch
BranchTrueFalseTotal
if taint_is_on() 0 1 1
241 1 ▶ $file_create_mode = oct(666) if !defined($file_create_mode);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_create_mode 1 0 1
242  
243 # Fast spew, adapted from File::Slurp::write, with unnecessary options removed
244 #
245 {
246 1 my $write_fh;
247 1 ▶ 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 1 1
248 0 croak "write_file '$file' - sysopen: $!";
249 }
250 1 my $size_left = length($data);
251 1 my $offset = 0;
252 1 ▶ 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 1 1
253 1 my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset );
254 1 ▶ unless ( defined $write_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $write_cnt) 0 1 1
255 0 croak "write_file '$file' - syswrite: $!";
256 }
257 1 $size_left -= $write_cnt;
258 1 $offset += $write_cnt;
259 } while ( $size_left > 0 );
260 1 truncate( $write_fh, sysseek( $write_fh, 0, SEEK_CUR ) )
261 }
262 }
263  
264 1 1;
↑ Summary blib/lib/Mason/Plugin/LvalueAttributes/Interp.pm »