lib/Module/Generic/File/Cache.pm

filter: Statementstmt 73.2% 400 / 546 Branchbran 36.7% 111 / 302 Conditioncond 29.7% 64 / 215 MC/DCmcdc 5.4% 6 / 111 Subroutinesub 80.5% 58 / 72 Podpod 87.5% 35 / 40 totaltotal 52.4% 674 / 1286 CC 245 SCAR 56.0
CC
245
Cov
54%
END
1618.5
write
246.5
__ANON__
107.9
CRAP 269.7
« lib/Module/Generic/File/Mmap.pm ↑ Summary lib/Module/Generic/Dynamic.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/File/Cache.pm
3 ## Version v0.3.1
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/16
7 ## Modified 2026/06/02
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Module::Generic::File::Cache;
14 BEGIN
15 5 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶ use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 warnings::register_categories( 'Module::Generic' );
20 5 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶ use vars qw( $DEBUG $HAS_B64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶ use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶ use Module::Generic::Global ':const';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶ use Module::Generic::File qw( file sys_tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 # use Nice::Try;
26 # This is disruptive for everybody. Bad idea.
27 # use JSON 4.03 qw( -convert_blessed_universally );
28 5 ▶ use JSON 4.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 # use Storable 3.25 ();
31 5 ▶ use Storable::Improved v0.1.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 $DEBUG = 0;
33 5 our $VERSION = 'v0.3.1';
34 };
35  
36 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 sub init
40 8 ▶{
Subroutine init: called CC=3 SCAR=11.0
Pod documented
41 8 my $self = shift( @_ );
42 8 $self->{binmode} = 'utf-8';
43 8 $self->{base64} = undef;
44 # Default action when accessing a shared file cache? If 1, it will create it if it does not exist already
45 8 $self->{create} = 0;
46 # If true, this will destroy both the shared file cache and the semaphore upon end
47 8 $self->{destroy} = 0;
48 8 $self->{exclusive} = 0;
49 5 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 8 $self->{key} = Data::UUID->new->create_str;
51 8 $self->{mode} = 0666;
52 8 $self->{serial} = '';
53 # SHM_BUFSIZ
54 8 $self->{size} = 0;
55 8 $self->{tmpdir} = sys_tmpdir();
56 8 $self->{_init_strict_use_sub} = 1;
57 # Storable keeps breaking :(
58 # I leave the feature of using it as a choice to the user, but defaults to JSON.
59 # Other possibilities could be cbor, sereal and storable
60 8 $self->{_packing_method} = 'json';
61 8 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 8 0 8
62 8 ▶ my $tmpdir = $self->{tmpdir} || sys_tmpdir();
Condition33%1/3
$self->{'tmpdir'} || sys_tmpdir()
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->{'tmpdir'} || sys_tmpdir()
$self->{'tmpdir'} sys_tmpdir()
Truth table33%1/3
A || B
A= $self->{'tmpdir'}B= sys_tmpdir()
ABresult
1X1
011
000
63 8 $self->{_cache_dir} = $tmpdir->child( 'cache_file' );
64 8 $self->{_cache_dir}->debug( $self->debug );
65 8 $self->{_cache_file} = '';
66 8 $self->{id} = undef();
67 8 $self->{locked} = 0;
68 8 $self->{owner} = $$;
69 8 $self->{removed} = 0;
70 8 return( $self );
71 }
72  
73 # This class does not convert to an HASH
74 0 ▶sub as_hash { return( $_[0] ); }
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
75  
76 3 ▶sub base64 { return( shift->_set_get_scalar( 'base64', @_ ) ); }
Subroutine base64: called CC=1 SCAR=0.0
Pod documented
77  
78 3 ▶sub binmode { return( shift->_set_get_scalar( 'binmode', @_ ) ); }
Subroutine binmode: called CC=1 SCAR=0.0
Pod undocumented
79  
80 0 ▶sub cbor { return( shift->_packing_method( 'cbor' ) ); }
Subroutine cbor: not called CC=1 SCAR=6.9
Pod documented
81  
82 14 ▶sub create { return( shift->_set_get_boolean( 'create', @_ ) ); }
Subroutine create: called CC=1 SCAR=0.0
Pod documented
83  
84 0 ▶sub delete { return( shift->remove( @_ ) ); }
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
85  
86 12 ▶sub destroy { return( shift->_set_get_boolean( 'destroy', @_ ) ); }
Subroutine destroy: called CC=1 SCAR=0.0
Pod documented
87  
88 0 ▶sub exclusive { return( shift->_set_get_boolean( 'exclusive', @_ ) ); }
Subroutine exclusive: not called CC=1 SCAR=6.9
Pod documented
89  
90 sub exists
91 3 ▶{
Subroutine exists: called CC=3 SCAR=16.5
Pod documented
92 3 my $self = shift( @_ );
93 3 ▶ my $file = $self->{_cache_file} || return(0);
Condition50%1/2
$self->{'_cache_file'} || (return 0)
l!l
03
MC/DC0%0/1
$self->{'_cache_file'} || (return 0)
$self->{'_cache_file'}
Truth table50%1/2
A
A= $self->{'_cache_file'}
Aresult
11
00
94 0 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 0 0
95 0 return( $file->exists );
96 }
97  
98 sub flags
99 3 ▶{
Subroutine flags: called CC=5 SCAR=16.2
Pod documented
100 3 my $self = shift( @_ );
101 3 my $opts = $self->_get_args_as_hash( @_ );
102 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 5 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 3 ▶ $opts->{create} = $self->create unless( length( $opts->{create} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"create"} 0 3 3
105 3 ▶ $opts->{mode} = $self->mode unless( length( $opts->{mode} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"mode"} 3 0 3
106 3 my $flags = 0;
107 3 ▶ $flags |= 0600 if( $opts->{create} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"create"} 2 1 3
108 3 ▶ $flags |= ( $opts->{mode} || 0666 );
Condition50%1/2
$opts->{'mode'} || 438
l!l
30
MC/DC0%0/1
$opts->{'mode'} || 438
$opts->{'mode'}
Truth table50%1/2
A
A= $opts->{'mode'}
Aresult
11
00
109 3 return( $flags );
110 }
111  
112 sub ftok
113 11 ▶{
Subroutine ftok: called CC=1 SCAR=0.0
Pod documented
114 11 my $self = shift( @_ );
115 11 my $id = shift( @_ );
116 11 my $f = sys_tmpdir();
117 return( int(
118 11 ( int( $f->inode ) & 0xffff ) |
119 int( ( $f->device ) << 16 ) |
120 ( ( $id & 0xff ) << 21 )
121 ) );
122 }
123  
124 6 ▶sub id { return( shift->_set_get_scalar( 'id', @_ ) ); }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
125  
126 0 ▶sub json { return( shift->_packing_method( 'json' ) ); }
Subroutine json: not called CC=1 SCAR=6.9
Pod documented
127  
128 sub key
129 21 ▶{
Subroutine key: called CC=2 SCAR=6.9
Pod documented
130 21 my $self = shift( @_ );
131 21 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 10 21
132 {
133 11 $self->{key} = shift( @_ );
134 11 $self->{serial} = $self->_str2key( $self->{key} );
135 }
136 21 return( $self->{key} );
137 }
138  
139 sub lock
140 1 ▶{
Subroutine lock: called CC=4 SCAR=14.6
Pod documented
141 1 my $self = shift( @_ );
142 my $file = $self->{_cache_file} ||
143 1 ▶ return( $self->error( "No cache file is set yet. Have you first opened the cache file with open()?" ) );
Condition50%1/2
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
l!l
10
MC/DC0%0/1
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
$self->{'_cache_file'}
Truth table50%1/2
A
A= $self->{'_cache_file'}
Aresult
11
00
144 1 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 1 1
145 1 ▶ $self->locked( $file->lock ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$file->lock ? : 1 0 1
146 1 return( $self );
147 }
148  
149 2 ▶sub locked { return( shift->_set_get_boolean( 'locked', @_ ) ); }
Subroutine locked: called CC=1 SCAR=0.0
Pod documented
150  
151 15 ▶sub mode { return( shift->_set_get_scalar( 'mode', @_ ) ); }
Subroutine mode: called CC=1 SCAR=0.0
Pod documented
152  
153 sub open
154 3 ▶{
Subroutine open: called CC=43 SCAR=46.4
Pod documented
155 3 my $self = shift( @_ );
156 3 my $opts = {};
157 3 ▶ if( ref( $_[0] ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 1 3
158 {
159 2 $opts = shift( @_ );
160 }
161 else
162 {
163 1 @$opts{ qw( key mode size ) } = @_;
164 }
165 3 ▶ $opts->{size} = $self->size unless( length( $opts->{size} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"size"} 3 0 3
166 3 $opts->{size} = int( $opts->{size} );
167 3 ▶ $opts->{mode} //= '';
Condition100%2/2
$opts->{"mode"} //= ""
l!l
21
MC/DC100%1/1
$opts->{"mode"} //= ""
$opts->{"mode"}
Truth table100%2/2
A
A= $opts->{"mode"}
Aresult
11
00
168 3 ▶ $opts->{key} //= '';
Condition50%1/2
$opts->{"key"} //= ""
l!l
03
MC/DC0%0/1
$opts->{"key"} //= ""
$opts->{"key"}
Truth table50%1/2
A
A= $opts->{"key"}
Aresult
11
00
169 3 ▶ $opts->{binmode} //= $self->binmode // 'utf-8';
Condition50%2/4
$opts->{"binmode"} //= $self->binmode // "utf-8"
l!l
03
$self->binmode // "utf-8"
l!l
30
MC/DC0%0/1
$opts->{"binmode"} //= $self->binmode // "utf-8"
$opts->{"binmode"}
Truth table50%1/2
A
A= $opts->{"binmode"}
Aresult
11
00
170 5 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
171 3 my $serial;
172 3 ▶ if( length( $opts->{key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts->{'key'}) { } 0 3 3
173 {
174 0 ▶ $serial = $self->_str2key( $opts->{key} ) ||
Condition0%0/2
$self->_str2key($opts->{'key'}) || (return $self->error("Cannot get serial from key '$opts->{'key'}': ", $self->error))
l!l
00
MC/DC0%0/1
$self->_str2key($opts->{'key'}) || (return $self->error("Cannot get serial from key '$opts->{'key'}': ", $self->error))
$self->_str2key($opts->{'key'})
Truth table0%0/2
A
A= $self->_str2key($opts->{'key'})
Aresult
11
00
175 return( $self->error( "Cannot get serial from key '$opts->{key}': ", $self->error ) );
176 }
177 else
178 {
179 3 $serial = $self->serial;
180 }
181 3 ▶ die( "There is no serial!!" ) if( !CORE::length( $serial // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length($serial // "") 0 3 3
Condition50%1/2
$serial // ""
l!l
30
MC/DC0%0/1
$serial // ""
$serial
Truth table50%1/2
A
A= $serial
Aresult
11
00
182 3 my $create = 0;
183 3 ▶ if( $opts->{mode} eq 'w' || $opts->{key} =~ s/^>// )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'mode'} eq 'w' or $opts->{'key'} =~ s/^>//) { } 2 1 3
elsif ($opts->{'mode'} eq 'r' or $opts->{'key'} =~ s/^<//) { } 0 1 1
Condition66%2/3
$opts->{"mode"} eq "w" or $opts->{"key"} =~ s/^>//
l!l&&r!l&&!r
201
MC/DC50%1/2
$opts->{"mode"} eq "w" or $opts->{"key"} =~ s/^>//
$opts->{"mode"} eq "w" $opts->{"key"} =~ s/^>//
Truth table66%2/3
A or B
A= $opts->{"mode"} eq "w"B= $opts->{"key"} =~ s/^>//
ABresult
1X1
011
000
184 {
185 2 $create++;
186 }
187 ▶ elsif( $opts->{mode} eq 'r' || $opts->{key} =~ s/^<// )
Condition33%1/3
$opts->{"mode"} eq "r" or $opts->{"key"} =~ s/^<//
l!l&&r!l&&!r
001
MC/DC0%0/2
$opts->{"mode"} eq "r" or $opts->{"key"} =~ s/^<//
$opts->{"mode"} eq "r" $opts->{"key"} =~ s/^<//
Truth table33%1/3
A or B
A= $opts->{"mode"} eq "r"B= $opts->{"key"} =~ s/^<//
ABresult
1X1
011
000
188 {
189 0 $create = 0;
190 }
191 else
192 {
193 1 $create = $self->create;
194 }
195 3 ▶ my $flags = $self->flags( create => $create, ( $opts->{mode} =~ /^\d+$/ ? $opts->{mode} : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'mode'} =~ /^\d+$/ ? : 0 3 3
196  
197 3 ▶ my $cache_dir = $self->{_cache_dir} || return( $self->error( "Cache directory object is gone!" ) );
Condition50%1/2
$self->{'_cache_dir'} || (return $self->error('Cache directory object is gone!'))
l!l
30
MC/DC0%0/1
$self->{'_cache_dir'} || (return $self->error('Cache directory object is gone!'))
$self->{'_cache_dir'}
Truth table50%1/2
A
A= $self->{'_cache_dir'}
Aresult
11
00
198 3 ▶ return( $self->error( "Cache directory is not a Module::Generic::File object!" ) ) if( !$self->_is_a( $cache_dir => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($cache_dir, "Module::Generic::File") 0 3 3
199 3 ▶ if( $cache_dir->exists )
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_dir->exists) { } 1 2 3
200 {
201 1 ▶ if( !$cache_dir->is_dir )
Branch: 2 branches
BranchTrueFalseTotal
if (not $cache_dir->is_dir) { } 0 1 1
elsif (not $cache_dir->can_write) { } 0 1 1
202 {
203 0 return( $self->error( "Cache directory exists, but is not a directory!" ) );
204 }
205 elsif( !$cache_dir->can_write )
206 {
207 0 return( $self->error( "Cache directory exists, but the current user id $> is not allowed to write to it!" ) );
208 }
209 }
210 else
211 {
212 2 ▶ $cache_dir->makepath || return( $self->pass_error( $cache_dir->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$cache_dir->makepath or return $self->pass_error($cache_dir->error) 2 0 2
213 }
214 3 my $fo = $cache_dir->child( $serial );
215 3 $fo->debug( $self->debug );
216 3 my $cache_file = "$fo";
217 3 my $io;
218 3 ▶ if( $fo->exists )
Branch: 1 branch
BranchTrueFalseTotal
if ($fo->exists) { } 1 2 3
219 {
220 # Need to find a way to make that more efficient
221 1 ▶ if( ( $flags & 0600 ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 384 or $flags & 48 or $flags & 6) { } 1 0 1
Condition33%2/6
$flags & 384 or $flags & 48 or $flags & 6
l!l&&r!l&&!r
100
$flags & 384 or $flags & 48
l!l&&r!l&&!r
100
MC/DC0%0/3
$flags & 384 or $flags & 48 or $flags & 6
$flags & 384 $flags & 48 $flags & 6
Truth table25%1/4
A or B or C
A= $flags & 384B= $flags & 48C= $flags & 6
ABCresult
1XX1
01X1
0011
0000
222 ( $flags & 0060 ) ||
223 ( $flags & 0006 ) )
224 {
225 1 ▶ return( $self->error( "Requested mode ($flags) requires writing, but uid $> is missing write privilege to the cache file \"$cache_file\"." ) ) if( !$fo->can_write );
Branch: 1 branch
BranchTrueFalseTotal
unless $fo->can_write 0 1 1
226 1 ▶ $io = $fo->open( '+<', { binmode => $opts->{binmode}, autoflush => 1 } ) ||
Condition50%1/2
$fo->open('+<', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
l!l
10
MC/DC0%0/1
$fo->open('+<', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
$fo->open('+<', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Truth table50%1/2
A
A= $fo->open('+<', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Aresult
11
00
227 return( $self->pass_error( $fo->error ) );
228 # Ok, we could write to the cache file, remove data we just wrote
229 1 ▶ $fo->seek(0,0) || return( $self->pass_error( $fo->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$fo->seek(0, 0) or return $self->pass_error($fo->error) 1 0 1
230 # $fo->truncate( $fo->tell ) || return( $self->pass_error( $fo->error ) );
231 }
232 else
233 {
234 0 ▶ return( $self->error( "Requested mode ($flags) require reading, but missing access privilege to the cache file \"$cache_file\"." ) ) if( !$fo->can_read );
Branch: 1 branch
BranchTrueFalseTotal
unless $fo->can_read 0 0 0
235 0 ▶ $io = $fo->open( '<', { binmode => $opts->{binmode}, autoflush => 1 } ) ||
Condition0%0/2
$fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
l!l
00
MC/DC0%0/1
$fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
$fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Truth table0%0/2
A
A= $fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Aresult
11
00
236 return( $self->pass_error( $fo->error ) );
237 }
238 }
239 else
240 {
241 2 ▶ if( ( $flags & 0600 ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 384 or $flags & 48 or $flags & 6) { } 2 0 2
Condition33%2/6
$flags & 384 or $flags & 48 or $flags & 6
l!l&&r!l&&!r
200
$flags & 384 or $flags & 48
l!l&&r!l&&!r
200
MC/DC0%0/3
$flags & 384 or $flags & 48 or $flags & 6
$flags & 384 $flags & 48 $flags & 6
Truth table25%1/4
A or B or C
A= $flags & 384B= $flags & 48C= $flags & 6
ABCresult
1XX1
01X1
0011
0000
242 ( $flags & 0060 ) ||
243 ( $flags & 0006 ) )
244 {
245 2 ▶ $io = $fo->open( '+>', { binmode => $opts->{binmode}, autoflush => 1 } ) ||
Condition50%1/2
$fo->open('+>', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
l!l
20
MC/DC0%0/1
$fo->open('+>', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
$fo->open('+>', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Truth table50%1/2
A
A= $fo->open('+>', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Aresult
11
00
246 return( $self->pass_error( $fo->error ) );
247 # Some size, was provided, we fill the file with it, thus ensuring the filesystem lets us use that much
248 # Subsequent calls to write(9 will clobber the file and truncate it to the actual size
249 # but no more than the size if it was provided.
250 2 ▶ if( $opts->{size} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"size"}) 2 0 2
251 {
252 2 ▶ $self->_fill( $opts->{size} => $fo ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_fill($opts->{"size"}, $fo) or return $self->pass_error 2 0 2
253 }
254 }
255 else
256 {
257 0 return( $self->error( "Requested mode ($flags) require reading, but the cache file \"$cache_file\" does not exist yet." ) );
258 }
259 }
260  
261 3 $self->serial( $serial );
262 my $new = $self->new(
263 key => ( $opts->{key} || $self->key ),
264 debug => $self->debug,
265 mode => $flags,
266 destroy => $self->destroy,
267 _packing_method => $self->_packing_method,
268 3 ▶ ) || return( $self->error( "Cannot create object with key '", ( $opts->{key} || $self->key ), "': ", $self->error ) );
Condition25%2/8
$self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, '_packing_method', $self->_packing_method) || (return $self->error(q[Cannot create object with key '], $opts->{'key'} || $self->key, q[': ], $self->error))
l!l
30
$opts->{'key'} || $self->key
l!l&&r!l&&!r
030
$opts->{'key'} || $self->key
l!l&&r!l&&!r
000
MC/DC0%0/1
$self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, '_packing_method', $self->_packing_method) || (return $self->error(q[Cannot create object with key '], $opts->{'key'} || $self->key, q[': ], $self->error))
$self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, '_packing_method', $self->_packing_method)
MC/DC0%0/2
$opts->{'key'} || $self->key
$opts->{'key'} $self->key
MC/DC0%0/2
$opts->{'key'} || $self->key
$opts->{'key'} $self->key
Truth table50%1/2
A
A= $self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, '_packing_method', $self->_packing_method)
Aresult
11
00
Truth table33%1/3
A || B
A= $opts->{'key'}B= $self->key
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $opts->{'key'}B= $self->key
ABresult
1X1
011
000
269 3 $new->{base64} = $self->base64;
270 3 $new->key( $self->key );
271 3 $new->serial( $serial );
272 3 my $addr = Scalar::Util::refaddr( $new );
273 3 $new->id( $addr );
274 3 $new->size( $opts->{size} );
275 3 $new->{_cache_file} = $fo;
276 3 my $repo = Module::Generic::Global->new( 'cache' => CORE::ref( $self ), key => CORE::ref( $self ) );
277 3 my $file2obj_repo = Module::Generic::Global->new( 'file2object_repo' => CORE::ref( $self ), key => CORE::ref( $self ) );
278 3 $repo->lock;
279 3 ▶ my $repo_data = $repo->get // [];
Condition100%2/2
$repo->get // []
l!l
12
MC/DC100%1/1
$repo->get // []
$repo->get
Truth table100%2/2
A
A= $repo->get
Aresult
11
00
280 3 CORE::push( @$repo_data, $new );
281 3 $repo->set( $repo_data );
282 3 $repo->unlock;
283 3 $file2obj_repo->lock;
284 3 ▶ my $f2o_data = $file2obj_repo->get // {};
Condition100%2/2
$file2obj_repo->get // {}
l!l
12
MC/DC100%1/1
$file2obj_repo->get // {}
$file2obj_repo->get
Truth table100%2/2
A
A= $file2obj_repo->get
Aresult
11
00
285 3 ▶ $f2o_data->{ "$fo" } //= [];
Condition100%2/2
$$f2o_data{"$fo"} //= []
l!l
12
MC/DC100%1/1
$$f2o_data{"$fo"} //= []
$$f2o_data{"$fo"}
Truth table100%2/2
A
A= $$f2o_data{"$fo"}
Aresult
11
00
286 # To keep memory size reasonable, we only store the object address and not the object itself.
287 3 CORE::push( @{$f2o_data->{ "$fo" }}, $addr );
288 3 $file2obj_repo->set( $f2o_data );
289 3 $file2obj_repo->unlock;
290 3 return( $new );
291 }
292  
293 2 ▶sub owner { return( shift->_set_get_scalar( 'owner', @_ ) ); }
Subroutine owner: called CC=1 SCAR=0.0
Pod documented
294  
295 0 ▶sub pid { return( shift->owner( @_ ) ); }
Subroutine pid: not called CC=1 SCAR=6.9
Pod documented
296  
297 sub rand
298 0 ▶{
Subroutine rand: not called CC=1 SCAR=6.9
Pod documented
299 0 my $self = shift( @_ );
300 0 return( Data::UUID->new->create_str );
301 }
302  
303 sub read
304 5 ▶{
Subroutine read: called CC=24 SCAR=45.4
Pod documented
305 5 my( $self, $buf ) = @_;
306 my $file = $self->{_cache_file} ||
307 5 ▶ return( $self->error( "No cache file is set yet. Have you first opened the cache file with open()?" ) );
Condition50%1/2
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
l!l
50
MC/DC0%0/1
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
$self->{'_cache_file'}
Truth table50%1/2
A
A= $self->{'_cache_file'}
Aresult
11
00
308 5 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 5 5
309 # The cache file does not exists; maybe it has been removed by a destroyed process ?
310 5 ▶ if( !$file->exists )
Branch: 1 branch
BranchTrueFalseTotal
unless ($file->exists) 0 5 5
311 {
312 0 $self->removed(1);
313 0 return( '' );
314 }
315 5 ▶ $file->lock || return( $self->pass_error( $file->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$file->lock or return $self->pass_error($file->error) 5 0 5
316 # Make sure we are at the top of the file
317 5 ▶ if( !$file->seek(0,0) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($file->seek(0, 0)) 0 5 5
318 {
319 0 $file->unlock;
320 0 return( $self->pass_error( $file->error ) )
321 }
322 5 my $buffer = '';
323 5 my $bytes = $file->read( $buffer, $file->length );
324 5 $file->unlock;
325 5 ▶ return( $self->pass_error( $file->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 5 5
326 5 my $packing = $self->_packing_method;
327 5 ▶ $packing = lc( $packing ) if( defined( $packing ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $packing 5 0 5
328 5 my $data;
329 5 ▶ if( CORE::length( $buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer) { } 5 0 5
330 {
331 # There may be encapsulation of data before writing data to memory.
332 # e.g.: MG[14]something here
333 5 ▶ if( index( $buffer, 'MG[' ) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($buffer, "MG[") == 0) 0 5 5
334 {
335 0 my $def = substr( $buffer, 0, index( $buffer, ']' ) + 1, '' );
336 # Get the string length stored
337 0 my $len = int( substr( $def, 3, -1 ) );
338 # Remove any possible remaining unwanted data
339 0 substr( $buffer, $len, length( $buffer ), '' );
340 }
341  
342 # try-catch
343 5 local $@;
344 eval
345 5 {
346 5 ▶ if( $packing eq 'json' )
Branch: 3 branches
BranchTrueFalseTotal
if ($packing eq 'json') { } 5 0 5
elsif ($packing eq 'cbor') { } 0 0 0
elsif ($packing eq 'sereal') { } 0 0 0
347 {
348 5 $data = $self->_decode_json( $buffer );
349 }
350 elsif( $packing eq 'cbor' )
351 {
352 $data = $self->deserialise(
353 data => $buffer,
354 serialiser => 'CBOR::XS',
355 allow_sharing => 1,
356 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
357 );
358 }
359 elsif( $packing eq 'sereal' )
360 {
361 $data = $self->deserialise(
362 data => $buffer,
363 serialiser => 'Sereal',
364 freeze_callbacks => 1,
365 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
366 );
367 }
368 # By default Storable::Improved
369 else
370 {
371 # $data = Storable::Improved::thaw( $buffer );
372 $data = $self->deserialise(
373 data => $buffer,
374 serialiser => 'Storable::Improved',
375 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
376 );
377 }
378 };
379 5 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
380 {
381 0 ▶ return( $self->error( "An error occured while decoding data using $packing with base64 set to '", ( $self->{base64} // '' ), "': $@" ) );
Condition0%0/2
$self->{"base64"} // ""
l!l
00
MC/DC0%0/1
$self->{"base64"} // ""
$self->{"base64"}
Truth table0%0/2
A
A= $self->{"base64"}
Aresult
11
00
382 };
383 }
384 else
385 {
386 0 $data = $buffer;
387 }
388  
389 5 ▶ $self->__message( 5, "read(): unpacked data of type ", ( ref($data) || 'SCALAR' ), " with value: ", overload::StrVal( $data ) );
Condition50%1/2
ref $data || 'SCALAR'
l!l
50
MC/DC0%0/1
ref $data || 'SCALAR'
ref $data
Truth table50%1/2
A
A= ref $data
Aresult
11
00
390  
391 # data decoded is not a reference and size was provided and is greater than 0
392 5 ▶ if( !ref( $data ) && scalar( @_ ) > 2 && int( $_[2] ) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $data and scalar @_ > 2 and int $_[2] > 0) 0 5 5
Condition33%2/6
not ref $data and scalar @_ > 2 and int $_[2] > 0
!ll&&!rl&&r
500
not ref $data and scalar @_ > 2
!ll&&!rl&&r
500
MC/DC0%0/3
not ref $data and scalar @_ > 2 and int $_[2] > 0
not ref $data scalar @_ > 2 int $_[2] > 0
Truth table25%1/4
not(A) and B and C
A= not ref $dataB= scalar @_ > 2C= int $_[2] > 0
ABCresult
0XX0
10X0
1100
1111
393 {
394 0 $data = substr( $data, 0, $_[2] );
395 }
396  
397 5 ▶ if( scalar( @_ ) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 1 4 5
398 {
399 1 $_[1] = $data;
400 1 ▶ return( CORE::length( $_[1] ) || "0E0" );
Condition50%1/2
length $_[1] || '0E0'
l!l
10
MC/DC0%0/1
length $_[1] || '0E0'
length $_[1]
Truth table50%1/2
A
A= length $_[1]
Aresult
11
00
401 }
402 else
403 {
404 4 return( $data );
405 }
406 }
407  
408 sub remove
409 1 ▶{
Subroutine remove: called CC=13 SCAR=29.2
Pod documented
410 1 my $self = shift( @_ );
411 1 ▶ return(1) if( $self->removed );
Branch: 1 branch
BranchTrueFalseTotal
if $self->removed 0 1 1
412 my $file = $self->{_cache_file} ||
413 1 ▶ return( $self->error( "No cache file is set yet. Have you first opened the cache file with open()?" ) );
Condition50%1/2
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
l!l
10
MC/DC0%0/1
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
$self->{'_cache_file'}
Truth table50%1/2
A
A= $self->{'_cache_file'}
Aresult
11
00
414 1 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 1 1
415 1 ▶ if( !$file->exists )
Branch: 1 branch
BranchTrueFalseTotal
unless ($file->exists) 0 1 1
416 {
417 0 $self->removed(1);
418 0 return( $self );
419 }
420 1 $file->unlock;
421 1 my $rv = $file->delete;
422 1 ▶ return( $self->pass_error( $file->error ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 1 1
423 1 ▶ $self->removed( $rv ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 1 0 1
424 1 ▶ if( $rv )
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 1 0 1
425 {
426 1 my $fname = "$file";
427 1 my $file2obj_repo = Module::Generic::Global->new( 'file2object_repo' => CORE::ref( $self ), key => CORE::ref( $self ) );
428 1 $file2obj_repo->lock;
429 1 ▶ my $f2o_data = $file2obj_repo->get // {};
Condition50%1/2
$file2obj_repo->get // {}
l!l
10
MC/DC0%0/1
$file2obj_repo->get // {}
$file2obj_repo->get
Truth table50%1/2
A
A= $file2obj_repo->get
Aresult
11
00
430 1 CORE::delete( $f2o_data->{ $fname } );
431 1 $file2obj_repo->set( $f2o_data );
432 1 $file2obj_repo->unlock;
433 1 $self->{_cache_file} = '';
434 1 $self->id( undef() );
435 }
436  
437 1 ▶ if( $self->{_cache_dir} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_cache_dir"} and $self->{"_cache_dir"}->exists and $self->{"_cache_dir"}->is_empty) 1 0 1
Condition33%2/6
$self->{"_cache_dir"} and $self->{"_cache_dir"}->exists and $self->{"_cache_dir"}->is_empty
!ll&&!rl&&r
001
$self->{"_cache_dir"} and $self->{"_cache_dir"}->exists
!ll&&!rl&&r
001
MC/DC0%0/3
$self->{"_cache_dir"} and $self->{"_cache_dir"}->exists and $self->{"_cache_dir"}->is_empty
$self->{"_cache_dir"} $self->{"_cache_dir"}->exists $self->{"_cache_dir"}->is_empty
Truth table25%1/4
A and B and C
A= $self->{"_cache_dir"}B= $self->{"_cache_dir"}->existsC= $self->{"_cache_dir"}->is_empty
ABCresult
0XX0
10X0
1100
1111
438 $self->{_cache_dir}->exists &&
439 $self->{_cache_dir}->is_empty )
440 {
441 1 $self->{_cache_dir}->remove;
442 }
443 1 ▶ return( $rv ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 1 0 1
444 }
445  
446 2 ▶sub removed { return( shift->_set_get_boolean( 'removed', @_ ) ); }
Subroutine removed: called CC=1 SCAR=0.0
Pod documented
447  
448 sub reset
449 0 ▶{
Subroutine reset: not called CC=4 SCAR=30.0
Pod documented
450 0 my $self = shift( @_ );
451 0 my $default;
452 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
453 {
454 0 $default = shift( @_ );
455 }
456 else
457 {
458 0 $default = '';
459 }
460 my $file = $self->{_cache_file} ||
461 0 ▶ return( $self->error( "No cache file is set yet. Have you first opened the cache file with open()?" ) );
Condition0%0/2
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
l!l
00
MC/DC0%0/1
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
$self->{'_cache_file'}
Truth table0%0/2
A
A= $self->{'_cache_file'}
Aresult
11
00
462 0 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 0 0
463 0 $self->write( $default );
464 0 return( $self );
465 }
466  
467 0 ▶sub sereal { return( shift->_packing_method( 'sereal' ) ); }
Subroutine sereal: not called CC=1 SCAR=6.9
Pod documented
468  
469 11 ▶sub serial { return( shift->_set_get_scalar( 'serial', @_ ) ); }
Subroutine serial: called CC=1 SCAR=0.0
Pod documented
470  
471 0 ▶sub serialiser { return( shift->_set_get_scalar( '_packing_method', @_ ) ); }
Subroutine serialiser: not called CC=1 SCAR=6.9
Pod documented
472  
473 {
474 5 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
475 5 *serializer = \&serialiser;
476 }
477  
478 13 ▶sub size { return( shift->_set_get_scalar( 'size', @_ ) ); }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
479  
480 sub stat
481 0 ▶{
Subroutine stat: not called CC=3 SCAR=24.8
Pod documented
482 0 my $self = shift( @_ );
483 0 ▶ my $file = $self->{_cache_file} || return(0);
Condition0%0/2
$self->{'_cache_file'} || (return 0)
l!l
00
MC/DC0%0/1
$self->{'_cache_file'} || (return 0)
$self->{'_cache_file'}
Truth table0%0/2
A
A= $self->{'_cache_file'}
Aresult
11
00
484 0 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 0 0
485 0 return( $file->stat );
486 }
487  
488 0 ▶sub storable { return( shift->_packing_method( 'storable' ) ); }
Subroutine storable: not called CC=1 SCAR=6.9
Pod documented
489  
490 0 ▶sub supported { return(1); }
Subroutine supported: not called CC=1 SCAR=6.9
Pod documented
491  
492 0 ▶sub tmpdir { return( shift->_set_get_file( 'tmpdir', @_ ) ); }
Subroutine tmpdir: not called CC=1 SCAR=6.9
Pod documented
493  
494 sub unlock
495 4 ▶{
Subroutine unlock: called CC=3 SCAR=11.4
Pod documented
496 4 my $self = shift( @_ );
497 my $file = $self->{_cache_file} ||
498 4 ▶ return( $self->error( "No cache file is set yet. Have you first opened the cache file with open()?" ) );
Condition50%1/2
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
l!l
40
MC/DC0%0/1
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
$self->{'_cache_file'}
Truth table50%1/2
A
A= $self->{'_cache_file'}
Aresult
11
00
499 4 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 4 4
500 4 return( $file->unlock );
501 }
502  
503 sub write
504 4 ▶{
Subroutine write: called CC=29 SCAR=55.1
Pod documented
505 4 my $self = shift( @_ );
506 4 my $data;
507 4 ▶ if( scalar( @_ ) == 1 && ref( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0]) { } 4 0 4
Condition33%1/3
scalar @_ == 1 and ref $_[0]
!ll&&!rl&&r
004
MC/DC0%0/2
scalar @_ == 1 and ref $_[0]
scalar @_ == 1 ref $_[0]
Truth table33%1/3
A and B
A= scalar @_ == 1B= ref $_[0]
ABresult
0X0
100
111
508 {
509 4 $data = shift( @_ );
510 }
511 else
512 {
513 0 $data = \join( '', @_ );
514 }
515 my $file = $self->{_cache_file} ||
516 4 ▶ return( $self->error( "No cache file is set yet. Have you first opened the cache file with open()?" ) );
Condition50%1/2
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
l!l
40
MC/DC0%0/1
$self->{'_cache_file'} || (return $self->error('No cache file is set yet. Have you first opened the cache file with open()?'))
$self->{'_cache_file'}
Truth table50%1/2
A
A= $self->{'_cache_file'}
Aresult
11
00
517 4 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 4 4
518 # The cache file does not exists; maybe it has been removed by a destroyed process ?
519 4 ▶ if( !$file->exists )
Branch: 1 branch
BranchTrueFalseTotal
unless ($file->exists) 0 4 4
520 {
521 0 $self->removed(1);
522 0 return( $self );
523 }
524 4 my $size = 0;
525 4 ▶ $size = $self->size // 0;
Condition50%1/2
$self->size // 0
l!l
40
MC/DC0%0/1
$self->size // 0
$self->size
Truth table50%1/2
A
A= $self->size
Aresult
11
00
526 4 ▶ $size = int( "${size}" ) if( defined( $size ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $size 4 0 4
527 4 my $packing = $self->_packing_method;
528 4 my $encoded;
529 4 ▶ if( $packing eq 'json' )
Branch: 3 branches
BranchTrueFalseTotal
if ($packing eq 'json') { } 4 0 4
elsif ($packing eq 'cbor') { } 0 0 0
elsif ($packing eq 'sereal') { } 0 0 0
530 {
531 4 $encoded = $self->_encode_json( $data );
532 }
533 elsif( $packing eq 'cbor' )
534 {
535 # try-catch
536 0 local $@;
537 eval
538 0 {
539 $encoded = $self->serialise( $data,
540 serialiser => 'CBOR::XS',
541 allow_sharing => 1,
542 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
543 );
544 };
545 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
546 {
547 0 return( $self->error( "An error occured encoding data provided using $packing: $@. Data was: '$data'" ) );
548 }
549 0 ▶ return( $self->pass_error ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
550 }
551 elsif( $packing eq 'sereal' )
552 {
553 0 ▶ $self->_load_class( 'Sereal::Encoder' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Sereal::Encoder") or return $self->pass_error 0 0 0
554 0 my $const;
555 0 ▶ $const = \&{"Sereal\::Encoder::SRL_ZLIB"} if( defined( &{"Sereal\::Encoder::SRL_ZLIB"} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"Sereal::Encoder::SRL_ZLIB";} 0 0 0
556 # try-catch
557 0 local $@;
558 eval
559 0 {
560 $encoded = $self->serialise( $data,
561 serialiser => 'Sereal',
562 freeze_callbacks => 1,
563 ( defined( $const ) ? ( compress => $const->() ) : () ),
564 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $const ? : 0 0 0
defined $self->{'base64'} ? : 0 0 0
565 );
566 };
567 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
568 {
569 0 return( $self->error( "An error occured encoding data provided using $packing: $@. Data was: '$data'" ) );
570 }
571 0 ▶ return( $self->pass_error ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
572 }
573 # Default to Storable::Improved
574 else
575 {
576 # local $Storable::forgive_me = 1;
577 # $encoded = Storable::Improved::freeze( $data );
578 # try-catch
579 0 local $@;
580 eval
581 0 {
582 $encoded = $self->serialise( $data,
583 serialiser => 'Storable::Improved',
584 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
585 );
586 };
587 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
588 {
589 0 return( $self->error( "An error occured encoding data provided using $packing: $@. Data was: '$data'" ) );
590 }
591 0 ▶ return( $self->pass_error ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
592 }
593  
594 # For some reason, I get warning that $size is uninitialised despite having initialised it and ensured it is defined
595 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
596 4 ▶ $size //= 0;
Condition50%1/2
$size //= 0
l!l
40
MC/DC0%0/1
$size //= 0
$size
Truth table50%1/2
A
A= $size
Aresult
11
00
597 4 ▶ if( defined( $size ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and $size > 0 and length $encoded > $size) 0 4 4
Condition33%2/6
defined $size and $size > 0 and length $encoded > $size
!ll&&!rl&&r
040
defined $size and $size > 0
!ll&&!rl&&r
004
MC/DC0%0/3
defined $size and $size > 0 and length $encoded > $size
defined $size $size > 0 length $encoded > $size
Truth table25%1/4
A and B and C
A= defined $sizeB= $size > 0C= length $encoded > $size
ABCresult
0XX0
10X0
1100
1111
598 ( $size > 0 ) &&
599 length( $encoded ) > $size )
600 {
601 0 return( $self->error( "Data to write are ", length( $encoded ), " bytes long and exceed the maximum you have set of '$size'." ) );
602 }
603  
604 # We use simple encapsulation to later remove irrelevant null-bytes and ensuring our data integrity
605 # FYI: MG = Module::Generic
606 # substr( $encoded, 0, 0, 'MG[' . length( $encoded ) . ']' );
607  
608 4 $file->lock( shared => 1 );
609 4 $file->seek(0,0);
610 4 $file->print( $encoded );
611  
612 # Ensure data is safely flushed to disk
613 4 ▶ $file->flush if( $file->can( 'flush' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $file->can("flush") 4 0 4
614 4 ▶ $file->sync if( $file->can( 'sync' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $file->can("sync") 4 0 4
615  
616 4 $file->truncate( $file->tell );
617 4 $file->unlock;
618 4 return( $self );
619 }
620  
621 sub _decode_json
622 5 ▶{
Subroutine _decode_json: called CC=4 SCAR=16.5
623 5 my $self = shift( @_ );
624 5 my $data = shift( @_ );
625 # Nothing to do
626 5 ▶ return( $data ) if( !defined( $data ) || !CORE::length( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and length $data 0 5 5
Condition33%1/3
defined $data and length $data
!ll&&!rl&&r
005
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table33%1/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
627 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
628 5 my $j = JSON->new->utf8->relaxed->allow_nonref;
629 5 my $seen = {};
630 5 my $crawl;
631 $crawl = sub
632 5 ▶ {
Subroutine __ANON__: called CC=11 SCAR=35.3
633 5 my $this = shift( @_ );
634 5 my $type = Scalar::Util::reftype( $this );
635 5 ▶ return( $this ) if( ( $type eq 'HASH' || $type eq 'ARRAY' ) && ++$seen->{ Scalar::Util::refaddr( $this ) } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1 0 5 5
Condition33%2/6
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
!ll&&!rl&&r
050
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
500
MC/DC0%0/3
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
$type eq "HASH" $type eq "ARRAY" ++$$seen{builtin::refaddr($this)} > 1
Truth table20%1/5
A or B and C
A= $type eq "HASH"B= $type eq "ARRAY"C= ++$$seen{builtin::refaddr($this)} > 1
ABCresult
00X0
1X00
0100
1X11
0111
636 5 ▶ if( $type eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 5 0 5
elsif ($type eq 'ARRAY') { } 0 0 0
637 {
638 # Found a former scalar reference, restore it
639 5 ▶ if( CORE::exists( $this->{__scalar_gen_shm} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"__scalar_gen_shm"}) 0 5 5
640 {
641 0 return( \$this->{__scalar_gen_shm} );
642 }
643  
644 5 foreach my $k ( keys( %$this ) )
645 {
646 12 ▶ next if( !ref( $this->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{$k} 12 0 12
647 0 $this->{ $k } = $crawl->( $this->{ $k } );
648 }
649 }
650 elsif( $type eq 'ARRAY' )
651 {
652 0 ▶ for( my $i = 0; $i < scalar( @$this ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$this) 0 0 0
653 {
654 0 ▶ next if( !ref( $this->[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->[$i] 0 0 0
655 0 $this->[$i] = $crawl->( $this->[$i] );
656 }
657 }
658 5 return( $this );
659 5 };
660  
661 # try-catch
662 5 local $@;
663 my $decoded = eval
664 5 {
665 5 $j->decode( $data );
666 };
667 5 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
668 {
669 0 return( $self->error( "An error occurred while trying to decode JSON data: $@" ) );
670 }
671 5 my $result = $crawl->( $decoded );
672 5 return( $result );
673 }
674  
675 # Purpose of this method is to recursively check the given data and change scalar reference if they are anything else than 1 or 0, otherwise JSON would complain
676 sub _encode_json
677 4 ▶{
Subroutine _encode_json: called CC=2 SCAR=10.1
678 4 my $self = shift( @_ );
679 4 my $data = shift( @_ );
680 4 my $seen = {};
681 4 my $crawl;
682 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
683 $crawl = sub
684 4 ▶ {
Subroutine __ANON__: called CC=15 SCAR=46.8
685 4 my $this = shift( @_ );
686 4 my $type = Scalar::Util::reftype( $this );
687 # Skip this reference if it is either hash or array and we have already seen it in order to avoid looping.
688 4 ▶ return( $this ) if( ( $type eq 'HASH' || $type eq 'ARRAY' ) && ++$seen->{ Scalar::Util::refaddr( $this ) } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1 0 4 4
Condition33%2/6
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
!ll&&!rl&&r
040
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
400
MC/DC0%0/3
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
$type eq "HASH" $type eq "ARRAY" ++$$seen{builtin::refaddr($this)} > 1
Truth table20%1/5
A or B and C
A= $type eq "HASH"B= $type eq "ARRAY"C= ++$$seen{builtin::refaddr($this)} > 1
ABCresult
00X0
1X00
0100
1X11
0111
689 4 ▶ if( $type eq 'HASH' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 4 0 4
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'SCALAR') { } 0 0 0
690 {
691 4 foreach my $k ( keys( %$this ) )
692 {
693 11 ▶ next if( !ref( $this->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{$k} 11 0 11
694 0 $this->{ $k } = $crawl->( $this->{ $k } );
695 }
696 }
697 elsif( $type eq 'ARRAY' )
698 {
699 0 ▶ for( my $i = 0; $i < scalar( @$this ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$this) 0 0 0
700 {
701 0 ▶ next if( !ref( $this->[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->[$i] 0 0 0
702 0 $this->[$i] = $crawl->( $this->[$i] );
703 }
704 }
705 elsif( $type eq 'SCALAR' )
706 {
707 # The only supported value by JSON for a scalar reference
708 0 ▶ return( $this ) if( $$this eq "1" or $$this eq "0" );
Branch: 1 branch
BranchTrueFalseTotal
if $$this eq 1 or $$this eq 0 0 0 0
Condition0%0/3
$$this eq 1 or $$this eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$$this eq 1 or $$this eq 0
$$this eq 1 $$this eq 0
Truth table0%0/3
A or B
A= $$this eq 1B= $$this eq 0
ABresult
1X1
011
000
709 0 my $pkg;
710 0 ▶ if( ( $pkg = Scalar::Util::blessed( $this ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg = builtin::blessed($this)) { } 0 0 0
711 {
712 0 ▶ if( overload::Method( $this => '""' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (overload::Method($this, '""')) { } 0 0 0
713 {
714 0 $this = { __scalar_gen_shm => "$this", __package => $pkg };
715 }
716 else
717 {
718 0 $this = { __scalar_gen_shm => $$this, __package => $pkg };
719 }
720 }
721 else
722 {
723 0 $this = { __scalar_gen_shm => $$this };
724 }
725 }
726 4 return( $this );
727 4 };
728 4 my $ref = $crawl->( $data );
729 4 my $j = JSON->new->utf8->relaxed->allow_nonref->convert_blessed;
730 # try-catch
731 4 local $@;
732 my $encoded = eval
733 4 {
734 4 $j->encode( $ref );
735 };
736 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
737 {
738 0 return( $self->error( "An error occurred while trying to JSON encode data: $@" ) );
739 };
740 4 return( $encoded );
741 }
742  
743 sub _fill
744 2 ▶{
Subroutine _fill: called CC=10 SCAR=24.3
745 2 my $self = shift( @_ );
746 2 my $size = shift( @_ );
747 2 ▶ my $file = shift( @_ ) || return( $self->error( "No file was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No file was provided.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No file was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
748 2 ▶ return( $self->error( "Cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 2 2
749 # The cache file does not exists; maybe it has been removed by a destroyed process ?
750 2 ▶ if( !$file->exists )
Branch: 1 branch
BranchTrueFalseTotal
unless ($file->exists) 0 2 2
751 {
752 0 $self->removed(1);
753 0 return(0);
754 }
755 2 ▶ return( $self->error( "Size value provided ($size) is not an integer." ) ) if( $size !~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /^\d+$/ 0 2 2
756 2 ▶ return(1) if( $size <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $size <= 0 0 2 2
757 2 ▶ return( $self->error( "File provided is not a Module::Generic::File file object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 2 2
758 2 $file->seek(0,0);
759 2 my $minimum = 32;
760 2 my $range = 96;
761 2 ▶ for( my $bytes = 0; $bytes < $size; $bytes += 4 )
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes < $size) 1024 2 1026
762 {
763 1024 my $rand = int( CORE::rand( $range ** 4 ) );
764 1024 my $string = '';
765 1024 for( 1..4 )
766 {
767 4096 $string .= chr( $rand % $range + $minimum );
768 4096 $rand = int( $rand / $range );
769 }
770 1024 ▶ $file->print( $string ) || return( $self->pass_error( $file->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$file->print($string) or return $self->pass_error($file->error) 1024 0 1024
771 }
772 2 $file->truncate( $file->tell );
773 2 return(1);
774 }
775  
776 15 ▶sub _packing_method { return( shift->_set_get_scalar( '_packing_method', @_ ) ); }
Subroutine _packing_method: called CC=1 SCAR=0.0
777  
778 sub _str2key
779 11 ▶{
Subroutine _str2key: called CC=8 SCAR=24.1
780 11 my $self = shift( @_ );
781 11 my $key = shift( @_ );
782 5 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
783 11 ▶ if( !defined( $key ) || $key eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key or $key eq "") 0 11 11
Condition33%1/3
not defined $key or $key eq ""
l!l&&r!l&&!r
0011
MC/DC0%0/2
not defined $key or $key eq ""
not defined $key $key eq ""
Truth table33%1/3
not(A) or B
A= not defined $keyB= $key eq ""
ABresult
1X1
011
000
784 {
785 0 return( Data::UUID->new->create_str );
786 }
787 # We do not actually use any path, but this is for standardisation with Module::Generic::SharedMem
788 11 my $path;
789 11 ▶ ( $key, $path ) = ref( $key ) eq 'ARRAY' ? @$key : ( $key, [getpwuid($>)]->[7] );
Branch: 1 branch
BranchTrueFalseTotal
ref $key eq 'ARRAY' ? : 0 11 11
790 11 ▶ $path = [getpwuid($path)]->[7] if( $path =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /^\d+$/ 0 11 11
791 11 ▶ if( $key =~ /^\d+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\d+$/) { } 0 11 11
792 {
793 0 ▶ my $id = $self->ftok( $key ) ||
Condition0%0/2
$self->ftok($key) || (return $self->error("Unable to get a key using IPC::SysV::ftok: $!"))
l!l
00
MC/DC0%0/1
$self->ftok($key) || (return $self->error("Unable to get a key using IPC::SysV::ftok: $!"))
$self->ftok($key)
Truth table0%0/2
A
A= $self->ftok($key)
Aresult
11
00
794 return( $self->error( "Unable to get a key using IPC::SysV::ftok: $!" ) );
795 0 return( $id );
796 }
797 else
798 {
799 11 my $id = 0;
800 11 $id += $_ for( unpack( "C*", $key ) );
801 11 my $val = $self->ftok( $id );
802 11 return( $val );
803 }
804 }
805  
806 sub DESTROY
807 11 ▶{
Subroutine DESTROY: called CC=22 SCAR=35.6
808 # <https://perldoc.perl.org/perlobj#Destructors>
809 11 CORE::local( $., $@, $!, $^E, $? );
810 11 ▶ CORE::return if( Module::Generic::_in_global_destruction() );
Branch: 1 branch
BranchTrueFalseTotal
if Module::Generic::_in_global_destruction() 0 11 11
811 11 my $self = CORE::shift( @_ );
812 11 ▶ CORE::return if( !CORE::defined( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 11 11
813 11 ▶ CORE::return unless( CORE::exists( $self->{_cache_file} ) && CORE::defined( $self->{_cache_file} ) && CORE::length( $self->{_cache_file} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_cache_file"} and defined $self->{"_cache_file"} and length $self->{"_cache_file"} 8 3 11
Condition50%3/6
exists $self->{"_cache_file"} and defined $self->{"_cache_file"} and length $self->{"_cache_file"}
!ll&&!rl&&r
083
exists $self->{"_cache_file"} and defined $self->{"_cache_file"}
!ll&&!rl&&r
0011
MC/DC33%1/3
exists $self->{"_cache_file"} and defined $self->{"_cache_file"} and length $self->{"_cache_file"}
exists $self->{"_cache_file"} defined $self->{"_cache_file"} length $self->{"_cache_file"}
Truth table50%2/4
A and B and C
A= exists $self->{"_cache_file"}B= defined $self->{"_cache_file"}C= length $self->{"_cache_file"}
ABCresult
0XX0
10X0
1100
1111
814 3 my $prefix = __PACKAGE__ . '::DESTROY';
815 3 my $cache_file;
816 3 ▶ $cache_file = $self->{_cache_file} if( CORE::length( $self->{_cache_file} ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"_cache_file"} 3 0 3
817 3 ▶ CORE::return if( !defined( $cache_file ) || !$self->_is_a( $cache_file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache_file and $self->_is_a($cache_file, "Module::Generic::File") 0 3 3
Condition33%1/3
defined $cache_file and $self->_is_a($cache_file, "Module::Generic::File")
!ll&&!rl&&r
003
MC/DC0%0/2
defined $cache_file and $self->_is_a($cache_file, "Module::Generic::File")
defined $cache_file $self->_is_a($cache_file, "Module::Generic::File")
Truth table33%1/3
A and B
A= defined $cache_fileB= $self->_is_a($cache_file, "Module::Generic::File")
ABresult
0X0
100
111
818 3 my $fname = "$cache_file";
819 3 ▶ print( STDERR "\t${prefix}: Destroying object with cache file \"${fname}\"\n" ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 3 3
820 3 $self->unlock;
821  
822 3 my $class = CORE::ref( $self );
823 3 my $repo = Module::Generic::Global->new( 'cache' => $class, key => $class );
824 3 my $file2obj_repo = Module::Generic::Global->new( 'file2object_repo' => $class, key => $class );
825 3 $file2obj_repo->lock;
826 3 my $f2o_data = $file2obj_repo->get;
827 3 ▶ if( defined( $f2o_data ) && ref( $f2o_data // '' ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f2o_data and ref($f2o_data // "") eq "HASH") 3 0 3
Condition40%2/5
defined $f2o_data and ref($f2o_data // "") eq "HASH"
!ll&&!rl&&r
003
$f2o_data // ""
l!l
30
MC/DC0%0/2
defined $f2o_data and ref($f2o_data // "") eq "HASH"
defined $f2o_data $f2o_data
Truth table33%1/3
A and B
A= defined $f2o_dataB= $f2o_data
ABresult
0X0
100
111
828 {
829 3 my $ref = $f2o_data->{ $fname };
830  
831 3 ▶ if( defined( $ref ) && ref( $ref // '' ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref and ref($ref // '') eq 'ARRAY') { } 3 0 3
Condition40%2/5
defined $ref and ref($ref // "") eq "ARRAY"
!ll&&!rl&&r
003
$ref // ""
l!l
30
MC/DC0%0/2
defined $ref and ref($ref // "") eq "ARRAY"
defined $ref $ref
Truth table33%1/3
A and B
A= defined $refB= $ref
ABresult
0X0
100
111
832 {
833 3 ▶ printf( STDERR "\t${prefix}: %d objects found for cache file \"${fname}\"\n", scalar( @$ref ) ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 3 3
834 3 my $addr = Scalar::Util::refaddr( $self );
835 3 ▶ for( my $i = 0; $i <= $#$ref; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $ref->$#*) 5 3 8
836 {
837 5 ▶ if( $ref->[$i] eq $addr )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->[$i] eq $addr) 2 3 5
838 {
839 2 splice( @$ref, $i, 1 );
840 2 $i--;
841 }
842 }
843 3 ▶ if( !scalar( @$ref ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$ref) 1 2 3
844 {
845 1 CORE::delete( $f2o_data->{ $fname } );
846 }
847 }
848 else
849 {
850 0 CORE::delete( $f2o_data->{ $fname } );
851 }
852 3 $file2obj_repo->set( $f2o_data );
853 3 $file2obj_repo->unlock;
854 }
855 # For non-object context, we need to call cleanup to ensure there is no leftover
856 3 $repo->cleanup;
857 3 $file2obj_repo->cleanup;
858  
859 # Are we expected to clean up and remove the cache file?
860 3 ▶ if( $self->destroy )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->destroy) 0 3 3
861 {
862 0 ▶ print( STDERR "\t${prefix}: No object left, removing cache file \"${fname}\"\n" ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 0 0
863 0 $self->remove;
864 }
865 };
866  
867 sub FREEZE
868 6 ▶{
Subroutine FREEZE: called CC=6 SCAR=18.3
Pod undocumented
869 6 my $self = CORE::shift( @_ );
870 6 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition50%1/2
shift @_ // ""
l!l
60
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
871 6 my $class = CORE::ref( $self );
872 6 my @props = qw(
873 binmode base64 create destroy exclusive id key locked mode owner removed serial size
874 _packing_method _cache_dir _cache_file
875 );
876 6 my $hash = {};
877 6 foreach my $prop ( @props )
878 {
879 96 ▶ if( exists( $self->{ $prop } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$prop}) 96 0 96
880 {
881 96 $hash->{ $prop } = $self->{ $prop };
882 }
883 }
884 # Return an array reference rather than a list so this works with Sereal and CBOR
885 # On or before Sereal version 4.023, Sereal did not support multiple values returned
886 6 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 1 5 6
887 {
888 1 require Sereal::Encoder;
889 1 require version;
890
891 1 ▶ if( version->parse( Sereal::Encoder->VERSION ) <= version->parse( '4.023' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse("Sereal::Encoder"->VERSION) <= "version"->parse("4.023")) 0 1 1
892 {
893 0 CORE::return( [$class, $hash] );
894 }
895 }
896 # But Storable want a list with the first element being the serialised element
897 6 CORE::return( $class, $hash );
898 }
899  
900 5 ▶sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
901  
902 2 ▶sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
903  
904 # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
905 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
906 sub THAW
907 3 ▶{
Subroutine THAW: called CC=10 SCAR=27.6
Pod undocumented
908 3 my( $self, undef, @args ) = @_;
909 3 ▶ my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 && ref $args[0] eq 'ARRAY' ? : 0 3 3
Condition33%1/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
910 3 ▶ my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && ref $ref eq 'ARRAY' && scalar @$ref > 1 ? : 3 0 3
Condition22%2/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
003
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
003
ref $self or $self
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
defined $ref ref $ref eq "ARRAY" scalar @$ref > 1
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table25%1/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= scalar @$ref > 1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
911 3 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 3 0 3
912 # Storable pattern requires to modify the object it created rather than returning a new one
913 3 my $new;
914 3 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 2 1 3
915 {
916 2 foreach( CORE::keys( %$hash ) )
917 {
918 32 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
919 }
920 2 $new = $self;
921 }
922 else
923 {
924 1 $new = CORE::bless( $hash => $class );
925 }
926 3 CORE::return( $new );
927 }
928  
929 # NOTE: END
930 END
931 5 ▶{
Subroutine END: called CC=42 SCAR=73.9
932 5 my $repo = Module::Generic::Global->new( 'cache' => __PACKAGE__, key => __PACKAGE__ );
933 5 ▶ if( Module::Generic::_in_end_phase() || Module::Generic::_in_global_destruction() )
Branch: 1 branch
BranchTrueFalseTotal
if (Module::Generic::_in_end_phase() or Module::Generic::_in_global_destruction()) 5 0 5
Condition33%1/3
Module::Generic::_in_end_phase() or Module::Generic::_in_global_destruction()
l!l&&r!l&&!r
500
MC/DC0%0/2
Module::Generic::_in_end_phase() or Module::Generic::_in_global_destruction()
Module::Generic::_in_end_phase() Module::Generic::_in_global_destruction()
Truth table33%1/3
A or B
A= Module::Generic::_in_end_phase()B= Module::Generic::_in_global_destruction()
ABresult
1X1
011
000
934 {
935 5 return;
936 }
937  
938 0 my $repo_data;
939 0 ▶ if( $repo )
Branch: 1 branch
BranchTrueFalseTotal
if ($repo) { } 0 0 0
940 {
941 0 $repo_data = $repo->get;
942 0 ▶ return unless( defined( $repo_data ) && ref( $repo_data ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $repo_data and ref $repo_data eq "ARRAY" 0 0 0
Condition0%0/3
defined $repo_data and ref $repo_data eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $repo_data and ref $repo_data eq "ARRAY"
defined $repo_data ref $repo_data eq "ARRAY"
Truth table0%0/3
A and B
A= defined $repo_dataB= ref $repo_data eq "ARRAY"
ABresult
0X0
100
111
943 }
944 else
945 {
946 0 ▶ warn( "Error instantiating a new Module::Generic::Global object for 'cache': ", ( Module::Generic::Global->error // '' ) );
Condition0%0/2
"Module::Generic::Global"->error // ""
l!l
00
MC/DC0%0/1
"Module::Generic::Global"->error // ""
"Module::Generic::Global"->error
Truth table0%0/2
A
A= "Module::Generic::Global"->error
Aresult
11
00
947 0 return;
948 }
949 0 my $file2obj_repo = Module::Generic::Global->new( 'file2object_repo' => __PACKAGE__, key => __PACKAGE__ );
950 0 my $f2o_data;
951 0 ▶ if( $file2obj_repo )
Branch: 1 branch
BranchTrueFalseTotal
if ($file2obj_repo) { } 0 0 0
952 {
953 0 $f2o_data = $file2obj_repo->get;
954 }
955 else
956 {
957 0 warn( "Error instantiating a new Module::Generic::Global object for 'file2object_repo': ", Module::Generic::Global->error );
958 0 return;
959 }
960  
961 0 my $prefix = __PACKAGE__ . '::END';
962 0 ▶ printf( STDERR "${prefix}: %d objects in repo to check.\n", scalar( @$repo_data ) ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 0 0
963  
964 0 foreach my $cache ( @$repo_data )
965 {
966 # Only allow the parent process to clean up
967 0 ▶ my $pid = $cache->owner // '';
Condition0%0/2
$cache->owner // ""
l!l
00
MC/DC0%0/1
$cache->owner // ""
$cache->owner
Truth table0%0/2
A
A= $cache->owner
Aresult
11
00
968 0 ▶ next if( $pid ne $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $pid ne $$ 0 0 0
969  
970 0 ▶ if( !Scalar::Util::blessed( $cache // '' ) ||
Branch: 3 branches
BranchTrueFalseTotal
if (not builtin::blessed($cache // '') or ref($cache // '') ne 'Module::Generic::File::Cache') { } 0 0 0
elsif (not defined $cache && ref $cache or builtin::blessed($cache) and not $cache->can('remove')) { } 0 0 0
elsif (not builtin::blessed($cache->{'_cache_file'}) or builtin::blessed($cache->{'_cache_file'}) and not $cache->{'_cache_file'}->isa('Module::Generic::File')) { } 0 0 0
Condition0%0/7
not builtin::blessed($cache // "") or ref($cache // "") ne "Module::Generic::File::Cache"
l!l&&r!l&&!r
000
$cache // ""
l!l
00
$cache // ""
l!l
00
MC/DC0%0/2
not builtin::blessed($cache // "") or ref($cache // "") ne "Module::Generic::File::Cache"
$cache $cache
Truth table0%0/3
not(A) or B
A= $cacheB= $cache
ABresult
0X1
111
100
971 ref( $cache // '' ) ne 'Module::Generic::File::Cache' )
972 {
973 0 warn( "${prefix}: Object found in object repo is not a Module::Generic::File::Cache object." );
974 0 next;
975 }
976 ▶ elsif( !defined( $cache ) ||
Condition0%0/9
not defined $cache && ref $cache or builtin::blessed($cache) and not $cache->can("remove")
l!l&&r!l&&!r
000
defined $cache && ref $cache
!ll&&!rl&&r
000
builtin::blessed($cache) and not $cache->can("remove")
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $cache && ref $cache or builtin::blessed($cache) and not $cache->can("remove")
defined $cache ref $cache builtin::blessed($cache) not $cache->can("remove")
Truth table0%0/5
not(A && B) or C and not(D)
A= defined $cacheB= ref $cacheC= builtin::blessed($cache)D= not $cache->can("remove")
ABCDresult
0XXX1
10XX1
11111
110X0
11100
977 !ref( $cache ) ||
978 ( Scalar::Util::blessed( $cache ) && !$cache->can( 'remove' ) ) )
979 {
980 0 next;
981 }
982 ▶ elsif( !Scalar::Util::blessed( $cache->{_cache_file} ) ||
Condition0%0/6
not builtin::blessed($cache->{"_cache_file"}) or builtin::blessed($cache->{"_cache_file"}) and not $cache->{"_cache_file"}->isa("Module::Generic::File")
l!l&&r!l&&!r
000
builtin::blessed($cache->{"_cache_file"}) and not $cache->{"_cache_file"}->isa("Module::Generic::File")
!ll&&!rl&&r
000
MC/DC0%0/3
not builtin::blessed($cache->{"_cache_file"}) or builtin::blessed($cache->{"_cache_file"}) and not $cache->{"_cache_file"}->isa("Module::Generic::File")
not builtin::blessed($cache->{"_cache_file"}) builtin::blessed($cache->{"_cache_file"}) not $cache->{"_cache_file"}->isa("Module::Generic::File")
Truth table0%0/4
not(A) or B and not(C)
A= not builtin::blessed($cache->{"_cache_file"})B= builtin::blessed($cache->{"_cache_file"})C= not $cache->{"_cache_file"}->isa("Module::Generic::File")
ABCresult
1XX1
0111
00X0
0100
983 (
984 Scalar::Util::blessed( $cache->{_cache_file} ) &&
985 !$cache->{_cache_file}->isa( 'Module::Generic::File' )
986 ) )
987 {
988 0 warn( "${prefix}: File object found in _cache_file (", overload::StrVal( $cache->{_cache_file} ), ") is actually not a Module::Generic::File, which is weird." );
989 0 next;
990 }
991  
992 0 my $fname = "$cache->{_cache_file}";
993 0 ▶ print( STDERR "\t${prefix}: Cache file is: \"", ( $fname // 'undef' ), "\"\n" ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 0 0
Condition0%0/2
$fname // "undef"
l!l
00
MC/DC0%0/1
$fname // "undef"
$fname
Truth table0%0/2
A
A= $fname
Aresult
11
00
994 0 ▶ next if( !length( $fname // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length($fname // "") 0 0 0
Condition0%0/2
$fname // ""
l!l
00
MC/DC0%0/1
$fname // ""
$fname
Truth table0%0/2
A
A= $fname
Aresult
11
00
995  
996 # An object refaddr
997 0 my $id = $cache->id;
998 0 ▶ if( !CORE::length( $id // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length($id // "")) 0 0 0
Condition0%0/2
$id // ""
l!l
00
MC/DC0%0/1
$id // ""
$id
Truth table0%0/2
A
A= $id
Aresult
11
00
999 {
1000 0 ▶ print( STDERR "\t${prefix}: ID is missing for this object with cache file $fname\n" ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 0 0
1001 }
1002  
1003 # Check for other references to the same file
1004 0 ▶ if( defined( $f2o_data ) && ref( $f2o_data ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f2o_data and ref $f2o_data eq 'HASH') { } 0 0 0
Condition0%0/3
defined $f2o_data and ref $f2o_data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $f2o_data and ref $f2o_data eq "HASH"
defined $f2o_data ref $f2o_data eq "HASH"
Truth table0%0/3
A and B
A= defined $f2o_dataB= ref $f2o_data eq "HASH"
ABresult
0X0
100
111
1005 {
1006 0 ▶ if( exists( $f2o_data->{ $fname } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $f2o_data->{$fname}) 0 0 0
1007 {
1008 0 ▶ my $ref = $f2o_data->{ $fname } || next;
Condition0%0/2
$f2o_data->{$fname} || (next)
l!l
00
MC/DC0%0/1
$f2o_data->{$fname} || (next)
$f2o_data->{$fname}
Truth table0%0/2
A
A= $f2o_data->{$fname}
Aresult
11
00
1009 0 ▶ next if( ref( $ref ) ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref ne "ARRAY" 0 0 0
1010 0 ▶ printf( STDERR "\t${prefix}: %d objects associated with cache file \"${fname}\".\n", scalar( @$ref ) ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 0 0
1011 0 ▶ @$ref = grep{ CORE::length( $_ // '' ) && $_ != $id } @$ref;
Condition0%0/5
length($_ // '') && $_ != $id
!ll&&!rl&&r
000
$_ // ""
l!l
00
MC/DC0%0/2
length($_ // '') && $_ != $id
$_ $_ != $id
Truth table0%0/3
A && B
A= $_B= $_ != $id
ABresult
0X0
100
111
1012 # Only remove file if no one else is using it
1013 0 ▶ unless( @$ref )
Branch: 1 branch
BranchTrueFalseTotal
unless (@$ref) 0 0 0
1014 {
1015 0 ▶ if( $cache->destroy && !$cache->removed )
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->destroy and not $cache->removed) 0 0 0
Condition0%0/3
$cache->destroy and not $cache->removed
!ll&&!rl&&r
000
MC/DC0%0/2
$cache->destroy and not $cache->removed
$cache->destroy not $cache->removed
Truth table0%0/3
A and not(B)
A= $cache->destroyB= not $cache->removed
ABresult
0X0
100
111
1016 {
1017 0 ▶ print( STDERR "\t${prefix}: Removing cache file \"${fname}\"\n" ) if( $DEBUG >= 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Cache::DEBUG >= 4 0 0 0
1018 0 $cache->remove;
1019 }
1020 0 delete( $f2o_data->{ $fname } );
1021 }
1022 }
1023 }
1024 else
1025 {
1026 0 $cache->remove;
1027 }
1028 }
1029 };
1030  
1031 5 1;
1032 # NOTE: POD
1033 __END__
« lib/Module/Generic/File/Mmap.pm ↑ Summary lib/Module/Generic/Dynamic.pm »