Shm/Make.pm

filter: Statementstmt 55.7% 39 / 70 Branchbran 45.0% 9 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 72.7% 8 / 11 Podpod 100% 3 / 3 totaltotal 56.7% 59 / 104 CC 11 SCAR 27.0
CC
11
Cov
53%
makeshm
27.3
_makeshm_scalar
2.0
_makeshm_array
2.0
CRAP 14.9
« Shm/Tied/ARRAY.pm ↑ Summary Shm/Segment.pm »
1 package IPC::Shm::Make;
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #
6 # Copyright (c) 2014 by Kevin Cody-Little <kcody@cpan.org>
7 #
8 # This code may be modified or redistributed under the terms
9 # of either the Artistic or GNU General Public licenses, at
10 # the modifier or redistributor's discretion.
11 #
12  
13 =head1 NAME
14  
15 IPC::Shm::Make
16  
17 =head1 SYNOPSIS
18  
19 This module is part of the IPC::Shm::Tied::* implementations. You should
20 definitely not be using it directly.
21  
22 =head1 FUNCTIONS
23  
24 =head2 makeshm( $scalar_variable_reference );
25  
26 If the referenced variable contains a plain scalar, nothing is done.
27  
28 If the referenced variable itself contains a reference, the target of that
29 inner reference is tied into shared memory with its contents preserved.
30  
31 The inner reference is then replaced with a stand-in containing an
32 identifier, which can be used to recover the original (now tied) target.
33  
34 =head2 getback( $standin );
35  
36 Given the standin left by makeshm, returns a reference to the original
37 (now tied into shared memory) data. It's up to the calling program to
38 know whether it expects a scalar, array, or hash reference.
39  
40 =head2 getback_discard( $standin );
41  
42 The same as C<getback> but also decrements the reference counter.
43  
44 =cut
45  
46 ###############################################################################
47 # library dependencies
48  
49 6 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 6 our @EXPORT = qw( makeshm getback );
51  
52 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 ###############################################################################
55 # lower level migration handlers
56  
57 0 ▶sub _makeshm_scalar {
Subroutine _makeshm_scalar: not called CC=1 SCAR=6.9
58 0 my ( $ref ) = @_;
59  
60 0 my $tmp = $$ref;
61 0 my $obj = tie $$ref, 'IPC::Shm::Tied';
62  
63 0 $obj->writelock;
64 0 $$ref = $tmp;
65 0 $obj->incref;
66 0 $obj->unlock;
67  
68 # $obj->reftype( 'SCALAR' );
69 0 $obj->tiedref( $ref );
70  
71 0 return $obj->standin;
72 }
73  
74 0 ▶sub _makeshm_array {
Subroutine _makeshm_array: not called CC=1 SCAR=6.9
75 0 my ( $ref ) = @_;
76  
77 0 my @tmp = @$ref;
78 0 my $obj = tie @$ref, 'IPC::Shm::Tied';
79  
80 0 $obj->writelock;
81 0 @$ref = @tmp;
82 0 $obj->incref;
83 0 $obj->unlock;
84  
85 # $obj->reftype( 'ARRAY' );
86 0 $obj->tiedref( $ref );
87  
88 0 return $obj->standin;
89 }
90  
91 5 ▶sub _makeshm_hash {
Subroutine _makeshm_hash: called CC=1 SCAR=0.0
92 5 my ( $ref ) = @_;
93  
94 5 my %tmp = %$ref;
95 5 my $obj = tie %$ref, 'IPC::Shm::Tied';
96  
97 5 $obj->writelock;
98 5 %$ref = %tmp;
99 5 $obj->incref;
100 5 $obj->unlock;
101  
102 # $obj->reftype( 'HASH' );
103 5 $obj->tiedref( $ref );
104  
105 5 return $obj->standin;
106 }
107  
108  
109 ###############################################################################
110 # migrate a variable into anonymous shared memory
111  
112 60 ▶sub makeshm {
Subroutine makeshm: called CC=11 SCAR=33.1
Pod documented
113 60 my ( $valueref ) = @_;
114  
115 60 my $reftype = ref( $$valueref );
116  
117 # pass plain scalars as-is
118 60 ▶ return unless $reftype;
Branch: 1 branch
BranchTrueFalseTotal
unless $reftype 54 6 60
119  
120 # save a few dereferences
121 6 my $refdata = $$valueref;
122  
123 6 ▶ if ( $reftype eq 'SCALAR' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'SCALAR') { } 1 5 6
elsif ($reftype eq 'ARRAY') { } 0 5 5
elsif ($reftype eq 'HASH') { } 5 0 5
124 1 ▶ if ( my $obj = tied $$refdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = tied $$refdata) { } 1 0 1
125 1 ▶ confess "Cannot store tied scalars in shared memory"
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("IPC::Shm::Tied") 0 1 1
126 unless $obj->isa( 'IPC::Shm::Tied' );
127 1 $obj->incref;
128 1 $$valueref = $obj->standin;
129 } else {
130 0 $$valueref = _makeshm_scalar( $refdata );
131 }
132 }
133  
134 elsif ( $reftype eq 'ARRAY' ) {
135 0 ▶ if ( my $obj = tied @$refdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = tied @$refdata) { } 0 0 0
136 0 ▶ confess "Cannot store tied arrays in shared memory"
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("IPC::Shm::Tied") 0 0 0
137 unless $obj->isa( 'IPC::Shm::Tied' );
138 0 $obj->incref;
139 0 $$valueref = $obj->standin;
140 } else {
141 0 $$valueref = _makeshm_array( $refdata );
142 }
143 }
144  
145 elsif ( $reftype eq 'HASH' ) {
146 5 ▶ if ( my $obj = tied %$refdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = tied %$refdata) { } 0 5 5
147 0 ▶ confess "Cannot store tied hashes in shared memory"
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("IPC::Shm::Tied") 0 0 0
148 unless $obj->isa( 'IPC::Shm::Tied' );
149 0 $obj->incref;
150 0 $$valueref = $obj->standin;
151 } else {
152 5 $$valueref = _makeshm_hash( $refdata );
153 }
154 }
155  
156 else {
157 0 confess "Incompatible reference type $reftype";
158 }
159  
160 }
161  
162  
163 ###############################################################################
164 # get back the (now shared) referenced datum
165  
166 12 ▶sub getback {
Subroutine getback: called CC=1 SCAR=0.0
Pod documented
167 12 my ( $standin ) = @_;
168  
169 12 return IPC::Shm::Tied->standin_tiedref( $standin );
170 }
171  
172 0 ▶sub getback_discard {
Subroutine getback_discard: not called CC=1 SCAR=6.9
Pod documented
173 0 my ( $standin ) = @_;
174  
175 0 my $this = IPC::Shm::Tied->standin_discard( $standin );
176  
177 0 return $this->tiedref;
178 }
179  
180  
181 ###############################################################################
182 ###############################################################################
183  
184 =head1 AUTHOR
185  
186 Kevin Cody-Little <kcody@cpan.org>
187  
188 =cut
189  
190 6 1;
« Shm/Tied/ARRAY.pm ↑ Summary Shm/Segment.pm »