lib/Module/Generic/SharedMem.pm

filter: Statementstmt 66.3% 571 / 861 Branchbran 33.7% 147 / 436 Conditioncond 25.0% 60 / 240 MC/DCmcdc 2.3% 3 / 128 Subroutinesub 64.7% 92 / 142 Podpod 91.3% 42 / 46 totaltotal 49.3% 915 / 1853 CC 313 SCAR 45.3
CC
313
Cov
51%
write
524.3
remove_semaphore
338.4
open
151.2
CRAP 92.9
« lib/Module/Generic/SharedMemXS.pm ↑ Summary lib/Module/Generic/Scalar.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/SharedMem.pm
3 ## Version v0.6.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/01/18
7 ## Modified 2026/09/25
8 ## All rights reserved
9 ## This program is free software; you can redistribute it and/or modify it
10 ## under the same terms as Perl itself.
11 ##----------------------------------------------------------------------------
12 package Module::Generic::SharedMem;
13 BEGIN
14 6 ▶{
Subroutine BEGIN: called CC=15 SCAR=29.9
15 6 ▶ use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 warnings::register_categories( 'Module::Generic' );
19 6 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶ use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 $SUPPORTED_RE $SYSV_SUPPORTED $SEMOP_ARGS $N $HAS_B64
22 6 );
23 6 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶ use Errno qw( EINVAL EIDRM );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶ use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 # This is disruptive for everybody. Bad idea.
28 # use JSON 4.03 qw( -convert_blessed_universally );
29 6 ▶ use JSON 4.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶ use Module::Generic::Global ':const';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶ use Storable::Improved ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 SHM_BUFSIZ => 65536,
34 SEM_LOCKER => 0,
35 SEM_MARKER => 0,
36 SHM_LOCK_WAIT => 0,
37 SHM_LOCK_EX => 1,
38 SHM_LOCK_UN => -1,
39 SHM_EXISTS => 1,
40 LOCK_SH => 1,
41 LOCK_EX => 2,
42 LOCK_NB => 4,
43 LOCK_UN => 8,
44 6 };
45 # if( $^O =~ /^(?:Android|cygwin|dos|MSWin32|os2|VMS|riscos)/ )
46 # Even better
47 6 $SUPPORTED_RE = qr/IPC\/SysV/;
48 6 ▶ if( $Config{extensions} =~ /$SUPPORTED_RE/ &&
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Generic::SharedMem::Config{'extensions'} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{'d_msg'} eq 'define' and $Module::Generic::SharedMem::Config{'d_sem'} eq 'define' and $Module::Generic::SharedMem::Config{'d_semctl'} eq 'define' and $Module::Generic::SharedMem::Config{'d_semget'} eq 'define' and $Module::Generic::SharedMem::Config{'d_semop'} eq 'define' and $Module::Generic::SharedMem::Config{'d_shm'} eq 'define' and $Module::Generic::SharedMem::Config{'d_shmat'} eq 'define' and $Module::Generic::SharedMem::Config{'d_shmctl'} eq 'define' and $Module::Generic::SharedMem::Config{'d_shmdt'} eq 'define' and $Module::Generic::SharedMem::Config{'d_shmget'} eq 'define') { } 6 0 6
Condition33%11/33
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define" and $Module::Generic::SharedMem::Config{"d_semop"} eq "define" and $Module::Generic::SharedMem::Config{"d_shm"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmat"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmdt"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmget"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define" and $Module::Generic::SharedMem::Config{"d_semop"} eq "define" and $Module::Generic::SharedMem::Config{"d_shm"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmat"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmdt"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define" and $Module::Generic::SharedMem::Config{"d_semop"} eq "define" and $Module::Generic::SharedMem::Config{"d_shm"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmat"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmctl"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define" and $Module::Generic::SharedMem::Config{"d_semop"} eq "define" and $Module::Generic::SharedMem::Config{"d_shm"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmat"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define" and $Module::Generic::SharedMem::Config{"d_semop"} eq "define" and $Module::Generic::SharedMem::Config{"d_shm"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define" and $Module::Generic::SharedMem::Config{"d_semop"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define"
!ll&&!rl&&r
006
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu
!ll&&!rl&&r
006
MC/DC0%0/12
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u and not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu and $Module::Generic::SharedMem::Config{"d_msg"} eq "define" and $Module::Generic::SharedMem::Config{"d_sem"} eq "define" and $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_semget"} eq "define" and $Module::Generic::SharedMem::Config{"d_semop"} eq "define" and $Module::Generic::SharedMem::Config{"d_shm"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmat"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmctl"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmdt"} eq "define" and $Module::Generic::SharedMem::Config{"d_shmget"} eq "define"
$Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/u not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iu $Module::Generic::SharedMem::Config{"d_msg"} eq "define" $Module::Generic::SharedMem::Config{"d_sem"} eq "define" $Module::Generic::SharedMem::Config{"d_semctl"} eq "define" $Module::Generic::SharedMem::Config{"d_semget"} eq "define" $Module::Generic::SharedMem::Config{"d_semop"} eq "define" $Module::Generic::SharedMem::Config{"d_shm"} eq "define" $Module::Generic::SharedMem::Config{"d_shmat"} eq "define" $Module::Generic::SharedMem::Config{"d_shmctl"} eq "define" $Module::Generic::SharedMem::Config{"d_shmdt"} eq "define" $Module::Generic::SharedMem::Config{"d_shmget"} eq "define"
Truth table7%1/13
A and not(B) and C and D and E and F and G and H and I and J and K and L
A= $Module::Generic::SharedMem::Config{"extensions"} =~ /$Module::Generic::SharedMem::SUPPORTED_RE/uB= not $^O =~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/iuC= $Module::Generic::SharedMem::Config{"d_msg"} eq "define"D= $Module::Generic::SharedMem::Config{"d_sem"} eq "define"E= $Module::Generic::SharedMem::Config{"d_semctl"} eq "define"F= $Module::Generic::SharedMem::Config{"d_semget"} eq "define"G= $Module::Generic::SharedMem::Config{"d_semop"} eq "define"H= $Module::Generic::SharedMem::Config{"d_shm"} eq "define"I= $Module::Generic::SharedMem::Config{"d_shmat"} eq "define"J= $Module::Generic::SharedMem::Config{"d_shmctl"} eq "define"K= $Module::Generic::SharedMem::Config{"d_shmdt"} eq "define"L= $Module::Generic::SharedMem::Config{"d_shmget"} eq "define"
ABCDEFGHIJKLresult
0XXXXXXXXXXX0
10XXXXXXXXXX0
110XXXXXXXXX0
1110XXXXXXXX0
11110XXXXXXX0
111110XXXXXX0
1111110XXXXX0
11111110XXXX0
111111110XXX0
1111111110XX0
11111111110X0
1111111111100
1111111111111
49 $^O !~ /^(?:Android|dos|MSWin32|os2|VMS|riscos)/i &&
50 # we need semaphore and messages
51 $Config{d_msg} eq 'define' &&
52 $Config{d_sem} eq 'define' &&
53 $Config{d_semctl} eq 'define' &&
54 $Config{d_semget} eq 'define' &&
55 $Config{d_semop} eq 'define' &&
56 $Config{d_shm} eq 'define' &&
57 $Config{d_shmat} eq 'define' &&
58 $Config{d_shmctl} eq 'define' &&
59 $Config{d_shmdt} eq 'define' &&
60 $Config{d_shmget} eq 'define'
61 )
62 {
63 6 require IPC::SysV;
64 6 IPC::SysV->import( qw( IPC_RMID IPC_PRIVATE IPC_SET IPC_STAT IPC_CREAT IPC_EXCL IPC_NOWAIT
65 SEM_UNDO S_IRWXU S_IRWXG S_IRWXO S_IRUSR S_IWUSR
66 GETNCNT GETZCNT GETVAL SETVAL GETPID GETALL SETALL
67 shmat shmdt memread memwrite ftok ) );
68 6 $SYSV_SUPPORTED = 1;
69 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 6 eval( <<'EOT' );
71 our $SEMOP_ARGS =
72 {
73 (LOCK_EX) =>
74 [
75 1, 0, 0, # wait for readers to finish
76 2, 0, 0, # wait for writers to finish
77 2, 1, SEM_UNDO, # assert write lock
78 ],
79 (LOCK_EX | LOCK_NB) =>
80 [
81 1, 0, IPC_NOWAIT, # wait for readers to finish
82 2, 0, IPC_NOWAIT, # wait for writers to finish
83 2, 1, (SEM_UNDO | IPC_NOWAIT), # assert write lock
84 ],
85 (LOCK_EX | LOCK_UN) =>
86 [
87 2, -1, (SEM_UNDO | IPC_NOWAIT),
88 ],
89 (LOCK_SH) =>
90 [
91 2, 0, 0, # wait for writers to finish
92 1, 1, SEM_UNDO, # assert shared read lock
93 ],
94 (LOCK_SH | LOCK_NB) =>
95 [
96 2, 0, IPC_NOWAIT, # wait for writers to finish
97 1, 1, (SEM_UNDO | IPC_NOWAIT), # assert shared read lock
98 ],
99 (LOCK_SH | LOCK_UN) =>
100 [
101 1, -1, (SEM_UNDO | IPC_NOWAIT), # remove shared read lock
102 ],
103 };
104 EOT
105 6 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
106 {
107 0 warn( "Error while trying to eval \$SEMOP_ARGS: $@\n" );
108 }
109 }
110 else
111 {
112 0 $SYSV_SUPPORTED = 0;
113 }
114 # Credits IPC::SysV
115 6 ▶ $N = do { my $foo = eval { pack "L!", 0 }; $@ ? '' : '!' };
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 6 6
116  
117 6 our @EXPORT_OK = qw(LOCK_EX LOCK_SH LOCK_NB LOCK_UN);
118 6 our %EXPORT_TAGS = (
119 all => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
120 lock => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
121 'flock' => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
122 );
123 6 our $VERSION = 'v0.6.1';
124 };
125  
126 # use strict;
127 6 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 sub init
130 11 ▶{
Subroutine init: called CC=3 SCAR=11.0
Pod documented
131 11 my $self = shift( @_ );
132 11 $self->{base64} = undef;
133 # Default action when accessing a shared memory? If 1, it will create it if it does not exist already
134 11 $self->{create} = 0;
135 # If true, this will destroy both the shared memory and the semaphore upon end
136 11 $self->{destroy} = 0;
137 # If true, this will destroy only the semaphore upon end
138 11 $self->{destroy_semaphore} = 0;
139 11 $self->{exclusive} = 0;
140 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 11 ▶ $self->{key} = &IPC::SysV::IPC_PRIVATE if( $SYSV_SUPPORTED );
Branch: 1 branch
BranchTrueFalseTotal
if $SYSV_SUPPORTED 11 0 11
142 11 $self->{mode} = 0666;
143 11 $self->{serial} = '';
144 # SHM_BUFSIZ
145 11 $self->{size} = SHM_BUFSIZ;
146 11 $self->{_init_strict_use_sub} = 1;
147 # $self->{_packing_method} = 'storable';
148 # Storable keps breaking :(
149 # I leave the feature of using it as a choice to the user, but defaults to JSON
150 11 $self->{_packing_method} = 'json';
151 11 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 11 0 11
152 11 $self->{addr} = undef();
153 11 $self->{id} = undef();
154 11 $self->{locked} = 0;
155 11 $self->{owner} = $$;
156 11 $self->{removed} = 0;
157 11 $self->{removed_semaphore} = 0;
158 11 $self->{semid} = undef();
159 11 return( $self );
160 }
161  
162 28 ▶sub addr { return( shift->_set_get_scalar( 'addr', @_ ) ); }
Subroutine addr: called CC=1 SCAR=0.0
Pod documented
163  
164 # This class does not convert to an HASH
165 0 ▶sub as_hash { return( $_[0] ); }
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
166  
167 sub attach
168 0 ▶{
Subroutine attach: not called CC=5 SCAR=34.0
Pod documented
169 0 my $self = shift( @_ );
170 0 my $flags = shift( @_ );
171 0 ▶ $flags = $self->flags if( !defined( $flags ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 0 0
172 0 my $addr = $self->addr;
173 0 ▶ return( $addr ) if( defined( $addr ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $addr 0 0 0
174 0 my $id = $self->id;
175 0 ▶ return( $self->error( "No shared memory id! Have you opened it first?" ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
176 0 $addr = shmat( $id, undef(), $flags );
177 0 ▶ return( $self->error( "Unable to attach to shared memory: $!" ) ) if( !defined( $addr ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 0 0 0
178 0 $self->addr( $addr );
179 0 return( $addr );
180 }
181  
182 0 ▶sub base64 { return( shift->_set_get_scalar( 'base64', @_ ) ); }
Subroutine base64: not called CC=1 SCAR=6.9
Pod documented
183  
184 0 ▶sub cbor { return( shift->_packing_method( 'cbor' ) ); }
Subroutine cbor: not called CC=1 SCAR=6.9
Pod documented
185  
186 17 ▶sub create { return( shift->_set_get_boolean( 'create', @_ ) ); }
Subroutine create: called CC=1 SCAR=0.0
Pod documented
187  
188 0 ▶sub delete { return( shift->remove( @_ ) ); }
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
189  
190 sub destroy
191 20 ▶{
Subroutine destroy: called CC=2 SCAR=6.9
Pod documented
192 20 my $self = shift( @_ );
193 20 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 10 20
194 {
195 10 my $val = shift( @_ );
196 10 $self->_set_get_boolean( 'destroy', $val );
197 10 $self->_set_get_boolean( 'destroy_semaphore', $val );
198 }
199 20 return( $self->_set_get_boolean( 'destroy' ) );
200 }
201  
202 10 ▶sub destroy_semaphore { return( shift->_set_get_boolean( 'destroy_semaphore', @_ ) ); }
Subroutine destroy_semaphore: called CC=1 SCAR=0.0
Pod documented
203  
204 sub detach
205 2 ▶{
Subroutine detach: called CC=3 SCAR=16.7
Pod documented
206 2 my $self = shift( @_ );
207 2 my $addr = $self->addr;
208 2 ▶ return if( !defined( $addr ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 2 0 2
209 0 my $rv = shmdt( $addr );
210 0 ▶ return( $self->error( "Unable to detach from shared memory: $!" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
211 0 $self->addr( undef() );
212 0 return( $self );
213 }
214  
215 18 ▶sub exclusive { return( shift->_set_get_boolean( 'exclusive', @_ ) ); }
Subroutine exclusive: called CC=1 SCAR=0.0
Pod documented
216  
217 sub exists
218 9 ▶{
Subroutine exists: called CC=12 SCAR=35.0
Pod documented
219 9 my $self = shift( @_ );
220 9 my $opts = {};
221 9 ▶ if( ref( $_[0] ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 9 9
222 {
223 0 $opts = shift( @_ );
224 }
225 else
226 {
227 9 @$opts{ qw( key mode size ) } = @_;
228 }
229 9 ▶ $opts->{size} = $self->size unless( length( $opts->{size} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"size"} 9 0 9
230 9 $opts->{size} = int( $opts->{size} );
231 9 my $serial;
232 9 ▶ if( length( $opts->{key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts->{'key'}) { } 0 9 9
233 {
234 0 ▶ my $key_val = $self->_verify_key( $opts->{key} ) ||
Condition0%0/2
$self->_verify_key($opts->{'key'}) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_verify_key($opts->{'key'}) || (return $self->pass_error)
$self->_verify_key($opts->{'key'})
Truth table0%0/2
A
A= $self->_verify_key($opts->{'key'})
Aresult
11
00
235 return( $self->pass_error );
236 0 $serial = $self->_str2key( $key_val );
237 # $serial = $opts->{key};
238 }
239 else
240 {
241 9 $serial = $self->serial;
242 # $serial = $self->key;
243 }
244 9 my $flags = $self->flags({
245 create => 0,
246 exclusive => 0,
247 mode => 0644,
248 });
249 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
250 9 my $semid;
251 # try-catch
252 9 local $@;
253 my @rv = eval
254 9 {
255 9 $semid = semget( $serial, 0, $flags );
256 9 ▶ if( defined( $semid ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $semid) { } 0 9 9
257 {
258 0 my $arg = 0;
259 0 my $found = semctl( $semid, SEM_MARKER, &IPC::SysV::GETVAL, $arg );
260 0 ▶ return( $found == SHM_EXISTS ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$found == 1 ? : 0 0 0
261 }
262 else
263 {
264 9 ▶ return(0) if( $! =~ /\bNo[[:blank:]]+such[[:blank:]]+file\b/ );
Branch: 1 branch
BranchTrueFalseTotal
if $! =~ /\bNo[[:blank:]]+such[[:blank:]]+file\b/ 9 0 9
265 0 return;
266 }
267 };
268 9 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 9 9
269 {
270 # warn( "Trying to access shared memory triggered error: $e" ) if( $self->_warnings_is_enabled( 'Module::Generic' ) );
271 0 my $arg = 0;
272 0 ▶ if( defined( $semid ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $semid) 0 0 0
273 {
274 # try-catch
275 0 local $@;
276 0 ▶ if( !eval
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { semctl $semid, 0, &IPC::SysV::IPC_RMID, $arg } }) 0 0 0
277 {
278 0 semctl( $semid, 0, &IPC::SysV::IPC_RMID, $arg );
279 })
280 {
281 0 ▶ warn( "Error trying to remove semaphore id ${semid} after checking if shared memory existed: $@" ) if( $self->_warnings_is_enabled( 'Module::Generic' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled("Module::Generic") 0 0 0
282 }
283 }
284 0 return(0);
285 }
286 else
287 {
288 9 return( $rv[0] );
289 }
290 }
291  
292 sub flags
293 13 ▶{
Subroutine flags: called CC=7 SCAR=19.5
Pod documented
294 13 my $self = shift( @_ );
295 13 my $opts = $self->_get_args_as_hash( @_ );
296 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
297 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
298 13 ▶ $opts->{create} = $self->create unless( length( $opts->{create} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"create"} 0 13 13
299 13 ▶ $opts->{exclusive} = $self->exclusive unless( length( $opts->{exclusive} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"exclusive"} 4 9 13
300 13 ▶ $opts->{mode} = $self->mode unless( length( $opts->{mode} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"mode"} 4 9 13
301 13 my $flags = 0;
302 13 ▶ $flags |= &IPC::SysV::IPC_CREAT if( $opts->{create} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"create"} 3 10 13
303 13 ▶ $flags |= &IPC::SysV::IPC_EXCL if( $opts->{exclusive} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"exclusive"} 0 13 13
304 13 ▶ $flags |= ( defined( $opts->{mode} ) ? $opts->{mode} : 0666 );
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'mode'} ? : 13 0 13
305 13 return( $flags );
306 }
307  
308 # sub id { return( shift->_set_get_scalar( 'id', @_ ) ); }
309 sub id
310 25 ▶{
Subroutine id: called CC=2 SCAR=6.9
Pod documented
311 25 my $self = shift( @_ );
312 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
313 25 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 19 25
314 {
315 6 $self->{id} = shift( @_ );
316 }
317 25 return( $self->{id} );
318 }
319  
320 0 ▶sub json { return( shift->_packing_method( 'json' ) ); }
Subroutine json: not called CC=1 SCAR=6.9
Pod documented
321  
322 sub key
323 19 ▶{
Subroutine key: called CC=3 SCAR=11.0
Pod documented
324 19 my $self = shift( @_ );
325 19 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 8 19
326 {
327 11 ▶ my $key = $self->_verify_key( shift( @_ ) ) ||
Condition50%1/2
$self->_verify_key(shift @_) || (return $self->pass_error)
l!l
110
MC/DC0%0/1
$self->_verify_key(shift @_) || (return $self->pass_error)
$self->_verify_key(shift @_)
Truth table50%1/2
A
A= $self->_verify_key(shift @_)
Aresult
11
00
328 return( $self->pass_error );
329 11 $self->{key} = $key;
330 11 $self->{serial} = $self->_str2key( $key );
331 }
332 19 return( $self->{key} );
333 }
334  
335 sub lock
336 8 ▶{
Subroutine lock: called CC=10 SCAR=24.5
Pod documented
337 8 my $self = shift( @_ );
338 8 my $type = shift( @_ );
339 8 my $timeout = shift( @_ );
340 # $type = LOCK_EX if( !defined( $type ) );
341 8 ▶ $type = LOCK_SH if( !defined( $type ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 2 6 8
342 8 ▶ return( $self->unlock ) if( ( $type & LOCK_UN ) );
Branch: 1 branch
BranchTrueFalseTotal
if $type & 8 0 8 8
343 8 ▶ return(1) if( $self->locked & $type );
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked & $type 0 8 8
344 8 ▶ $timeout = 0 if( !defined( $timeout ) || $timeout !~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout and $timeout =~ /^\d+$/ 8 0 8
Condition33%1/3
defined $timeout and $timeout =~ /^\d+$/
!ll&&!rl&&r
800
MC/DC0%0/2
defined $timeout and $timeout =~ /^\d+$/
defined $timeout $timeout =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $timeoutB= $timeout =~ /^\d+$/
ABresult
0X0
100
111
345 # If the lock is different, release it first
346 8 ▶ $self->unlock if( $self->locked );
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked 2 6 8
347 8 my $semid = $self->semid;
348 8 ▶ return( $self->error( "No semaphore id set yet." ) ) if( !defined( $semid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $semid 0 8 8
349 # try-catch
350 8 local $@;
351 8 my $rc;
352 eval
353 8 {
354 0 ▶ local $SIG{ALRM} = sub{ die( "timeout" ); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
355 8 alarm( $timeout );
356 8 $rc = $self->op( @{$SEMOP_ARGS->{ $type }} );
357 8 alarm(0);
358 };
359 8 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 8 8
360 {
361 0 return( $self->error( "Unable to set a lock: $@" ) );
362 }
363 else
364 {
365 8 ▶ if( $rc )
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) { } 8 0 8
366 {
367 8 $self->locked( $type );
368 }
369 else
370 {
371 0 return( $self->error( "Failed to set a lock on semaphore id \"$semid\" for lock type $type: $!" ) );
372 }
373 }
374 8 return( $self );
375 }
376  
377 56 ▶sub locked { return( shift->_set_get_scalar( 'locked', @_ ) ); }
Subroutine locked: called CC=1 SCAR=0.0
Pod documented
378  
379 23 ▶sub mode { return( shift->_set_get_scalar( 'mode', @_ ) ); }
Subroutine mode: called CC=1 SCAR=0.0
Pod documented
380  
381 sub op
382 24 ▶{
Subroutine op: called CC=8 SCAR=27.2
Pod documented
383 24 my $self = shift( @_ );
384 24 ▶ return( $self->error( "No argument was provided!" ) ) if( !scalar( @_ ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 24 24
385 24 ▶ return( $self->error( "Invalid number of argument: '", join( ', ', @_ ), "'." ) ) if( @_ % 3 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 3 0 24 24
386 24 my $id = $self->semid;
387 24 ▶ return( $self->error( "No semaphore set yet. You must open the shared memory first to set the semaphore." ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 24 24
388 24 my $data = pack( "s$N*", @_ );
389 24 my $rv;
390 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
391 24 ▶ if( !( $rv = semop( $id, $data ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv = semop($id, $data)) 0 24 24
392 {
393 0 ▶ my $serial = $self->serial ||
Condition0%0/2
$self->serial || (return $self->error('Cannot get the serial'))
l!l
00
MC/DC0%0/1
$self->serial || (return $self->error('Cannot get the serial'))
$self->serial
Truth table0%0/2
A
A= $self->serial
Aresult
11
00
394 return( $self->error( "Cannot get the serial" ) );
395 0 my $new_semid = semget( $serial, 3, IPC_CREAT | 0666 );
396 0 ▶ return( $self->error( "Unable to recreate semaphore for serial '$serial': $!" ) ) if( !defined( $new_semid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_semid 0 0 0
397 0 $rv = semop( $new_semid, $data );
398 # Update the stored semaphore ID so that subsequent operations use the
399 # newly created semaphore rather than the now-invalid original one.
400 0 ▶ $self->semid( $new_semid ) if( $rv );
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 0 0
401 }
402 24 return( $rv );
403 }
404  
405 sub open
406 4 ▶{
Subroutine open: called CC=45 SCAR=50.2
Pod documented
407 4 my $self = shift( @_ );
408 4 my $opts = {};
409 4 ▶ if( ref( $_[0] ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 3 1 4
410 {
411 3 $opts = shift( @_ );
412 }
413 else
414 {
415 1 @$opts{ qw( key mode size semid ) } = @_;
416 }
417 4 ▶ $opts->{size} = $self->size unless( length( $opts->{size} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"size"} 4 0 4
418 4 $opts->{size} = int( $opts->{size} );
419 4 ▶ $opts->{mode} //= '';
Condition100%2/2
$opts->{"mode"} //= ""
l!l
31
MC/DC100%1/1
$opts->{"mode"} //= ""
$opts->{"mode"}
Truth table100%2/2
A
A= $opts->{"mode"}
Aresult
11
00
420 4 ▶ $opts->{key} //= $self->key // '';
Condition50%2/4
$opts->{"key"} //= $self->key // ""
l!l
04
$self->key // ""
l!l
40
MC/DC0%0/1
$opts->{"key"} //= $self->key // ""
$opts->{"key"}
Truth table50%1/2
A
A= $opts->{"key"}
Aresult
11
00
421 4 ▶ $opts->{semid} //= undef;
Condition50%1/2
$opts->{"semid"} //= undef
l!l
04
MC/DC0%0/1
$opts->{"semid"} //= undef
$opts->{"semid"}
Truth table50%1/2
A
A= $opts->{"semid"}
Aresult
11
00
422 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
423  
424 4 my $create;
425 4 ▶ if( $opts->{mode} eq 'w' )
Branch: 3 branches
BranchTrueFalseTotal
if ($opts->{'mode'} eq 'w') { } 3 1 4
elsif ($opts->{'mode'} eq 'r') { } 0 1 1
elsif (not ref $opts->{'key'} or $self->_can_overload($opts->{'key'}, '""')) { } 1 0 1
426 {
427 3 $create = 1;
428 }
429 elsif( $opts->{mode} eq 'r' )
430 {
431 0 $create = 0;
432 }
433 ▶ elsif( !ref( $opts->{key} ) || $self->_can_overload( $opts->{key} => '""' ) )
Condition33%1/3
not ref $opts->{"key"} or $self->_can_overload($opts->{"key"}, "\"\"")
l!l&&r!l&&!r
100
MC/DC0%0/2
not ref $opts->{"key"} or $self->_can_overload($opts->{"key"}, "\"\"")
not ref $opts->{"key"} $self->_can_overload($opts->{"key"}, "\"\"")
Truth table33%1/3
not(A) or B
A= not ref $opts->{"key"}B= $self->_can_overload($opts->{"key"}, "\"\"")
ABresult
1X1
011
000
434 {
435 1 my $key = "$opts->{key}";
436 1 ▶ if( $key =~ s/^>// )
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ s/^>//) { } 0 1 1
elsif ($key =~ s/^<//) { } 0 1 1
437 {
438 0 $opts->{key} = $key;
439 0 $create = 1;
440 }
441 elsif( $key =~ s/^<// )
442 {
443 0 $opts->{key} = $key;
444 0 $create = 0;
445 }
446 else
447 {
448 1 $create = $self->create;
449 }
450 }
451 else
452 {
453 0 $create = $self->create;
454 }
455  
456 4 my $serial;
457 4 ▶ if( length( $opts->{key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts->{'key'}) { } 4 0 4
458 {
459 4 ▶ my $key_val = $self->_verify_key( $opts->{key} ) ||
Condition50%1/2
$self->_verify_key($opts->{'key'}) || (return $self->pass_error)
l!l
40
MC/DC0%0/1
$self->_verify_key($opts->{'key'}) || (return $self->pass_error)
$self->_verify_key($opts->{'key'})
Truth table50%1/2
A
A= $self->_verify_key($opts->{'key'})
Aresult
11
00
460 return( $self->pass_error );
461 4 $serial = $self->_str2key( $key_val );
462 4 ▶ return( $self->error( "Cannot get serial from key '", ( $self->_is_array( $key_val ) ? join( "', '", @$key_val ) : $key_val ), "': ", $self->error ) ) if( !defined( $serial ) );
Branch: 2 branches
BranchTrueFalseTotal
unless defined $serial 0 4 4
$self->_is_array($key_val) ? : 0 0 0
463 }
464 else
465 {
466 0 $serial = $self->serial;
467 }
468 4 ▶ return( $self->error( "There is no serial." ) ) if( !defined( $serial ) || !CORE::length( "$serial" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $serial and length "$serial" 0 4 4
Condition33%1/3
defined $serial and length "$serial"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $serial and length "$serial"
defined $serial length "$serial"
Truth table33%1/3
A and B
A= defined $serialB= length "$serial"
ABresult
0X0
100
111
469  
470 my $flags = $self->flags(
471 create => $create,
472 4 ▶ ( $opts->{mode} =~ /^\d+$/ ? ( mode => $opts->{mode} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'mode'} =~ /^\d+$/ ? : 0 4 4
473 );
474  
475 4 my $id;
476 # try-catch
477 4 local $@;
478 eval
479 4 {
480 4 $id = shmget( $serial, $opts->{size}, $flags );
481 };
482 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
483 {
484 0 ▶ if( $@ =~ /shmget[[:blank:]\h]+not[[:blank:]\h]+implemented/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /shmget[[:blank:]\h]+not[[:blank:]\h]+implemented/i) { } 0 0 0
485 {
486 0 return( $self->error( "IPC SysV is supported, but somehow shmget is not implemented: $@" ) );
487 }
488 else
489 {
490 0 return( $self->error( "Error while trying to get the shared memory id: $@" ) );
491 }
492 }
493  
494 4 ▶ if( !defined( $id ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 4 4
495 {
496 0 ▶ return( $self->error( "Unable to ", ( $create ? 'create or open' : 'open' ), " shared memory id with key \"$serial\", size \"$opts->{size}\" and flags \"$flags\": $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$create ? : 0 0 0
497 }
498 4 $self->serial( $serial );
499  
500 4 my $semid;
501 # try-catch
502 4 local $@;
503 eval
504 4 {
505 4 my $serial2semid_repo = Module::Generic::Global->new( 'serial2semid' => CORE::ref( $self ), key => $serial );
506 4 my $serial2semid = $serial2semid_repo->get;
507 4 ▶ if( defined( $opts->{semid} ) )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts->{'semid'}) { } 0 4 4
elsif (not $create and defined(my $semaphore_id = $self->semid)) { } 0 4 4
elsif (not $create and defined $serial2semid) { } 1 3 4
508 {
509 0 $semid = $opts->{semid};
510 }
511 ▶ elsif( !$create && defined( my $semaphore_id = $self->semid ) )
Condition66%2/3
not $create and defined(my $semaphore_id = $self->semid)
!ll&&!rl&&r
310
MC/DC0%0/2
not $create and defined(my $semaphore_id = $self->semid)
not $create defined(my $semaphore_id = $self->semid)
Truth table66%2/3
not(A) and B
A= not $createB= defined(my $semaphore_id = $self->semid)
ABresult
0X0
100
111
512 {
513 0 $semid = $semaphore_id;
514 }
515 ▶ elsif( !$create && defined( $serial2semid ) )
Condition66%2/3
not $create and defined $serial2semid
!ll&&!rl&&r
301
MC/DC50%1/2
not $create and defined $serial2semid
not $create defined $serial2semid
Truth table66%2/3
not(A) and B
A= not $createB= defined $serial2semid
ABresult
0X0
100
111
516 {
517 1 $semid = $serial2semid;
518 }
519 else
520 {
521 3 ▶ $semid = semget( $serial, ( $create ? 3 : 0 ), $flags );
Branch: 1 branch
BranchTrueFalseTotal
$create ? : 3 0 3
522 3 ▶ if( !defined( $semid ) && $create )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $semid and $create) 0 3 3
Condition33%1/3
not defined $semid and $create
!ll&&!rl&&r
300
MC/DC0%0/2
not defined $semid and $create
not defined $semid $create
Truth table33%1/3
not(A) and B
A= not defined $semidB= $create
ABresult
0X0
100
111
523 {
524 0 my $newflags = ( $flags | &IPC::SysV::IPC_CREAT );
525 0 $semid = semget( $serial, 3, $newflags );
526 }
527 3 ▶ if( defined( $semid ) && $create )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $semid and $create) 3 0 3
Condition33%1/3
defined $semid and $create
!ll&&!rl&&r
003
MC/DC0%0/2
defined $semid and $create
defined $semid $create
Truth table33%1/3
A and B
A= defined $semidB= $create
ABresult
0X0
100
111
528 {
529 3 $serial2semid_repo->lock;
530 3 $serial2semid_repo->set( $semid );
531 3 $serial2semid_repo->unlock;
532 }
533 }
534 };
535 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
536 {
537 0 ▶ if( $@ =~ /semget[[:blank:]\h]+not[[:blank:]\h]+implemented/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /semget[[:blank:]\h]+not[[:blank:]\h]+implemented/i) { } 0 0 0
538 {
539 0 return( $self->error( "IPC SysV is supported, but somehow semget is not implemented: $@" ) );
540 }
541 else
542 {
543 0 return( $self->error( "Error while trying to get the semaphore for shared memory id: $@" ) );
544 }
545 }
546 4 ▶ if( !defined( $semid ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $semid) 0 4 4
547 {
548 0 ▶ return( $self->error( "Unable to get semaphore for key '", ( $self->_is_array( $opts->{key} ) ? join( "', '", @{$opts->{key}} ) : $opts->{key} ), "' (serial \"$serial\", create=$create, flags=$flags): $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($opts->{'key'}) ? : 0 0 0
549 }
550  
551 my $new = $self->new(
552 key => $opts->{key},
553 debug => $self->debug,
554 mode => $self->mode,
555 destroy => $self->destroy,
556 destroy_semaphore => $self->destroy_semaphore,
557 exclusive => $self->exclusive,
558 _packing_method => $self->_packing_method,
559 ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
560 0 ▶ ) || return( $self->error( "Cannot create object with key '", ( $self->_is_array( $opts->{key} ) ? join( "', '", @{$opts->{key}} ) : $opts->{key} ), "': ", $self->error ) );
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 4 4
$self->_is_array($opts->{'key'}) ? : 0 0 0
Condition50%1/2
$self->new('key', $opts->{'key'}, 'debug', $self->debug, 'mode', $self->mode, 'destroy', $self->destroy, 'destroy_semaphore', $self->destroy_semaphore, 'exclusive', $self->exclusive, '_packing_method', $self->_packing_method, defined $self->{'base64'} ? ('base64', $self->{'base64'}) : ()) || (return $self->error(q[Cannot create object with key '], $self->_is_array($opts->{'key'}) ? join(q[', '], @{$opts->{'key'};}) : $opts->{'key'}, q[': ], $self->error))
l!l
40
MC/DC0%0/1
$self->new('key', $opts->{'key'}, 'debug', $self->debug, 'mode', $self->mode, 'destroy', $self->destroy, 'destroy_semaphore', $self->destroy_semaphore, 'exclusive', $self->exclusive, '_packing_method', $self->_packing_method, defined $self->{'base64'} ? ('base64', $self->{'base64'}) : ()) || (return $self->error(q[Cannot create object with key '], $self->_is_array($opts->{'key'}) ? join(q[', '], @{$opts->{'key'};}) : $opts->{'key'}, q[': ], $self->error))
$self->new('key', $opts->{'key'}, 'debug', $self->debug, 'mode', $self->mode, 'destroy', $self->destroy, 'destroy_semaphore', $self->destroy_semaphore, 'exclusive', $self->exclusive, '_packing_method', $self->_packing_method, defined $self->{'base64'} ? ('base64', $self->{'base64'}) : ())
Truth table50%1/2
A
A= $self->new('key', $opts->{'key'}, 'debug', $self->debug, 'mode', $self->mode, 'destroy', $self->destroy, 'destroy_semaphore', $self->destroy_semaphore, 'exclusive', $self->exclusive, '_packing_method', $self->_packing_method, defined $self->{'base64'} ? ('base64', $self->{'base64'}) : ())
Aresult
11
00
561 4 $new->serial( $serial );
562 4 $new->id( $id );
563 4 $new->semid( $semid );
564  
565 4 my $shem_repo = Module::Generic::Global->new( 'shem_repo' => CORE::ref( $self ), key => CORE::ref( $self ) );
566 4 my $id2obj_repo = Module::Generic::Global->new( 'id2obj' => CORE::ref( $self ), key => $id );
567 4 $shem_repo->lock;
568 4 ▶ my $all_shem = $shem_repo->get // [];
Condition100%2/2
$shem_repo->get // []
l!l
13
MC/DC100%1/1
$shem_repo->get // []
$shem_repo->get
Truth table100%2/2
A
A= $shem_repo->get
Aresult
11
00
569 4 CORE::push( @$all_shem, $id );
570 4 $shem_repo->set( $all_shem );
571 4 $shem_repo->unlock;
572  
573 4 $id2obj_repo->lock;
574 4 $id2obj_repo->set( $new );
575 4 $id2obj_repo->unlock;
576  
577 4 ▶ if( !defined( $new->op( @{$SEMOP_ARGS->{(LOCK_SH)}} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new->op(@{$$SEMOP_ARGS{1};})) 0 4 4
578 {
579 0 return( $self->error( "Unable to set lock on semaphore: $!" ) );
580 }
581  
582 4 my $there = $new->stat( SEM_MARKER );
583 4 $new->size( $opts->{size} );
584 4 ▶ if( $there == SHM_EXISTS )
Branch: 1 branch
BranchTrueFalseTotal
if ($there == 1) { } 1 3 4
585 {
586 }
587 else
588 {
589 3 ▶ $new->stat( SEM_MARKER, SHM_EXISTS ) ||
Branch: 1 branch
BranchTrueFalseTotal
$new->stat(0, 1) or return $new->error("Unable to set semaphore during object creation: ", $new->error) 3 0 3
590 return( $new->error( "Unable to set semaphore during object creation: ", $new->error ) );
591 }
592  
593 4 $new->op( @{$SEMOP_ARGS->{(LOCK_SH | LOCK_UN)}} );
594 4 return( $new );
595 }
596  
597 3 ▶sub owner { return( shift->_set_get_scalar( 'owner', @_ ) ); }
Subroutine owner: called CC=1 SCAR=0.0
Pod documented
598  
599 sub pid
600 0 ▶{
Subroutine pid: not called CC=4 SCAR=26.4
Pod documented
601 0 my $self = shift( @_ );
602 0 my $sem = shift( @_ );
603 0 my $semid = $self->semid;
604 0 ▶ return( $self->error( "No semaphore set yet. You must open the shared memory first to remove semaphore." ) ) if( !defined( $semid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $semid 0 0 0
605 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
606 0 my $arg = 0;
607 # try-catch
608 0 local $@;
609 my @rv = eval
610 0 {
611 0 my $v = semctl( $semid, $sem, &IPC::SysV::GETPID, $arg );
612 0 ▶ return( $v ? 0 + $v : undef() );
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
613 };
614 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
615 {
616 0 return( $self->error( "Error trying to get semaphore pid using semaphore id ${semid}: $@" ) );
617 }
618 0 return( $rv[0] );
619 }
620  
621 sub rand
622 0 ▶{
Subroutine rand: not called CC=4 SCAR=26.3
Pod documented
623 0 my $self = shift( @_ );
624 0 ▶ my $size = $self->size || 1024;
Condition0%0/2
$self->size || 1024
l!l
00
MC/DC0%0/1
$self->size || 1024
$self->size
Truth table0%0/2
A
A= $self->size
Aresult
11
00
625 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
626 # try-catch
627 0 local $@;
628 0 my $key;
629 eval
630 0 {
631 0 $key = shmget( &IPC::SysV::IPC_PRIVATE, $size, &IPC::SysV::S_IRWXU | &IPC::SysV::S_IRWXG | &IPC::SysV::S_IRWXO );
632 };
633 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
634 {
635 0 return( $self->error( "Error trying to get a random private key using shmget and IPC_PRIVATE: $@" ) );
636 }
637 0 ▶ if( !defined( $key ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key) { } 0 0 0
638 {
639 0 return( $self->error( "Unable to generate a share memory key: $!" ) )
640 }
641 else
642 {
643 0 return( $key );
644 }
645 }
646  
647 # $self->read( $buffer, $size );
648 # $self->read( $buffer );
649 # my $data = $self->read;
650 sub read
651 8 ▶{
Subroutine read: called CC=20 SCAR=40.0
Pod documented
652 8 my( $self, $buf ) = @_;
653 8 my $size;
654 8 ▶ $size = int( $_[2] ) if( scalar( @_ ) > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 2 0 8 8
655 # Optional length parameter for non-reference data only
656 8 ▶ $size //= int( $self->size || SHM_BUFSIZ );
Condition50%2/4
$size //= int($self->size || 65536)
l!l
08
$self->size || 65536
l!l
80
MC/DC0%0/1
$size //= int($self->size || 65536)
$size
Truth table50%1/2
A
A= $size
Aresult
11
00
657 8 my $id = $self->id;
658 8 ▶ return( $self->error( "No shared memory id! Have you opened it first?" ) ) if( !length( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $id 0 8 8
659 8 my $buffer = '';
660 8 my $addr = $self->addr;
661 8 ▶ if( $addr )
Branch: 1 branch
BranchTrueFalseTotal
if ($addr) { } 0 8 8
662 {
663 0 ▶ memread( $addr, $buffer, 0, $size ) ||
Branch: 1 branch
BranchTrueFalseTotal
&memread($addr, $buffer, 0, $size) or return $self->error(qq[Unable to read data from shared memory address "$addr" using memread: $!]) 0 0 0
664 return( $self->error( "Unable to read data from shared memory address \"$addr\" using memread: $!" ) );
665 }
666 else
667 {
668 8 ▶ shmread( $id, $buffer, 0, $size ) ||
Branch: 1 branch
BranchTrueFalseTotal
shmread $id, $buffer, 0, $size or return $self->error(qq[Unable to read data from shared memory id "$id": $!]) 8 0 8
669 return( $self->error( "Unable to read data from shared memory id \"$id\": $!" ) );
670 }
671 # Get rid of nulls end padded
672 # 2022-08-03: Ok, null bytes are added to Storable and CBOR::XS serialised data,
673 # so we cannot just remove them. Instead we encapsulate the serialised data
674 # $buffer = unpack( "A*", $buffer );
675 8 my $packing = $self->_packing_method;
676 8 my $data;
677 8 ▶ if( CORE::length( $buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer) { } 8 0 8
678 {
679 # There may be encapsulation of data before writing data to memory.
680 # e.g.: MG[14]something here
681 8 ▶ if( index( $buffer, 'MG[' ) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($buffer, "MG[") == 0) 8 0 8
682 {
683 8 my $def = substr( $buffer, 0, index( $buffer, ']' ) + 1, '' );
684 # Get the string length stored
685 8 my $len = int( substr( $def, 3, -1 ) );
686 # Remove any possible remaining unwanted data
687 8 substr( $buffer, $len, length( $buffer ), '' );
688 }
689  
690 # try-catch
691 8 local $@;
692 eval
693 8 {
694 8 ▶ if( $packing eq 'json' )
Branch: 3 branches
BranchTrueFalseTotal
if ($packing eq 'json') { } 8 0 8
elsif ($packing eq 'cbor') { } 0 0 0
elsif ($packing eq 'sereal') { } 0 0 0
695 {
696 8 $data = $self->_decode_json( $buffer );
697 }
698 elsif( $packing eq 'cbor' )
699 {
700 $data = $self->deserialise(
701 data => $buffer,
702 serialiser => 'CBOR::XS',
703 allow_sharing => 1,
704 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
705 );
706 }
707 elsif( $packing eq 'sereal' )
708 {
709 $data = $self->deserialise(
710 data => $buffer,
711 serialiser => 'Sereal',
712 freeze_callbacks => 1,
713 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
714 );
715 }
716 # By default Storable::Improved
717 else
718 {
719 # $data = Storable::Improved::thaw( $buffer );
720 $data = $self->deserialise(
721 data => $buffer,
722 serialiser => 'Storable::Improved',
723 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
724 );
725 }
726 };
727 8 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
728 {
729 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
730 }
731 }
732 else
733 {
734 0 $data = $buffer;
735 }
736  
737 8 ▶ if( scalar( @_ ) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 2 6 8
738 {
739 2 $_[1] = $data;
740 2 ▶ return( CORE::length( $_[1] ) || "0E0" );
Condition50%1/2
length $_[1] || '0E0'
l!l
20
MC/DC0%0/1
length $_[1] || '0E0'
length $_[1]
Truth table50%1/2
A
A= length $_[1]
Aresult
11
00
741 }
742 else
743 {
744 6 return( $data );
745 }
746 }
747  
748 sub remove
749 2 ▶{
Subroutine remove: called CC=16 SCAR=32.4
Pod documented
750 2 my $self = shift( @_ );
751 2 ▶ return(1) if( $self->removed );
Branch: 1 branch
BranchTrueFalseTotal
if $self->removed 0 2 2
752 2 my $id = $self->id();
753 2 ▶ return( $self->error( "No shared memory id! Have you opened it first?" ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 2 2
754 2 my $semid = $self->semid;
755 2 ▶ return( $self->error( "No semaphore set yet. You must open the shared memory first to remove semaphore." ) ) if( !defined( $semid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $semid 0 2 2
756 2 $self->unlock();
757 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
758 # Remove share memory segment
759 2 ▶ if( !defined( shmctl( $id, &IPC::SysV::IPC_RMID, 0 ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined shmctl($id, &IPC::SysV::IPC_RMID, 0)) 0 2 2
760 {
761 0 return( $self->error( "Unable to remove share memory segement id '$id' (IPC_RMID is '", &IPC::SysV::IPC_RMID, "'): $!" ) );
762 }
763 # Remove semaphore
764 2 my $rv;
765 2 my $arg = 0;
766 2 ▶ if( !defined( $rv = semctl( $semid, 0, &IPC::SysV::IPC_RMID, $arg ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($rv = semctl($semid, 0, &IPC::SysV::IPC_RMID, $arg))) 0 2 2
767 {
768 # Suppress warning for EINVAL/EIDRM (semaphore already removed or invalid)
769 0 ▶ if( $!{EINVAL} || $!{EIDRM} )
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'EINVAL'} or $!{'EIDRM'}) { } 0 0 0
Condition0%0/3
$!{"EINVAL"} or $!{"EIDRM"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EINVAL"} or $!{"EIDRM"}
$!{"EINVAL"} $!{"EIDRM"}
Truth table0%0/3
A or B
A= $!{"EINVAL"}B= $!{"EIDRM"}
ABresult
1X1
011
000
770 {
771 # Treat as success since semaphore is gone
772 0 $rv = 1;
773 }
774 else
775 {
776 0 ▶ warn( "Warning only: could not remove the semaphore id \"$semid\": $!" ) if( $self->_warnings_is_enabled( 'Module::Generic' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled("Module::Generic") 0 0 0
777 }
778 }
779 2 ▶ $self->removed( $rv ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 2 0 2
780 2 ▶ if( $rv )
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 2 0 2
781 {
782 2 my $id2obj_repo = Module::Generic::Global->new( 'id2obj' => CORE::ref( $self ), key => $id );
783 2 $id2obj_repo->remove;
784 2 my $shem_repo = Module::Generic::Global->new( 'shem_repo' => CORE::ref( $self ), key => CORE::ref( $self ) );
785 2 $shem_repo->lock;
786 2 ▶ my $all_shem = $shem_repo->get // [];
Condition50%1/2
$shem_repo->get // []
l!l
20
MC/DC0%0/1
$shem_repo->get // []
$shem_repo->get
Truth table50%1/2
A
A= $shem_repo->get
Aresult
11
00
787 2 my $modified = 0;
788 2 ▶ for( my $i = 0; $i < scalar( @$all_shem ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$all_shem) 2 0 2
789 {
790 2 my $this_id = $all_shem->[$i];
791 2 ▶ if( $this_id eq $id )
Branch: 1 branch
BranchTrueFalseTotal
if ($this_id eq $id) 2 0 2
792 {
793 2 CORE::splice( @$all_shem, $i, 1 );
794 2 $modified++;
795 2 last;
796 }
797 }
798 2 ▶ if( $modified )
Branch: 1 branch
BranchTrueFalseTotal
if ($modified) 2 0 2
799 {
800 2 $shem_repo->set( $all_shem );
801 }
802 2 $shem_repo->unlock;
803 2 $self->id( undef() );
804 2 $self->semid( undef() );
805 }
806 2 ▶ return( $rv ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 2 0 2
807 }
808  
809 sub remove_semaphore
810 0 ▶{
Subroutine remove_semaphore: not called CC=19 SCAR=58.2
Pod documented
811 0 my $self = shift( @_ );
812 0 ▶ return(1) if( $self->removed_semaphore );
Branch: 1 branch
BranchTrueFalseTotal
if $self->removed_semaphore 0 0 0
813 0 ▶ my $id = @_ ? shift( @_ ) : $self->id;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
814 0 ▶ return( $self->error( "No shared memory id provided nor found." ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
815 0 ▶ my $semid = @_ ? shift( @_ ) : $self->semid;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
816 0 ▶ return( $self->error( "No semaphore id provided nor found for shared memory id '$id'. You must open the shared memory first to remove semaphore." ) ) if( !defined( $semid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $semid 0 0 0
817 0 my $serial = $self->serial;
818 0 my $serial2semid_repo = Module::Generic::Global->new( 'serial2semid' => CORE::ref( $self ), key => $serial );
819 0 $serial2semid_repo->lock;
820 0 my $serial2semid = $serial2semid_repo->get;
821 0 my $repo_modified = 0;
822 # Should we really remove this part ?
823 # if( $semid eq '0' )
824 # {
825 # $self->removed_semaphore(1);
826 # $self->semid( undef() );
827 # return(1);
828 # }
829  
830 0 $self->unlock();
831 # try-catch
832 0 local $@;
833 0 my $rv;
834 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
835 0 my $arg = 0;
836 eval
837 0 {
838 0 $rv = semctl( $semid, 0, &IPC::SysV::IPC_RMID, $arg );
839 };
840 0 ▶ if( $@ )
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not defined $rv or $rv == -1 and $!) { } 0 0 0
841 {
842 0 $serial2semid_repo->unlock;
843 0 return( $self->error( "An error occurred while trying to remove semaphore with id '$semid' for shared memory segment with id '$id': $@" ) );
844 }
845 ▶ elsif( !defined( $rv ) || ( $rv == -1 && $! ) )
Condition0%0/6
not defined $rv or $rv == -1 and $!
l!l&&r!l&&!r
000
$rv == -1 and $!
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $rv or $rv == -1 and $!
not defined $rv $rv == -1 $!
Truth table0%0/4
not(A) or B and C
A= not defined $rvB= $rv == -1C= $!
ABCresult
1XX1
0111
00X0
0100
846 {
847 # return( $self->error( "Unable to remove semaphore with id '$semid' for shared memory segment with id '$id': $!" ) );
848 # Suppress warning for EINVAL/EIDRM (semaphore already removed or invalid)
849 0 ▶ if( $!{EINVAL} || $!{EIDRM} )
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'EINVAL'} or $!{'EIDRM'}) { } 0 0 0
Condition0%0/3
$!{"EINVAL"} or $!{"EIDRM"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EINVAL"} or $!{"EIDRM"}
$!{"EINVAL"} $!{"EIDRM"}
Truth table0%0/3
A or B
A= $!{"EINVAL"}B= $!{"EIDRM"}
ABresult
1X1
011
000
850 {
851 # Treat as success since semaphore is gone
852 0 $rv = 1;
853 0 ▶ if( defined( $serial ) && defined( $serial2semid ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $serial and defined $serial2semid) 0 0 0
Condition0%0/3
defined $serial and defined $serial2semid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $serial and defined $serial2semid
defined $serial defined $serial2semid
Truth table0%0/3
A and B
A= defined $serialB= defined $serial2semid
ABresult
0X0
100
111
854 {
855 0 $serial2semid_repo->remove;
856 }
857 }
858 else
859 {
860 0 ▶ warn( "Warning only: could not remove the semaphore id \"$semid\" with IPC::SysV::IPC_RMID value '", &IPC::SysV::IPC_RMID, "' for shared memory id $id: $!" ) if( $self->_warnings_is_enabled( 'Module::Generic' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled("Module::Generic") 0 0 0
861 }
862 }
863 else
864 {
865 0 ▶ if( defined( $serial ) && defined( $serial2semid ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $serial and defined $serial2semid) 0 0 0
Condition0%0/3
defined $serial and defined $serial2semid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $serial and defined $serial2semid
defined $serial defined $serial2semid
Truth table0%0/3
A and B
A= defined $serialB= defined $serial2semid
ABresult
0X0
100
111
866 {
867 0 $serial2semid_repo->remove;
868 }
869 # return(1);
870 }
871 0 $serial2semid_repo->unlock;
872 0 ▶ $self->removed_semaphore( $rv ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 0 0 0
873 0 $self->semid( undef() );
874 0 ▶ return( $rv ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 0 0 0
875 }
876  
877 4 ▶sub removed { return( shift->_set_get_boolean( 'removed', @_ ) ); }
Subroutine removed: called CC=1 SCAR=0.0
Pod documented
878  
879 0 ▶sub removed_semaphore { return( shift->_set_get_boolean( 'removed_semaphore', @_ ) ); }
Subroutine removed_semaphore: not called CC=1 SCAR=6.9
Pod documented
880  
881 sub reset
882 0 ▶{
Subroutine reset: not called CC=2 SCAR=17.9
Pod documented
883 0 my $self = shift( @_ );
884 0 my $default;
885 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
886 {
887 0 $default = shift( @_ );
888 }
889 else
890 {
891 0 $default = '';
892 }
893 0 $self->lock( LOCK_EX );
894 0 $self->write( $default );
895 0 $self->unlock;
896 0 return( $self );
897 }
898  
899 59 ▶sub semid { return( shift->_set_get_scalar( 'semid', @_ ) ); }
Subroutine semid: called CC=1 SCAR=0.0
Pod documented
900  
901 0 ▶sub sereal { return( shift->_packing_method( 'sereal' ) ); }
Subroutine sereal: not called CC=1 SCAR=6.9
Pod documented
902  
903 19 ▶sub serial { return( shift->_set_get_scalar( 'serial', @_ ) ); }
Subroutine serial: called CC=1 SCAR=0.0
Pod documented
904  
905 0 ▶sub serialiser { return( shift->_set_get_scalar( '_packing_method', @_ ) ); }
Subroutine serialiser: not called CC=1 SCAR=6.9
Pod documented
906  
907 {
908 6 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
909 6 *serializer = \&serialiser;
910 }
911  
912 sub shmstat
913 0 ▶{
Subroutine shmstat: not called CC=3 SCAR=19.6
Pod documented
914 0 my $self = shift( @_ );
915 0 my $data = '';
916 0 my $id = $self->id;
917 0 ▶ return( $self->error( "No shared memory id set!" ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
918 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
919 0 ▶ shmctl( $id, &IPC::SysV::IPC_STAT, $data ) or
Branch: 1 branch
BranchTrueFalseTotal
shmctl $id, &IPC::SysV::IPC_STAT, $data or return $self->error("Unable to stat shared memory with id '${id}': $!") 0 0 0
920 return( $self->error( "Unable to stat shared memory with id '$id': $!" ) );
921 0 return( Module::Generic::SharedStat->new->unpack( $data ) );
922 }
923  
924 42 ▶sub size { return( shift->_set_get_scalar( 'size', @_ ) ); }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
925  
926 sub stat
927 7 ▶{
Subroutine stat: called CC=10 SCAR=31.1
Pod documented
928 7 my $self = shift( @_ );
929 7 my $id = $self->semid;
930 7 ▶ return( $self->error( "No semaphore set yet. You must open the shared memory first to set the semaphore." ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 7 7
931 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
932 7 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 7 0 7
933 {
934 7 ▶ if( @_ == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 4 3 7
935 {
936 4 my $sem = shift( @_ );
937 4 my $arg = 0;
938 4 my $v = semctl( $id, $sem, &IPC::SysV::GETVAL, $arg );
939 4 ▶ return( $v ? 0 + $v : undef() );
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 4 0 4
940 }
941 else
942 {
943 3 my( $sem, $val ) = @_;
944 # semctl( $id, $sem, &IPC::SysV::SETVAL, $val ) ||
945 # return( $self->error( "Unable to semctl with semaphore id '$id', semaphore '$sem', SETVAL='", &IPC::SysV::SETVAL, "' and value='$val': $!" ) );
946 3 my $rv = semctl( $id, $sem, &IPC::SysV::SETVAL, $val );
947 3 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 3 3
948 {
949 # Suppress EINVAL as non-fatal (semaphore may be invalid or removed)
950 0 ▶ if( $!{EINVAL} )
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'EINVAL'}) { } 0 0 0
951 {
952 # Treat as success to allow recovery
953 0 $rv = 1;
954 }
955 else
956 {
957 0 return( $self->error( "Unable to semctl with semaphore id '$id', semaphore '$sem', SETVAL='${\(&SETVAL)}' and value='$val': $!" ) );
958 }
959 }
960 3 return( $rv );
961 }
962 }
963 else
964 {
965 0 my $data = '';
966 0 ▶ if( wantarray() )
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
967 {
968 0 ▶ semctl( $id, 0, &IPC::SysV::GETALL, $data ) || return( () );
Branch: 1 branch
BranchTrueFalseTotal
semctl $id, 0, &IPC::SysV::GETALL, $data or return () 0 0 0
969 0 return( ( unpack( "s$N*", $data ) ) );
970 }
971 else
972 {
973 0 ▶ semctl( $id, 0, &IPC::SysV::IPC_STAT, $data ) ||
Branch: 1 branch
BranchTrueFalseTotal
semctl $id, 0, &IPC::SysV::IPC_STAT, $data or return $self->error("Unable to stat semaphore with id '${id}': $!") 0 0 0
974 return( $self->error( "Unable to stat semaphore with id '$id': $!" ) );
975 0 return( Module::Generic::SemStat->new->unpack( $data ) );
976 }
977 }
978 }
979  
980 0 ▶sub storable { return( shift->_packing_method( 'storable' ) ); }
Subroutine storable: not called CC=1 SCAR=6.9
Pod documented
981  
982 6 ▶sub supported { return( $SYSV_SUPPORTED ); }
Subroutine supported: called CC=1 SCAR=0.0
Pod documented
983  
984 # sub unlock
985 # {
986 # my $self = shift( @_ );
987 # return(1) if( !$self->locked );
988 # my $semid = $self->semid;
989 # return( $self->error( "No semaphore set yet. You must open the shared memory first to unlock semaphore." ) ) if( !length( $semid // '' ) );
990 # my $type = ( $self->locked | LOCK_UN );
991 # $type ^= LOCK_NB if( $type & LOCK_NB );
992 # my $rc = $self->op( @{$SEMOP_ARGS->{ $type }} );
993 # if( !defined( $rc ) )
994 # {
995 # # Failed to unlock semaphore
996 # }
997 # $self->locked(0);
998 # return( $self );
999 # }
1000 sub unlock
1001 12 ▶{
Subroutine unlock: called CC=5 SCAR=16.3
Pod documented
1002 12 my $self = shift( @_ );
1003 12 ▶ return(1) if( !$self->locked );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->locked 4 8 12
1004 8 my $semid = $self->semid;
1005 8 ▶ return( $self->error( "No semaphore set yet. You must open the shared memory first to unlock semaphore." ) ) if( !defined( $semid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $semid 0 8 8
1006 8 my $type = ( $self->locked | LOCK_UN );
1007 8 ▶ $type ^= LOCK_NB if( $type & LOCK_NB );
Branch: 1 branch
BranchTrueFalseTotal
if $type & 4 0 8 8
1008 8 ▶ if( !defined( $self->op( @{$SEMOP_ARGS->{ $type }} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->op(@{$$SEMOP_ARGS{$type};})) 0 8 8
1009 {
1010 # Failed to unlock semaphore
1011 }
1012 8 $self->locked(0);
1013 8 return( $self );
1014 }
1015  
1016 sub write
1017 6 ▶{
Subroutine write: called CC=40 SCAR=62.6
Pod documented
1018 6 my $self = shift( @_ );
1019 6 my $data;
1020 6 ▶ if( scalar( @_ ) == 1 && ref( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0]) { } 6 0 6
Condition33%1/3
scalar @_ == 1 and ref $_[0]
!ll&&!rl&&r
006
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
1021 {
1022 6 $data = shift( @_ );
1023 }
1024 else
1025 {
1026 0 $data = \join( '', @_ );
1027 }
1028 6 my $id = $self->id();
1029 6 ▶ return( $self->error( "No shared memory id set yet. You must open the shared memory first to write." ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 6 6
1030 6 ▶ my $size = int( $self->size() ) || SHM_BUFSIZ;
Condition50%1/2
int $self->size || 65536
l!l
60
MC/DC0%0/1
int $self->size || 65536
int $self->size
Truth table50%1/2
A
A= int $self->size
Aresult
11
00
1031 # my @callinfo = caller;
1032 6 my $packing = $self->_packing_method;
1033 6 my $encoded;
1034 6 ▶ if( $packing eq 'json' )
Branch: 3 branches
BranchTrueFalseTotal
if ($packing eq 'json') { } 6 0 6
elsif ($packing eq 'cbor') { } 0 0 0
elsif ($packing eq 'sereal') { } 0 0 0
1035 {
1036 # try-catch
1037 6 local $@;
1038 eval
1039 6 {
1040 6 $encoded = $self->_encode_json( $data );
1041 };
1042 6 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
1043 {
1044 0 ▶ return( $self->error( "An error occured encoding data provided using $packing with base64 set to '", ( $self->{base64} // '' ), ": $@. Data was: '$data'" ) );
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
1045 }
1046 }
1047 elsif( $packing eq 'cbor' )
1048 {
1049 # try-catch
1050 0 local $@;
1051 eval
1052 0 {
1053 $encoded = $self->serialise( $data,
1054 serialiser => 'CBOR::XS',
1055 allow_sharing => 1,
1056 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
1057 );
1058 };
1059 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1060 {
1061 0 ▶ return( $self->error( "An error occured encoding data provided using $packing with base64 set to '", ( $self->{base64} // '' ), ": $@. Data was: '$data'" ) );
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
1062 }
1063 0 ▶ return( $self->error( "Unable to serialise ", CORE::length( $data ), " bytes of data using CBOR::XS with base64 set to '", ( $self->{base64} // '' ), ": ", $self->error ) ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
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
1064 }
1065 elsif( $packing eq 'sereal' )
1066 {
1067 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
1068 0 my $const;
1069 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
1070 # try-catch
1071 0 local $@;
1072 eval
1073 0 {
1074 $encoded = $self->serialise( $data,
1075 serialiser => 'Sereal',
1076 freeze_callbacks => 1,
1077 ( defined( $const ) ? ( compress => $const->() ) : () ),
1078 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $const ? : 0 0 0
defined $self->{'base64'} ? : 0 0 0
1079 );
1080 };
1081 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1082 {
1083 0 ▶ return( $self->error( "An error occured encoding data provided using $packing with base64 set to '", ( $self->{base64} // '' ), ": $@. Data was: '$data'" ) );
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
1084 }
1085 0 ▶ return( $self->error( "Unable to serialise ", CORE::length( $data ), " bytes of data using Sereal with base64 set to '", ( $self->{base64} // '' ), ": ", $self->error ) ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
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
1086 }
1087 # Default to Storable::Improved
1088 else
1089 {
1090 # local $Storable::forgive_me = 1;
1091 # $encoded = Storable::Improved::freeze( $data );
1092 eval
1093 0 {
1094 $encoded = $self->serialise( $data,
1095 serialiser => 'Storable::Improved',
1096 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
1097 );
1098 };
1099 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1100 {
1101 0 ▶ return( $self->error( "An error occured encoding data provided using $packing with base64 set to '", ( $self->{base64} // '' ), ": $@. Data was: '$data'" ) );
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
1102 }
1103 0 ▶ return( $self->error( "Unable to serialise ", CORE::length( $data ), " bytes of data using Storable with base64 set to '", ( $self->{base64} // '' ), ": ", $self->error ) ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
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
1104 }
1105  
1106 # Simple encapsulation
1107 # FYI: MG = Module::Generic
1108 6 substr( $encoded, 0, 0, 'MG[' . length( $encoded ) . ']' );
1109  
1110 6 my $len = length( $encoded );
1111 6 ▶ if( $len > $size )
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $size) 0 6 6
1112 {
1113 0 return( $self->error( "Data to write are ", length( $encoded ), " bytes long and exceed the maximum you have set of '$size'." ) );
1114 }
1115  
1116 # Ensure the shared memory segment is attached
1117 6 my $addr = $self->addr;
1118 6 ▶ if( !defined( $addr ) || ( Scalar::Util::looks_like_number( $addr ) && $addr == -1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $addr or &Scalar::Util::looks_like_number($addr) and $addr == -1) 6 0 6
Condition16%1/6
not defined $addr or &Scalar::Util::looks_like_number($addr) and $addr == -1
l!l&&r!l&&!r
600
&Scalar::Util::looks_like_number($addr) and $addr == -1
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $addr or &Scalar::Util::looks_like_number($addr) and $addr == -1
not defined $addr &Scalar::Util::looks_like_number($addr) $addr == -1
Truth table25%1/4
not(A) or B and C
A= not defined $addrB= &Scalar::Util::looks_like_number($addr)C= $addr == -1
ABCresult
1XX1
0111
00X0
0100
1119 {
1120 6 $addr = shmat( $id, undef, 0 );
1121 6 ▶ if( !defined( $addr ) || ( Scalar::Util::looks_like_number( $addr ) && $addr == -1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $addr or &Scalar::Util::looks_like_number($addr) and $addr == -1) 0 6 6
Condition33%2/6
not defined $addr or &Scalar::Util::looks_like_number($addr) and $addr == -1
l!l&&r!l&&!r
006
&Scalar::Util::looks_like_number($addr) and $addr == -1
!ll&&!rl&&r
600
MC/DC0%0/3
not defined $addr or &Scalar::Util::looks_like_number($addr) and $addr == -1
not defined $addr &Scalar::Util::looks_like_number($addr) $addr == -1
Truth table25%1/4
not(A) or B and C
A= not defined $addrB= &Scalar::Util::looks_like_number($addr)C= $addr == -1
ABCresult
1XX1
0111
00X0
0100
1122 {
1123 0 return( $self->error( "Unable to attach to shared memory id '$id': $!" ) );
1124 }
1125 6 $self->addr( $addr );
1126 }
1127  
1128 6 ▶ $self->lock( LOCK_EX ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->lock(2) or return $self->pass_error 6 0 6
1129 # my $rv = shmwrite( $id, $encoded, 0, $len ) ||
1130 # return( $self->error( "Unable to write to shared memory id '$id' with ${len} bytes of data encoded and memory size of $size: $!" ) );
1131 6 ▶ memwrite( $addr, $encoded, 0, $len ) ||
Branch: 1 branch
BranchTrueFalseTotal
&memwrite($addr, $encoded, 0, $len) or return $self->error("Unable to write to shared memory address '${addr}' using memwrite: $!") 6 0 6
1132 return( $self->error( "Unable to write to shared memory address '$addr' using memwrite: $!" ) );
1133 6 $self->unlock;
1134  
1135 # Detach the segment to avoid leaving it attached in the thread
1136 6 ▶ if( defined( $addr ) && !( Scalar::Util::looks_like_number( $addr ) && $addr == -1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr and not &Scalar::Util::looks_like_number($addr) && $addr == -1) 6 0 6
Condition33%2/6
defined $addr and not &Scalar::Util::looks_like_number($addr) && $addr == -1
!ll&&!rl&&r
006
&Scalar::Util::looks_like_number($addr) && $addr == -1
!ll&&!rl&&r
600
MC/DC0%0/3
defined $addr and not &Scalar::Util::looks_like_number($addr) && $addr == -1
defined $addr &Scalar::Util::looks_like_number($addr) $addr == -1
Truth table25%1/4
A and not(B && C)
A= defined $addrB= &Scalar::Util::looks_like_number($addr)C= $addr == -1
ABCresult
0XX0
1110
10X1
1101
1137 {
1138 6 shmdt( $addr );
1139 6 $self->addr( undef );
1140 }
1141 6 return( $self );
1142 }
1143  
1144 sub _decode_json
1145 8 ▶{
Subroutine _decode_json: called CC=4 SCAR=16.7
1146 8 my $self = shift( @_ );
1147 8 my $data = shift( @_ );
1148 # Nothing to do
1149 8 ▶ return( $data ) if( !defined( $data ) || !CORE::length( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and length $data 0 8 8
Condition33%1/3
defined $data and length $data
!ll&&!rl&&r
008
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
1150 8 my $j = JSON->new->utf8->relaxed->allow_nonref;
1151 8 my $seen = {};
1152 8 my $crawl;
1153 $crawl = sub
1154 8 ▶ {
Subroutine __ANON__: called CC=11 SCAR=35.3
1155 8 my $this = shift( @_ );
1156 8 my $type = Scalar::Util::reftype( $this );
1157 8 ▶ 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 8 8
Condition33%2/6
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
!ll&&!rl&&r
080
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
800
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
1158 8 ▶ if( $type eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 8 0 8
elsif ($type eq 'ARRAY') { } 0 0 0
1159 {
1160 # Found a former scalar reference, restore it
1161 8 ▶ if( CORE::exists( $this->{__scalar_gen_shm} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"__scalar_gen_shm"}) 0 8 8
1162 {
1163 0 return( \$this->{__scalar_gen_shm} );
1164 }
1165  
1166 8 foreach my $k ( keys( %$this ) )
1167 {
1168 20 ▶ next if( !ref( $this->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{$k} 20 0 20
1169 0 $this->{ $k } = $crawl->( $this->{ $k } );
1170 }
1171 }
1172 elsif( $type eq 'ARRAY' )
1173 {
1174 0 ▶ for( my $i = 0; $i < scalar( @$this ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$this) 0 0 0
1175 {
1176 0 ▶ next if( !ref( $this->[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->[$i] 0 0 0
1177 0 $this->[$i] = $crawl->( $this->[$i] );
1178 }
1179 }
1180 8 return( $this );
1181 8 };
1182  
1183 8 my $result;
1184 # try-catch
1185 8 local $@;
1186 eval
1187 8 {
1188 8 my $decoded = $j->decode( $data );
1189 8 $result = $crawl->( $decoded );
1190 };
1191 8 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
1192 {
1193 0 return( $self->error( "An error occurred while trying to decode JSON data: $@" ) );
1194 }
1195 8 return( $result );
1196 }
1197  
1198 # 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
1199 sub _encode_json
1200 6 ▶{
Subroutine _encode_json: called CC=2 SCAR=10.3
1201 6 my $self = shift( @_ );
1202 6 my $data = shift( @_ );
1203 6 my $seen = {};
1204 6 my $crawl;
1205 $crawl = sub
1206 6 ▶ {
Subroutine __ANON__: called CC=15 SCAR=46.8
1207 6 my $this = shift( @_ );
1208 6 my $type = Scalar::Util::reftype( $this );
1209 # Skip this reference if it is either hash or array and we have already seen it in order to avoid looping.
1210 6 ▶ 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 6 6
Condition33%2/6
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
!ll&&!rl&&r
060
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
600
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
1211 6 ▶ if( $type eq 'HASH' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 6 0 6
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'SCALAR') { } 0 0 0
1212 {
1213 6 foreach my $k ( keys( %$this ) )
1214 {
1215 17 ▶ next if( !ref( $this->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{$k} 17 0 17
1216 0 $this->{ $k } = $crawl->( $this->{ $k } );
1217 }
1218 }
1219 elsif( $type eq 'ARRAY' )
1220 {
1221 0 ▶ for( my $i = 0; $i < scalar( @$this ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$this) 0 0 0
1222 {
1223 0 ▶ next if( !ref( $this->[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->[$i] 0 0 0
1224 0 $this->[$i] = $crawl->( $this->[$i] );
1225 }
1226 }
1227 elsif( $type eq 'SCALAR' )
1228 {
1229 # The only supported value by JSON for a scalar reference
1230 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
1231 0 my $pkg;
1232 0 ▶ if( ( $pkg = Scalar::Util::blessed( $this ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg = builtin::blessed($this)) { } 0 0 0
1233 {
1234 0 ▶ if( overload::Method( $this => '""' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (overload::Method($this, '""')) { } 0 0 0
1235 {
1236 0 $this = { __scalar_gen_shm => "$this", __package => $pkg };
1237 }
1238 else
1239 {
1240 0 $this = { __scalar_gen_shm => $$this, __package => $pkg };
1241 }
1242 }
1243 else
1244 {
1245 0 $this = { __scalar_gen_shm => $$this };
1246 }
1247 }
1248 6 return( $this );
1249 6 };
1250 6 my $ref = $crawl->( $data );
1251 6 my $j = JSON->new->utf8->relaxed->allow_nonref->convert_blessed;
1252 6 my $encoded;
1253 # try-catch
1254 6 local $@;
1255 eval
1256 6 {
1257 6 $encoded = $j->encode( $ref );
1258 };
1259 6 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
1260 {
1261 0 return( $self->error( "An error occurred while trying to JSON encode data: $@" ) );
1262 }
1263 6 return( $encoded );
1264 }
1265  
1266 22 ▶sub _packing_method { return( shift->_set_get_scalar( '_packing_method', @_ ) ); }
Subroutine _packing_method: called CC=1 SCAR=0.0
1267  
1268 sub _str2key
1269 15 ▶{
Subroutine _str2key: called CC=11 SCAR=30.0
1270 15 my $self = shift( @_ );
1271 15 my $key = shift( @_ );
1272 6 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1273 15 ▶ if( !defined( $key ) || $key eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key or $key eq "") 0 15 15
Condition33%1/3
not defined $key or $key eq ""
l!l&&r!l&&!r
0015
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
1274 {
1275 0 return( &IPC::SysV::IPC_PRIVATE );
1276 }
1277  
1278 15 my $path;
1279 15 ▶ ( $key, $path ) = ref( $key ) eq 'ARRAY' ? @$key : ( $key, [getpwuid($>)]->[7] );
Branch: 1 branch
BranchTrueFalseTotal
ref $key eq 'ARRAY' ? : 0 15 15
1280 15 ▶ if( defined( $path ) && $path =~ /^\d+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and $path =~ /^\d+$/) 0 15 15
Condition33%1/3
defined $path and $path =~ /^\d+$/
!ll&&!rl&&r
0150
MC/DC0%0/2
defined $path and $path =~ /^\d+$/
defined $path $path =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $pathB= $path =~ /^\d+$/
ABresult
0X0
100
111
1281 {
1282 0 $path = [getpwuid($path)]->[7];
1283 }
1284 15 ▶ $path ||= File::Spec->rootdir();
Condition33%1/3
$path ||= "File::Spec"->rootdir
l!l&&r!l&&!r
1500
MC/DC0%0/2
$path ||= "File::Spec"->rootdir
$path "File::Spec"->rootdir
Truth table33%1/3
A ||= B
A= $pathB= "File::Spec"->rootdir
ABresult
1X1
011
000
1285  
1286 15 ▶ if( $key =~ /^\d+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\d+$/) 0 15 15
1287 {
1288 0 my $id = &IPC::SysV::ftok( $path, $key );
1289 0 ▶ return( $self->error( "Unable to get a key using IPC::SysV::ftok: $!" ) ) if( !defined( $id ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
1290 0 return( $id );
1291 }
1292  
1293 15 ▶ $self->_load_class( 'Digest::SHA' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Digest::SHA") or return $self->pass_error 15 0 15
1294  
1295 # ftok() only retains a very small project-id space. Deriving the project
1296 # id from a single byte of a digest therefore creates a high collision
1297 # probability for arbitrary string keys. For string keys, derive a stable
1298 # positive 31-bit SysV key directly from the path and key instead.
1299 15 my $seed = join( "\0", $path, $key );
1300 15 my $hash = Digest::SHA::sha1( $seed );
1301 15 my $val = unpack( 'N', substr( $hash, 0, 4 ) ) & 0x7fffffff;
1302  
1303 # Reserve zero for IPC_PRIVATE semantics.
1304 15 ▶ $val = 1 if( !$val );
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 15 15
1305 15 return( $val );
1306 }
1307  
1308 sub _verify_key
1309 15 ▶{
Subroutine _verify_key: called CC=10 SCAR=33.0
1310 15 my( $self, $key ) = @_;
1311 15 ▶ if( !defined( $key ) || !length( $key // '' ) )
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key && length($key // '')) { } 0 15 15
elsif (not ref $key or $self->_can_overload($key, '""')) { } 15 0 15
elsif ($self->_is_array($key)) { } 0 0 0
Condition40%2/5
defined $key && length($key // '')
!ll&&!rl&&r
0015
$key // ""
l!l
150
MC/DC0%0/2
defined $key && length($key // '')
defined $key $key
Truth table33%1/3
A && B
A= defined $keyB= $key
ABresult
0X0
100
111
1312 {
1313 # It's ok, _str2key will use IPC::SysV::IPC_PRIVATE then
1314 }
1315 ▶ elsif( !ref( $key ) || $self->_can_overload( $key => '""' ) )
Condition33%1/3
not ref $key or $self->_can_overload($key, "\"\"")
l!l&&r!l&&!r
1500
MC/DC0%0/2
not ref $key or $self->_can_overload($key, "\"\"")
not ref $key $self->_can_overload($key, "\"\"")
Truth table33%1/3
not(A) or B
A= not ref $keyB= $self->_can_overload($key, "\"\"")
ABresult
1X1
011
000
1316 {
1317 15 $key = "$key";
1318 15 ▶ if( !CORE::length( $key // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length($key // "")) 0 15 15
Condition50%1/2
$key // ""
l!l
150
MC/DC0%0/1
$key // ""
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
1319 {
1320 0 return( $self->error( "An empty key was provided." ) );
1321 }
1322 }
1323 elsif( $self->_is_array( $key ) )
1324 {
1325 # We use as-is
1326 }
1327 else
1328 {
1329 0 ▶ return( $self->error( "Key must be a string, but I got '", $self->_str_val( $key // 'undef' ), "'" ) );
Condition0%0/2
$key // "undef"
l!l
00
MC/DC0%0/1
$key // "undef"
$key
Truth table0%0/2
A
A= $key
Aresult
11
00
1330 }
1331 15 return( $key );
1332 }
1333  
1334 sub DESTROY
1335 13 ▶{
Subroutine DESTROY: called CC=12 SCAR=30.0
1336 # <https://perldoc.perl.org/perlobj#Destructors>
1337 13 CORE::local( $., $@, $!, $^E, $? );
1338 13 ▶ CORE::return if( Module::Generic::_in_global_destruction() );
Branch: 1 branch
BranchTrueFalseTotal
if Module::Generic::_in_global_destruction() 0 13 13
1339 13 my $self = CORE::shift( @_ );
1340 13 ▶ CORE::return if( !CORE::defined( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 13 13
1341 13 ▶ CORE::return if( !CORE::defined( $self->{id} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"id"} 11 2 13
1342  
1343 # For non-object context, we need to call cleanup to ensure there is no leftover
1344 2 my $class = CORE::ref( $self );
1345 2 my $serial = $self->serial;
1346 2 ▶ if( defined( $serial ) && CORE::length( "$serial" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $serial and length "$serial") 2 0 2
Condition33%1/3
defined $serial and length "$serial"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $serial and length "$serial"
defined $serial length "$serial"
Truth table33%1/3
A and B
A= defined $serialB= length "$serial"
ABresult
0X0
100
111
1347 {
1348 2 my $serial2semid_repo = Module::Generic::Global->new( 'serial2semid' => $class, key => $serial );
1349 2 $serial2semid_repo->cleanup;
1350 }
1351  
1352 2 my $shem_repo = Module::Generic::Global->new( 'shem_repo' => $class, key => $class );
1353 2 $shem_repo->cleanup;
1354  
1355 2 ▶ if( CORE::defined( $self->{id} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"id"}) 2 0 2
1356 {
1357 2 my $id2obj_repo = Module::Generic::Global->new( 'id2obj' => $class, key => $self->{id} );
1358 2 $id2obj_repo->cleanup;
1359 }
1360  
1361 2 ▶ $self->unlock if( $self->locked );
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked 0 2 2
1362 2 $self->detach;
1363  
1364 2 ▶ if( $self->destroy_semaphore )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->destroy_semaphore) 0 2 2
1365 {
1366 0 $self->remove_semaphore;
1367 }
1368  
1369 2 ▶ if( $self->destroy )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->destroy) 0 2 2
1370 {
1371 0 my $stat = $self->shmstat();
1372 0 ▶ if( defined( $stat ) && ( $stat->nattch() == 0 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stat and $stat->nattch == 0) 0 0 0
Condition0%0/3
defined $stat and $stat->nattch == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $stat and $stat->nattch == 0
defined $stat $stat->nattch == 0
Truth table0%0/3
A and B
A= defined $statB= $stat->nattch == 0
ABresult
0X0
100
111
1373 {
1374 0 $self->remove;
1375 }
1376 }
1377 }
1378  
1379 sub FREEZE
1380 6 ▶{
Subroutine FREEZE: called CC=6 SCAR=18.5
Pod undocumented
1381 6 my $self = CORE::shift( @_ );
1382 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
1383 6 my $class = CORE::ref( $self );
1384 # We skip 'owner' on purpose, since it represents the process ID
1385 6 my @props = qw(
1386 base64 create destroy destroy_semaphore exclusive key mode
1387 serial size _packing_method
1388 );
1389 6 my $hash = {};
1390 6 foreach my $prop ( @props )
1391 {
1392 60 ▶ if( exists( $self->{ $prop } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$prop}) 60 0 60
1393 {
1394 60 $hash->{ $prop } = $self->{ $prop };
1395 }
1396 }
1397 # CORE::delete( @hash{ qw( addr id locked owner removed removed_semaphore semid ) } );
1398 # Return an array reference rather than a list so this works with Sereal and CBOR
1399 # On or before Sereal version 4.023, Sereal did not support multiple values returned
1400 6 ▶ CORE::return( [$class, $hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023") 0 6 6
Condition66%2/3
$serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
!ll&&!rl&&r
510
MC/DC0%0/2
$serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
$serialiser eq "Sereal" "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
Truth table66%2/3
A and B
A= $serialiser eq "Sereal"B= "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
ABresult
0X0
100
111
1401 # But Storable want a list with the first element being the serialised element
1402 6 CORE::return( $class, $hash );
1403 }
1404  
1405 5 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
1406  
1407 1 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
1408  
1409 # 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.
1410 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
1411 sub THAW
1412 2 ▶{
Subroutine THAW: called CC=10 SCAR=27.6
Pod undocumented
1413 2 my( $self, undef, @args ) = @_;
1414 2 ▶ 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 2 2
Condition33%1/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
200
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
1415 2 ▶ 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 ? : 2 0 2
Condition22%2/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
002
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
002
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
1416 2 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 2 0 2
1417 2 my $new;
1418 # Storable pattern requires to modify the object it created rather than returning a new one
1419 2 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1 1 2
1420 {
1421 1 foreach( CORE::keys( %$hash ) )
1422 {
1423 10 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
1424 }
1425 1 $new = $self;
1426 }
1427 else
1428 {
1429 1 $new = CORE::bless( $hash => $class );
1430 }
1431 2 CORE::return( $new );
1432 }
1433  
1434 # NOTE: END
1435 # END
1436 # {
1437 # my $shem_repo = Module::Generic::Global->new( 'shem_repo' => __PACKAGE__, key => __PACKAGE__ );
1438 # $shem_repo->lock;
1439 # my $all_shem = $shem_repo->get // [];
1440 # foreach my $id ( @$all_shem )
1441 # {
1442 # my $id2obj_repo = Module::Generic::Global->new( 'id2obj' => __PACKAGE__, key => $id );
1443 # my $s = $id2obj_repo->get || next;
1444 # next if( $s->removed || !$s->id || !$s->destroy );
1445 # $s->detach;
1446 # $s->remove;
1447 # }
1448 # $shem_repo->unlock;
1449 # $shem_repo->remove;
1450 # };
1451  
1452 # NOTE: Module::Generic::SharedStat class
1453 {
1454 package
1455 Module::Generic::SharedStat;
1456 6 ▶ use IPC::SysV;
Subroutine BEGIN: called CC=1 SCAR=0.0
1457 6 require IPC::SharedMem;
1458 6 our $VERSION = 'v0.1.0';
1459  
1460 6 ▶ use constant UID => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1461 6 ▶ use constant GID => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1462 6 ▶ use constant CUID => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1463 6 ▶ use constant CGID => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
1464 6 ▶ use constant MODE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1465 6 ▶ use constant SEGSZ => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1466 6 ▶ use constant LPID => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
1467 6 ▶ use constant CPID => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
1468 6 ▶ use constant NATTCH => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
1469 6 ▶ use constant ATIME => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
1470 6 ▶ use constant DTIME => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
1471 6 ▶ use constant CTIME => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
1472  
1473 sub new
1474 0 ▶ {
Subroutine new: not called CC=2 SCAR=17.9
1475 0 my $this = shift( @_ );
1476 0 my @vals = @_;
1477 0 ▶ return( bless( [ @vals ] => ref( $this ) || $this ) );
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
1478 }
1479  
1480 sub unpack
1481 0 ▶ {
Subroutine unpack: not called CC=1 SCAR=6.9
1482 0 my $self = shift( @_ );
1483 0 my $data = shift( @_ );
1484 # XS method
1485 0 my $d = IPC::SharedMem::stat->new->unpack( $data );
1486 # my @unpacked = unpack( "i*", $data );
1487 0 return( $self->new( @$d ) );
1488 }
1489  
1490 # time when the last attach was completed to the associated shared memory segment.
1491 0 ▶ sub atime { return( shift->[ATIME] ); }
Subroutine atime: not called CC=1 SCAR=6.9
1492  
1493 0 ▶ sub cgid { return( shift->[CGID] ); }
Subroutine cgid: not called CC=1 SCAR=6.9
1494  
1495 # process ID of the creator of the shared memory entry.
1496 0 ▶ sub cpid { return( shift->[CPID] ); }
Subroutine cpid: not called CC=1 SCAR=6.9
1497  
1498 # time when the associated entry was created or changed.
1499 0 ▶ sub ctime { return( shift->[CTIME] ); }
Subroutine ctime: not called CC=1 SCAR=6.9
1500  
1501 0 ▶ sub cuid { return( shift->[CUID] ); }
Subroutine cuid: not called CC=1 SCAR=6.9
1502  
1503 # time the last detach was completed on the associated shared memory segment.
1504 0 ▶ sub dtime { return( shift->[DTIME] ); }
Subroutine dtime: not called CC=1 SCAR=6.9
1505  
1506 0 ▶ sub gid { return( shift->[GID] ); }
Subroutine gid: not called CC=1 SCAR=6.9
1507  
1508 # process ID of the last process to attach or detach the shared memory segment.
1509 0 ▶ sub lpid { return( shift->[LPID] ); }
Subroutine lpid: not called CC=1 SCAR=6.9
1510  
1511 0 ▶ sub mode { return( shift->[MODE] ); }
Subroutine mode: not called CC=1 SCAR=6.9
1512  
1513 # number of processes attached to the associated shared memory segment.
1514 0 ▶ sub nattch { return( shift->[NATTCH] ); }
Subroutine nattch: not called CC=1 SCAR=6.9
1515  
1516 # size of the associated shared memory segment in bytes.
1517 0 ▶ sub segsz { return( shift->[SEGSZ] ); }
Subroutine segsz: not called CC=1 SCAR=6.9
1518  
1519 0 ▶ sub uid { return( shift->[UID] ); }
Subroutine uid: not called CC=1 SCAR=6.9
1520  
1521 sub FREEZE
1522 0 ▶ {
Subroutine FREEZE: not called CC=4 SCAR=30.0
1523 0 my $self = CORE::shift( @_ );
1524 0 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition0%0/2
shift @_ // ""
l!l
00
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
1525 0 my $class = CORE::ref( $self );
1526 0 my @array = @$self;
1527 # Return an array reference rather than a list so this works with Sereal
1528 # On or before Sereal version 4.023, Sereal did not support multiple values returned
1529 0 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 0 0 0
1530 {
1531 0 require Sereal::Encoder;
1532 0 require version;
1533
1534 0 ▶ 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 0 0
1535 {
1536 0 CORE::return( [$class, \@array] );
1537 }
1538 }
1539 # But CBOR and Storable want a list with the first element being the serialised element
1540 0 CORE::return( $class, \@array );
1541 }
1542  
1543 0 ▶ sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
1544  
1545 0 ▶ sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
1546  
1547 # 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.
1548 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
1549 sub THAW
1550 0 ▶ {
Subroutine THAW: not called CC=9 SCAR=45.0
1551 0 my( $self, undef, @args ) = @_;
1552 0 ▶ 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 0 0
Condition0%0/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
1553 0 ▶ 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 ? : 0 0 0
Condition0%0/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
000
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
000
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 table0%0/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
1554 0 ▶ my $array = CORE::ref( $ref ) eq 'ARRAY' ? $ref : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
1555 # Storable pattern requires to modify the object it created rather than returning a new one
1556 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
1557 {
1558 0 @$self = @$array;
1559 0 CORE::return( $self );
1560 }
1561 else
1562 {
1563 0 my $new = bless( $array => $class );
1564 0 CORE::return( $new );
1565 }
1566 }
1567  
1568 0 ▶ sub TO_JSON { CORE::return( [ @{$_[0]} ] ); }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
1569 }
1570  
1571 # NOTE: Module::Generic::SemStat class
1572 {
1573 6 package
1574 Module::Generic::SemStat;
1575 6 ▶ use IPC::SysV;
Subroutine BEGIN: called CC=1 SCAR=0.0
1576 6 require IPC::Semaphore;
1577 6 our $VERSION = 'v0.1.0';
1578  
1579 6 ▶ use constant UID => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1580 6 ▶ use constant GID => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1581 6 ▶ use constant CUID => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1582 6 ▶ use constant CGID => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
1583 6 ▶ use constant MODE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1584 6 ▶ use constant CTIME => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1585 6 ▶ use constant OTIME => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
1586 6 ▶ use constant NSEMS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
1587  
1588 sub new
1589 0 ▶ {
Subroutine new: not called CC=2 SCAR=17.9
1590 0 my $this = shift( @_ );
1591 0 my @vals = @_;
1592 0 ▶ return( bless( [ @vals ] => ref( $this ) || $this ) );
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
1593 }
1594  
1595 sub unpack
1596 0 ▶ {
Subroutine unpack: not called CC=1 SCAR=6.9
1597 0 my $self = shift( @_ );
1598 0 my $data = shift( @_ );
1599 # my @unpacked = unpack( "i*", $data );
1600 # XS method
1601 0 my $d = IPC::Semaphore::stat->new->unpack( $data );
1602 0 return( $self->new( @$d ) );
1603 }
1604  
1605 0 ▶ sub cgid { return( shift->[CGID] ); }
Subroutine cgid: not called CC=1 SCAR=6.9
1606  
1607 0 ▶ sub ctime { return( shift->[CTIME] ); }
Subroutine ctime: not called CC=1 SCAR=6.9
1608  
1609 0 ▶ sub cuid { return( shift->[CUID] ); }
Subroutine cuid: not called CC=1 SCAR=6.9
1610  
1611 0 ▶ sub gid { return( shift->[GID] ); }
Subroutine gid: not called CC=1 SCAR=6.9
1612  
1613 0 ▶ sub mode { return( shift->[MODE] ); }
Subroutine mode: not called CC=1 SCAR=6.9
1614  
1615 # number of semaphores in the set associated with the semaphore entry.
1616 0 ▶ sub nsems { return( shift->[NSEMS] ); }
Subroutine nsems: not called CC=1 SCAR=6.9
1617  
1618 # time the last semaphore operation was completed on the set associated with the semaphore entry.
1619 0 ▶ sub otime { return( shift->[OTIME] ); }
Subroutine otime: not called CC=1 SCAR=6.9
1620  
1621 0 ▶ sub uid { return( shift->[UID] ); }
Subroutine uid: not called CC=1 SCAR=6.9
1622  
1623 sub FREEZE
1624 0 ▶ {
Subroutine FREEZE: not called CC=4 SCAR=30.0
1625 0 my $self = CORE::shift( @_ );
1626 0 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition0%0/2
shift @_ // ""
l!l
00
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
1627 0 my $class = CORE::ref( $self );
1628 0 my @array = @$self;
1629 # Return an array reference rather than a list so this works with Sereal
1630 # On or before Sereal version 4.023, Sereal did not support multiple values returned
1631 0 ▶ CORE::return( [$class, \@array] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023") 0 0 0
Condition0%0/3
$serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
!ll&&!rl&&r
000
MC/DC0%0/2
$serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
$serialiser eq "Sereal" "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
Truth table0%0/3
A and B
A= $serialiser eq "Sereal"B= "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
ABresult
0X0
100
111
1632 # But CBOR and Storable want a list with the first element being the serialised element
1633 0 CORE::return( $class, \@array );
1634 }
1635  
1636 0 ▶ sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
1637  
1638 0 ▶ sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
1639  
1640 # 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.
1641 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
1642 sub THAW
1643 0 ▶ {
Subroutine THAW: not called CC=9 SCAR=45.0
1644 0 my( $self, undef, @args ) = @_;
1645 0 ▶ 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 0 0
Condition0%0/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
1646 0 ▶ 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 ? : 0 0 0
Condition0%0/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
000
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
000
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 table0%0/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
1647 0 ▶ my $array = CORE::ref( $ref ) eq 'ARRAY' ? $ref : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
1648 # Storable pattern requires to modify the object it created rather than returning a new one
1649 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
1650 {
1651 0 @$self = @$array;
1652 0 CORE::return( $self );
1653 }
1654 else
1655 {
1656 0 my $new = bless( $array => $class );
1657 0 CORE::return( $new );
1658 }
1659 }
1660  
1661 0 ▶ sub TO_JSON { CORE::return( [ @{$_[0]} ] ); }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
1662 }
1663  
1664 6 1;
1665  
1666 __END__
« lib/Module/Generic/SharedMemXS.pm ↑ Summary lib/Module/Generic/Scalar.pm »