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