| 1 | package CHI::Util; | ||||||||||||||||||||||||||||
| 2 | 20 | $CHI::Util::VERSION = '0.60'; | |||||||||||||||||||||||||||
| 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 | sub can_load { | ||||||||||||||||||||||||||||
| 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) = @_; | ||||||||||||||||||||||||||
|
Subroutine
can_load: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 49 | my $result; | |||||||||||||||||||||||||||
| 43 | try { | ||||||||||||||||||||||||||||
| 44 | 49 | ▶ | require_module($class_name); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 21 | $result = 1; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | catch { | ||||||||||||||||||||||||||||
| 48 | 28 | ▶ | 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/
| |||||||||||||||||||||||||||||
| 49 | 28 | $result = 0; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||
| 52 | 0 | die $_; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 49 | }; | |||||||||||||||||||||||||||
| 55 | 49 | return $result; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub dump_one_line { | ||||||||||||||||||||||||||||
| 59 | 7 | ▶ | my ($value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dump_one_line: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub read_dir { | ||||||||||||||||||||||||||||
| 67 | 46 | ▶ | my ($dir) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_dir: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ## no critic (RequireInitializationForLocalVars) | ||||||||||||||||||||||||||||
| 70 | 46 | local *DIRH; | |||||||||||||||||||||||||||
| 71 | 46 | ▶ | opendir( DIRH, $dir ) or croak "cannot open '$dir': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 46 | ▶ | return grep { $_ ne "." && $_ ne ".." } readdir(DIRH); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub read_file { | ||||||||||||||||||||||||||||
| 76 | 5406 | ▶ | my ($file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=5 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | 5406 | ▶ | last if $read_cnt == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 5406 | $size_left -= $read_cnt; | |||||||||||||||||||||||||||
| 91 | 5406 | ▶ | last if $size_left <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||
| 94 | 0 | croak "read_file '$file' - sysread: $!"; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 5406 | return $buf; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub write_file { | ||||||||||||||||||||||||||||
| 101 | 2228 | ▶ | my ( $file, $data, $file_create_mode ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 2228 | ▶ | $file_create_mode = oct(666) if !defined($file_create_mode); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Fast spew, adapted from File::Slurp::write, with unnecessary options removed | ||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 2228 | my $write_fh; | |||||||||||||||||||||||||||
| 108 | 2228 | ▶ | unless ( sysopen( $write_fh, $file, $Store_Flags, $file_create_mode ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 2 | croak "write_file '$file' - sysopen: $!"; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 2226 | my $size_left = length($data); | |||||||||||||||||||||||||||
| 113 | 2226 | my $offset = 0; | |||||||||||||||||||||||||||
| 114 | 2226 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 2226 | my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset ); | |||||||||||||||||||||||||||
| 116 | 2226 | ▶ | unless ( defined $write_cnt ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | croak "write_file '$file' - syswrite: $!"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 2226 | $size_left -= $write_cnt; | |||||||||||||||||||||||||||
| 120 | 2226 | $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 | sub unique_id { | ||||||||||||||||||||||||||||
| 134 | 2537 | ▶ | 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
| |||||||||||||||||||||||||||||
| 135 | 9 | my $ug = Data::UUID->new(); | |||||||||||||||||||||||||||
| 136 | 9 | $uuid = $ug->create_hex(); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 2537 | my $hex = sprintf( '%s%04x', $uuid, $suffix ); | |||||||||||||||||||||||||||
| 139 | 2537 | $suffix = ( $suffix + 1 ) & 0xffff; | |||||||||||||||||||||||||||
| 140 | 2537 | return $hex; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 20 | use constant _FILE_SPEC_USING_UNIX => | |||||||||||||||||||||||||||
| 145 | 20 | ▶ | ( $File::Spec::ISA[0] eq 'File::Spec::Unix' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub fast_catdir { | ||||||||||||||||||||||||||||
| 148 | 2416 | ▶ | if (_FILE_SPEC_USING_UNIX) { | ||||||||||||||||||||||||||
|
Subroutine
fast_catdir: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 2416 | return join '/', @_; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||||||||||
| 152 | return catdir(@_); | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub fast_catfile { | ||||||||||||||||||||||||||||
| 157 | 11486 | ▶ | if (_FILE_SPEC_USING_UNIX) { | ||||||||||||||||||||||||||
|
Subroutine
fast_catfile: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 11486 | return join '/', @_; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | return catfile(@_); | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 20 | my %memory_size_units = ( 'k' => 1024, 'm' => 1024 * 1024 ); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | sub parse_memory_size { | ||||||||||||||||||||||||||||
| 168 | 12 | ▶ | my $size = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse_memory_size: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 169 | 12 | ▶ | if ( $size =~ /^\d+b?$/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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 | sub json_decode { | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | $json->decode( $_[0] ); | ||||||||||||||||||||||||||
|
Subroutine
json_decode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | sub json_encode { | ||||||||||||||||||||||||||||
| 187 | 770 | ▶ | $json->encode( $_[0] ); | ||||||||||||||||||||||||||
|
Subroutine
json_encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 20 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||