CHI/Util.pm

filter: Statementstmt 95.4% 105 / 110 Branchbran 65.3% 17 / 26 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 96.1% 25 / 26 Podpod 0.0% 0 / 11 totaltotal 82.9% 156 / 188 CC 17 SCAR 10.0
CC
17
Cov
88%
read_file
5.5
write_file
5.2
__ANON__
4.1
CRAP 2.7
« CHI/Driver/Role/HasSubcaches.pm ↑ Summary CHI/CacheObject.pm »
1 package CHI::Util;
2 20 $CHI::Util::VERSION = '0.61';
3 20 ▶use Carp qw( croak longmess );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Fcntl qw( :DEFAULT );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use File::Spec::Functions qw(catdir catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Time::Duration::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 20 our @EXPORT_OK = qw(
17 can_load
18 dump_one_line
19 fast_catdir
20 fast_catfile
21 has_moose_class
22 json_decode
23 json_encode
24 parse_duration
25 parse_memory_size
26 read_file
27 read_dir
28 unique_id
29 write_file
30 );
31  
32 20 my $Fetch_Flags = O_RDONLY | O_BINARY;
33 20 my $Store_Flags = O_WRONLY | O_CREAT | O_BINARY;
34  
35 49 ▶sub can_load {
Subroutine can_load: called CC=1 SCAR=0.2
Pod undocumented
36  
37 # Load $class_name if possible. Return 1 if successful, 0 if it could not be
38 # found, and rethrow load error (other than not found).
39 #
40 49 my ($class_name) = @_;
41  
42 49 my $result;
43 49 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 49 require_module($class_name);
45 21 $result = 1;
46 }
47 28 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=14.2
48 28 ▶ if ( /Can\'t locate .* in \@INC/ && !/Compilation failed/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/Can\'t locate .* in \@INC/ and not /Compilation failed/) { } 28 0 28
Condition33%1/3
/Can\'t locate .* in \@INC/ and not /Compilation failed/
!ll&&!rl&&r
0028
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
49 28 $result = 0;
50 }
51 else {
52 0 die $_;
53 }
54 49 };
55 49 return $result;
56 }
57  
58 7 ▶sub dump_one_line {
Subroutine dump_one_line: called CC=1 SCAR=0.0
Pod undocumented
59 7 my ($value) = @_;
60  
61 7 return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0)
62 ->Terse(1)->Dump();
63 }
64  
65 # Simplified read_dir cribbed from File::Slurp
66 46 ▶sub read_dir {
Subroutine read_dir: called CC=3 SCAR=11.0
Pod undocumented
67 46 my ($dir) = @_;
68  
69 ## no critic (RequireInitializationForLocalVars)
70 46 local *DIRH;
71 46 ▶ opendir( DIRH, $dir ) or croak "cannot open '$dir': $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIRH, $dir or croak("cannot open '${dir}': $!") 46 0 46
72 46 ▶ return grep { $_ ne "." && $_ ne ".." } readdir(DIRH);
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
4646186
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
73 }
74  
75 5406 ▶sub read_file {
Subroutine read_file: called CC=5 SCAR=17.1
Pod undocumented
76 5406 my ($file) = @_;
77  
78 # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed
79 #
80 5406 my $buf = "";
81 5406 my $read_fh;
82 5406 ▶ unless ( sysopen( $read_fh, $file, $Fetch_Flags ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $read_fh, $file, $Fetch_Flags) 0 5406 5406
83 0 croak "read_file '$file' - sysopen: $!";
84 }
85 5406 my $size_left = -s $read_fh;
86 5406 while (1) {
87 5406 my $read_cnt = sysread( $read_fh, $buf, $size_left, length $buf );
88 5406 ▶ if ( defined $read_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_cnt) { } 5406 0 5406
89 5406 ▶ last if $read_cnt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read_cnt == 0 0 5406 5406
90 5406 $size_left -= $read_cnt;
91 5406 ▶ last if $size_left <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size_left <= 0 5406 0 5406
92 }
93 else {
94 0 croak "read_file '$file' - sysread: $!";
95 }
96 }
97 5406 return $buf;
98 }
99  
100 2225 ▶sub write_file {
Subroutine write_file: called CC=5 SCAR=16.4
Pod undocumented
101 2225 my ( $file, $data, $file_create_mode ) = @_;
102 2225 ▶ $file_create_mode = oct(666) if !defined($file_create_mode);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_create_mode 0 2225 2225
103  
104 # Fast spew, adapted from File::Slurp::write, with unnecessary options removed
105 #
106 {
107 2225 my $write_fh;
108 2225 ▶ unless ( sysopen( $write_fh, $file, $Store_Flags, $file_create_mode ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $write_fh, $file, $Store_Flags, $file_create_mode) 2 2223 2225
109 {
110 2 croak "write_file '$file' - sysopen: $!";
111 }
112 2223 my $size_left = length($data);
113 2223 my $offset = 0;
114 2223 ▶ 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 2223 2223
115 2223 my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset );
116 2223 ▶ unless ( defined $write_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $write_cnt) 0 2223 2223
117 0 croak "write_file '$file' - syswrite: $!";
118 }
119 2223 $size_left -= $write_cnt;
120 2223 $offset += $write_cnt;
121 } while ( $size_left > 0 );
122 }
123 }
124  
125 {
126  
127 # For efficiency, use Data::UUID to generate an initial unique id, then suffix it to
128 # generate a series of 0x10000 unique ids. Not to be used for hard-to-guess ids, obviously.
129  
130 20 my $uuid;
131 20 my $suffix = 0;
132  
133 2534 ▶ sub unique_id {
Subroutine unique_id: called CC=3 SCAR=11.0
Pod undocumented
134 2534 ▶ if ( !$suffix || !defined($uuid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($suffix and defined $uuid) 9 2525 2534
Condition66%2/3
$suffix and defined $uuid
!ll&&!rl&&r
902525
MC/DC50%1/2
$suffix and defined $uuid
$suffix defined $uuid
Truth table66%2/3
A and B
A= $suffixB= defined $uuid
ABresult
0X0
100
111
135 9 my $ug = Data::UUID->new();
136 9 $uuid = $ug->create_hex();
137 }
138 2534 my $hex = sprintf( '%s%04x', $uuid, $suffix );
139 2534 $suffix = ( $suffix + 1 ) & 0xffff;
140 2534 return $hex;
141 }
142 }
143  
144 20 ▶use constant _FILE_SPEC_USING_UNIX =>
Subroutine BEGIN: called CC=1 SCAR=0.0
145 20 ( $File::Spec::ISA[0] eq 'File::Spec::Unix' );
146  
147 2413 ▶sub fast_catdir {
Subroutine fast_catdir: called CC=1 SCAR=0.0
Pod undocumented
148 2413 if (_FILE_SPEC_USING_UNIX) {
149 2413 return join '/', @_;
150 }
151 else {
152 return catdir(@_);
153 }
154 }
155  
156 11469 ▶sub fast_catfile {
Subroutine fast_catfile: called CC=1 SCAR=0.0
Pod undocumented
157 11469 if (_FILE_SPEC_USING_UNIX) {
158 11469 return join '/', @_;
159 }
160 else {
161 return catfile(@_);
162 }
163 }
164  
165 20 my %memory_size_units = ( 'k' => 1024, 'm' => 1024 * 1024 );
166  
167 12 ▶sub parse_memory_size {
Subroutine parse_memory_size: called CC=3 SCAR=11.0
Pod undocumented
168 12 my $size = shift;
169 12 ▶ if ( $size =~ /^\d+b?$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size =~ /^\d+b?$/) { } 1 11 12
elsif (() = $size =~ /^(\d+)\s*([km])b?$/i) { } 10 1 11
170 1 return $size;
171 }
172 elsif ( my ( $quantity, $unit ) = ( $size =~ /^(\d+)\s*([km])b?$/i ) ) {
173 10 return $quantity * $memory_size_units{ lc($unit) };
174 }
175 else {
176 1 croak "cannot parse memory size '$size'";
177 }
178 }
179  
180 20 my $json = JSON::MaybeXS->new( utf8 => 1, canonical => 1 );
181  
182 0 ▶sub json_decode {
Subroutine json_decode: not called CC=1 SCAR=6.9
Pod undocumented
183 0 $json->decode( $_[0] );
184 }
185  
186 769 ▶sub json_encode {
Subroutine json_encode: called CC=1 SCAR=0.0
Pod undocumented
187 769 $json->encode( $_[0] );
188 }
189  
190 20 1;
191  
192 __END__
« CHI/Driver/Role/HasSubcaches.pm ↑ Summary CHI/CacheObject.pm »