blib/lib/Object/LocalVars.pm

filter: Statementstmt 95.4% 311 / 326 Branchbran 88.4% 92 / 104 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 97.9% 47 / 48 Podpod 100% 5 / 5 totaltotal 91.7% 464 / 506 CC 88 SCAR 14.0
CC
88
Cov
92%
_install_accessors
10.0
MODIFY_SCALAR_ATTRIBUTES
10.0
CLONE
8.2
CRAP 4.1
↑ Summary
1 34 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Object::LocalVars;
6 # ABSTRACT: Outside-in objects with local aliasing of $self and object variables
7 7 our $VERSION = '0.21'; # VERSION
8  
9 #--------------------------------------------------------------------------#
10 # Required modules
11 #--------------------------------------------------------------------------#
12  
13 22 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use Scalar::Util 1.09 qw( weaken refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #--------------------------------------------------------------------------#
18 # Exporting -- wrap import so we can check for necessary warnings
19 #--------------------------------------------------------------------------#
20  
21 20 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 our @EXPORT = qw(
24 caller give_methods new BUILDALL CLONE DESTROY
25 MODIFY_SCALAR_ATTRIBUTES MODIFY_CODE_ATTRIBUTES
26 );
27  
28 25 ▶sub import {
Subroutine import: called CC=4 SCAR=15.2
29  
30 # check if threads are available
31 27 ▶ if( $Config{useithreads} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Object::LocalVars::Config{"useithreads"}) 10 37 47
32 2 my $caller = caller(0);
33
34 # Warn about sharing, but not for Test:: modules which always
35 # share if any threads are enabled
36 2 ▶ if ( $INC{'threads/shared.pm'} && ! $INC{'Test/Builder.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"threads/shared.pm"} and not $INC{"Test/Builder.pm"}) 2 0 2
Condition0%0/3
$INC{"threads/shared.pm"} and not $INC{"Test/Builder.pm"}
!ll&&!rl&&r
000
MC/DC0%0/2
$INC{"threads/shared.pm"} and not $INC{"Test/Builder.pm"}
$INC{"threads/shared.pm"} not $INC{"Test/Builder.pm"}
Truth table0%0/3
A and not(B)
A= $INC{"threads/shared.pm"}B= not $INC{"Test/Builder.pm"}
ABresult
0X0
100
111
37 2 carp "Warning: threads::shared is enabled, but $caller uses"
38 . " Object::LocalVars (which does not allow shared objects)";
39 }
40 }
41
42 # Hand off the rest of the import
43 27 goto &Exporter::import;
44 }
45  
46 #--------------------------------------------------------------------------#
47 # Declarations
48 #--------------------------------------------------------------------------#
49
50 7 my (%public_methods, %protected_methods, %private_methods);
51  
52 7 my %base_class_of;
53  
54 7 my %prefixes_for;
55  
56 #--------------------------------------------------------------------------#
57 # accessor_style
58 #--------------------------------------------------------------------------#
59  
60 5 ▶sub accessor_style {
Subroutine accessor_style: called CC=2 SCAR=6.9
Pod documented
61 7 my (undef, $prefix) = @_;
62 7 ▶ croak "Method accessor_style() requires a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prefix eq "HASH" 4 4 8
63 if not ref $prefix eq 'HASH';
64 4 my $class = caller(0);
65 4 $prefixes_for{ $class } = $prefix;
66 }
67  
68 #--------------------------------------------------------------------------#
69 # base_object
70 #--------------------------------------------------------------------------#
71  
72 2 ▶sub base_object {
Subroutine base_object: called CC=3 SCAR=11.0
Pod documented
73 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 my (undef, $base) = @_;
75 2 my $class = caller(0);
76 2 $base_class_of{ $class } = $base;
77
78 # import it if not already in @ISA
79 2 ▶ if ( ! grep { $_ eq $base } @{$class."::ISA"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $base;} @{$class . '::ISA';}) 4 1 5
80 1 push @{$class."::ISA"}, $base;
81 1 $base =~ s{::}{/}g;
82 1 $base .= ".pm";
83 1 eval { require $base };
84 1 ▶ croak $@ if $@ ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 1 2 3
85 }
86  
87 # change to other form of new
88 {
89 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 *{$class."::new"} = \&_new_with_base;
91 }
92
93 }
94  
95 #--------------------------------------------------------------------------#
96 # caller
97 #--------------------------------------------------------------------------#
98  
99 # custom caller routine ignores this module and keeps looking upwards.
100 # can't use Sub::Uplevel due to an off-by-one issue in the current version
101  
102 24 ▶use subs 'caller';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 40 ▶sub caller {
Subroutine caller: called CC=5 SCAR=16.1
104 40 my ($uplevel) = @_;
105 40 ▶ $uplevel ||= 0;
Condition50%1/2
$uplevel ||= 0
l!l
040
MC/DC0%0/1
$uplevel ||= 0
$uplevel
Truth table50%1/2
A
A= $uplevel
Aresult
11
00
106 40 ▶ $uplevel++ while ( (CORE::caller($uplevel+1))[0] eq __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
(CORE::caller $uplevel + 1)[0] eq __PACKAGE__ and do { ++$uplevel } 3 40 43
107 40 my @caller = CORE::caller($uplevel+1);
108 40 ▶ return wantarray ? ( @_ ? @caller : @caller[0 .. 2] ) : $caller[0];
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 28 17 45
@_ ? : 2 24 26
109 }
110  
111 #--------------------------------------------------------------------------#
112 # give_methods
113 #--------------------------------------------------------------------------#
114  
115 16 ▶sub give_methods {
Subroutine give_methods: called CC=4 SCAR=13.9
Pod documented
116 16 my $package = caller;
117 16 for ( @{$public_methods{$package}} ) {
118 69 _install_wrapper($package, $_, "public");
119 };
120 16 for ( @{$protected_methods{$package}} ) {
121 1 _install_wrapper($package, $_, "protected");
122 };
123 16 for ( @{$private_methods{$package}} ) {
124 2 _install_wrapper($package, $_, "private");
125 };
126 16 return 1;
127 }
128  
129 #--------------------------------------------------------------------------#
130 # new()
131 #--------------------------------------------------------------------------#
132  
133 37 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
134 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
135 37 my ($class, @args) = @_;
136 37 ▶ die "new can't be called on an object" if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 3 38 41
137  
138 # create blessed object
139 37 my $self = \do{ my $scalar };
140 37 bless $self, $class;
141  
142 # call initializer
143 37 return BUILDALL( $class, $self, @args );
144 }
145  
146 2 ▶sub _new_with_base {
Subroutine _new_with_base: called CC=3 SCAR=11.0
147 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
148 2 my ($class, @args) = @_;
149 2 ▶ die "new can't be called on an object" if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 3 2 5
150  
151 # create blessed object
152 2 my $base_class = $base_class_of{ $class };
153 2 my $prebuild = *{$class."::PREBUILD"}{CODE};
154 my @filtered_args
155 2 ▶ = defined $prebuild ? $prebuild->($base_class, @args) : @args;
Branch: 1 branch
BranchTrueFalseTotal
defined $prebuild ? : 2 2 4
156 2 my $self = $base_class->new( @filtered_args );
157 2 bless $self, $class;
158 2 my $addr = refaddr $self;
159 2 ${$class . "::TRACKER"}{$addr} = $self;
160 2 weaken ${$class . "::TRACKER"}{$addr}; # don't let this stop destruction
161  
162 # call initializer -- but skip base_class
163 {
164 2 local @{$class."::ISA"}
165 2 = grep { $_ ne $base_class } @{$class."::ISA"};
166 2 return BUILDALL( $class, $self, @_ );
167 }
168 }
169  
170 #--------------------------------------------------------------------------#
171 # BUILDALL
172 #--------------------------------------------------------------------------#
173  
174 43 ▶sub BUILDALL {
Subroutine BUILDALL: called CC=6 SCAR=17.9
Pod documented
175 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
176 43 my ($class, $self, @args) = @_;
177
178 # return if we've already initialized this class
179 43 my $addr = refaddr $self;
180 43 ▶ return $self if ( exists ${$class . "::TRACKER"}{$addr} );
Branch: 1 branch
BranchTrueFalseTotal
if exists do { $class . '::TRACKER' }->{$addr} 3 41 44
181  
182 # otherwise register $self in the tracker and continue
183 41 ${$class . "::TRACKER"}{$addr} = $self;
184 41 weaken ${$class . "::TRACKER"}{$addr}; # don't let this stop destruction
185
186 # initialize superclasses if they can
187 41 for my $superclass (@{"${class}::ISA"}) {
188 4 ▶ if ( my $super_buildall = $superclass->can( 'BUILDALL' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $super_buildall = $superclass->can("BUILDALL")) 4 0 4
189 4 my $prebuild = *{$class."::PREBUILD"}{CODE};
190 4 ▶ my @filtered_args =
Branch: 1 branch
BranchTrueFalseTotal
defined $prebuild ? : 0 4 4
191 defined $prebuild ? $prebuild->($superclass, @args) : @args;
192 4 $super_buildall->($superclass, $self, @filtered_args);
193 }
194 }
195
196 # initialize self if we have an initializer
197 11 *{$class."::BUILD"}{CODE}->($self, @args)
198 41 ▶ if defined *{$class."::BUILD"}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$class . '::BUILD';}{"CODE"} 11 30 41
199 41 return $self;
200 }
201  
202 #--------------------------------------------------------------------------#
203 # CLONE
204 #--------------------------------------------------------------------------#
205  
206 0 ▶sub CLONE {
Subroutine CLONE: not called CC=3 SCAR=21.1
207 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
208 0 my $class = shift;
209 0 for my $old_obj_id ( keys %{$class . "::TRACKER"} ) {
210 my $new_obj_id = refaddr(
211 0 ${$class . "::TRACKER"}{$old_obj_id}
212 );
213 0 for my $prop ( keys %{"${class}::DATA::"} ) {
214 0 my $qualified_name = $class . "::DATA::$prop";
215 0 $$qualified_name{ $new_obj_id } = $$qualified_name{ $old_obj_id };
216 0 delete $$qualified_name{ $old_obj_id };
217 }
218 0 ${$class . "::TRACKER"}{$new_obj_id} = $new_obj_id;
219 0 delete ${$class . "::TRACKER"}{$old_obj_id};
220 }
221 0 return 1;
222 }
223  
224 #--------------------------------------------------------------------------#
225 # DESTROY
226 #--------------------------------------------------------------------------#
227  
228 44 ▶sub DESTROY {
Subroutine DESTROY: called CC=7 SCAR=19.5
229 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
230 44 my ($self, $class) = @_;
231 44 ▶ $class ||= ref $self;
Condition66%2/3
$class ||= ref $self
l!l&&r!l&&!r
4400
MC/DC0%0/2
$class ||= ref $self
$class ref $self
Truth table66%2/3
A ||= B
A= $classB= ref $self
ABresult
1X1
011
000
232
233 # return if we've already destructed this class
234 44 my $addr = refaddr $self;
235 44 ▶ return if ( ! exists ${$class . "::TRACKER"}{$addr} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { $class . '::TRACKER' }->{$addr} 1 43 44
236
237 # otherwise mark that we're destroying this class and continue
238 43 delete ${$class . "::TRACKER"}{$addr};
239
240 # demolish and free data for this class
241 11 *{$class."::DEMOLISH"}{CODE}->($self)
242 43 ▶ if defined *{$class."::DEMOLISH"}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$class . '::DEMOLISH';}{"CODE"} 11 32 43
243 43 for ( keys %{"${class}::DATA::"} ) {
244 78 delete (${"${class}::DATA::$_"}{$addr});
245 }
246  
247 # destroy all superclasses
248 43 for my $superclass ( @{"${class}::ISA"} ) {
249 6 ▶ if ( my $super_destroyer = $superclass->can("DESTROY") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $super_destroyer = $superclass->can("DESTROY")) 4 2 6
250 4 $super_destroyer->($self, $superclass);
251 }
252 }
253  
254 }
255  
256 #--------------------------------------------------------------------------#
257 # MODIFY_CODE_ATTRIBUTES
258 #--------------------------------------------------------------------------#
259  
260 72 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=5 SCAR=16.1
261 72 my ($package, $referent, @attrs) = @_;
262 72 for my $attr (@attrs) {
263 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
264 72 ▶ if ( $attr =~ /^(?:Method|Pub)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($attr =~ /^(?:Method|Pub)$/) { } 69 3 72
elsif ($attr eq 'Prot') { } 1 2 3
elsif ($attr eq 'Priv') { } 2 0 2
265 69 push @{$public_methods{$package}}, $referent;
266 69 undef $attr;
267 }
268 elsif ($attr eq "Prot") {
269 1 push @{$protected_methods{$package}}, $referent;
270 1 undef $attr;
271 }
272 elsif ($attr eq "Priv") {
273 2 push @{$private_methods{$package}}, $referent;
274 2 undef $attr;
275 }
276 }
277 72 return grep {defined} @attrs;
278 }
279  
280 #--------------------------------------------------------------------------#
281 # MODIFY_SCALAR_ATTRIBUTES
282 #--------------------------------------------------------------------------#
283  
284 53 ▶sub MODIFY_SCALAR_ATTRIBUTES {
Subroutine MODIFY_SCALAR_ATTRIBUTES: called CC=10 SCAR=23.0
285 53 my ($OL_PACKAGE, $referent, @attrs) = @_;
286 53 for my $attr (@attrs) {
287 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
288 53 ▶ if ($attr eq "Pub") {
Branch: 8 branches
BranchTrueFalseTotal
if ($attr eq 'Pub') { } 35 18 53
elsif ($attr eq 'Prot') { } 1 17 18
elsif ($attr =~ /^(?:Prop|Priv)$/) { } 2 15 17
elsif ($attr =~ /^(?:ReadOnly)$/) { } 1 14 15
elsif ($attr =~ /^(?:Class|ClassPriv)$/) { } 11 3 14
elsif ($attr =~ /^(?:ClassProt)$/) { } 1 2 3
elsif ($attr =~ /^(?:ClassPub)$/) { } 1 1 2
elsif ($attr =~ /^(?:ClassReadOnly)$/) { } 1 0 1
289 35 _install_accessors( $OL_PACKAGE, $referent, "public", 0 );
290 35 undef $attr;
291 }
292 elsif ($attr eq "Prot") {
293 1 _install_accessors( $OL_PACKAGE, $referent, "protected", 0 );
294 1 undef $attr;
295 }
296 elsif ( $attr =~ /^(?:Prop|Priv)$/ ) {
297 2 _install_accessors( $OL_PACKAGE, $referent, "private", 0 );
298 2 undef $attr;
299 }
300 elsif ( $attr =~ /^(?:ReadOnly)$/ ) {
301 1 _install_accessors( $OL_PACKAGE, $referent, "readonly", 0 );
302 1 undef $attr;
303 }
304 elsif ($attr =~ /^(?:Class|ClassPriv)$/ ) {
305 11 _install_accessors( $OL_PACKAGE, $referent, "private", 1 );
306 11 undef $attr;
307 }
308 elsif ($attr =~ /^(?:ClassProt)$/ ) {
309 1 _install_accessors( $OL_PACKAGE, $referent, "protected", 1 );
310 1 undef $attr;
311 }
312 elsif ($attr =~ /^(?:ClassPub)$/ ) {
313 1 _install_accessors( $OL_PACKAGE, $referent, "public", 1 );
314 1 undef $attr;
315 }
316 elsif ($attr =~ /^(?:ClassReadOnly)$/ ) {
317 1 _install_accessors( $OL_PACKAGE, $referent, "readonly", 1 );
318 1 undef $attr;
319 }
320 else {
321 # we don't really care
322 }
323 }
324 53 return grep {defined} @attrs;
325 }
326  
327 #--------------------------------------------------------------------------#
328 # _findsym
329 #--------------------------------------------------------------------------#
330  
331 7 my %symcache;
332 125 ▶sub _findsym {
Subroutine _findsym: called CC=6 SCAR=18.1
333 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
334 125 my ($pkg, $ref, $type) = @_;
335 125 ▶ return $symcache{$pkg,$ref} if $symcache{$pkg,$ref};
Branch: 1 branch
BranchTrueFalseTotal
if $symcache{join $;, $pkg, $ref} 0 125 125
336 125 ▶ $type ||= ref($ref);
Condition33%1/3
$type ||= ref $ref
l!l&&r!l&&!r
01250
MC/DC0%0/2
$type ||= ref $ref
$type ref $ref
Truth table33%1/3
A ||= B
A= $typeB= ref $ref
ABresult
1X1
011
000
337 125 my $found;
338 125 foreach my $sym ( values %{$pkg."::"} ) {
339 return $symcache{$pkg,$ref} = \$sym
340 1339 ▶ if *{$sym}{$type} && *{$sym}{$type} == $ref;
Branch: 1 branch
BranchTrueFalseTotal
if *{$sym;}{$type} and *{$sym;}{$type} == $ref 125 1214 1339
Condition100%3/3
*{$sym;}{$type} and *{$sym;}{$type} == $ref
!ll&&!rl&&r
220994125
MC/DC100%2/2
*{$sym;}{$type} and *{$sym;}{$type} == $ref
*{$sym;}{$type} *{$sym;}{$type} == $ref
Truth table100%3/3
A and B
A= *{$sym;}{$type}B= *{$sym;}{$type} == $ref
ABresult
0X0
100
111
341 }
342 }
343  
344 #--------------------------------------------------------------------------#
345 # _gen_accessor
346 #--------------------------------------------------------------------------#
347  
348 38 ▶sub _gen_accessor {
Subroutine _gen_accessor: called CC=2 SCAR=6.9
349 38 my ($package, $name, $classwide) = @_;
350 38 ▶ return $classwide
Branch: 1 branch
BranchTrueFalseTotal
$classwide ? : 3 35 38
351 ? "return \$${package}::CLASSDATA{${name}}"
352 : "return \$${package}::DATA::${name}" .
353 "{refaddr( \$_[0] )}" ;
354 }
355  
356 #--------------------------------------------------------------------------#
357 # _gen_class_locals
358 #--------------------------------------------------------------------------#
359  
360 72 ▶sub _gen_class_locals {
Subroutine _gen_class_locals: called CC=2 SCAR=6.9
361 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
362 72 my $package = shift;
363 72 my $evaltext = "";
364 72 my @props = keys %{$package."::CLASSDATA"};
365 72 ▶ return "" unless @props;
Branch: 1 branch
BranchTrueFalseTotal
unless @props 21 51 72
366 51 my @globs = map { "*${package}::$_" } @props;
367 51 my @refs = map { "\\\$${package}::CLASSDATA{$_}" } @props;
368 51 $evaltext .= " local ( " . join(", ", @globs) . " ) = ( " .
369 join(", ", @refs) . " );\n";
370 51 return $evaltext;
371 }
372  
373 #--------------------------------------------------------------------------#
374 # _gen_acc_mut
375 #--------------------------------------------------------------------------#
376  
377 2 ▶sub _gen_acc_mut {
Subroutine _gen_acc_mut: called CC=2 SCAR=7.2
378 2 my ($package, $name, $classwide) = @_;
379 2 ▶ return $classwide
Branch: 1 branch
BranchTrueFalseTotal
$classwide ? : 0 2 2
380 ? "return (\@_ > 1) ? " .
381 "\$${package}::CLASSDATA{${name}} = \$_[1] : " .
382 "\$${package}::CLASSDATA{${name}} ; " .
383 "\n"
384 : "return (\@_ > 1) ? " .
385 "\$${package}::DATA::${name}" . "{refaddr( \$_[0] )} = \$_[1] : " .
386 "\$${package}::DATA::${name}" . "{refaddr( \$_[0] )} " .
387 "\n";
388 }
389  
390 #--------------------------------------------------------------------------#
391 # _gen_mutator
392 #--------------------------------------------------------------------------#
393  
394 38 ▶sub _gen_mutator {
Subroutine _gen_mutator: called CC=2 SCAR=6.9
395 38 my ($package, $name, $classwide) = @_;
396 38 ▶ return $classwide
Branch: 1 branch
BranchTrueFalseTotal
$classwide ? : 3 35 38
397 ? "\$${package}::CLASSDATA{${name}} = \$_[1];\n" .
398 "return \$_[0] "
399 : "\$${package}::DATA::${name}" .
400 "{refaddr( \$_[0] )} = \$_[1];\n" .
401 "return \$_[0]";
402 }
403  
404 #--------------------------------------------------------------------------#
405 # _gen_object_locals
406 #--------------------------------------------------------------------------#
407  
408 72 ▶sub _gen_object_locals {
Subroutine _gen_object_locals: called CC=2 SCAR=6.9
409 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
410 72 my $package = shift;
411 72 my @props = keys %{$package."::DATA::"};
412 72 ▶ return "" unless @props;
Branch: 1 branch
BranchTrueFalseTotal
unless @props 17 55 72
413 55 my $evaltext = " my \$id;\n"; # need to define it
414 55 $evaltext .= " \$id = refaddr(\$obj) if ref(\$obj);\n";
415 55 my @globs = map { "*${package}::$_" } @props;
416 55 my @refs = map { "\\\$${package}::DATA::$_ {\$id}" } @props;
417 55 $evaltext .= " local ( " . join(", ", @globs) . " ) = ( " .
418 join(", ", @refs) . " ) if \$id;\n";
419 55 return $evaltext;
420 }
421  
422 #--------------------------------------------------------------------------#
423 # _gen_privacy
424 #--------------------------------------------------------------------------#
425  
426 150 ▶sub _gen_privacy {
Subroutine _gen_privacy: called CC=5 SCAR=16.1
427 150 my ($package, $name, $privacy) = @_;
428 150 SWITCH: for ($privacy) {
429 150 ▶ /public/ && do { return "" };
Branch: 1 branch
BranchTrueFalseTotal
/public/ and do { return "" } 141 9 150
430  
431 9 ▶ /protected/ && do { return
Branch: 1 branch
BranchTrueFalseTotal
/protected/ and do { return " my (\$caller) = caller();\n" . " croak q/$name is a protected method and can't be called from $package/\n" . " unless \$caller->isa( '${package}' );\n" } 7 2 9
432 7 " my (\$caller) = caller();\n" .
433 " croak q/$name is a protected method and can't be called from ${package}/\n".
434 " unless \$caller->isa( '$package' );\n"
435 };
436  
437 2 ▶ /private/ && do { return
Branch: 1 branch
BranchTrueFalseTotal
/private/ and do { return " my (\$caller) = caller();\n" . " croak q/$name is a private method and can't be called from $package/\n" . " unless \$caller eq '${package}';\n" } 2 0 2
438 2 " my (\$caller) = caller();\n" .
439 " croak q/$name is a private method and can't be called from ${package}/\n".
440 " unless \$caller eq '$package';\n"
441 };
442 }
443 }
444  
445 #--------------------------------------------------------------------------#
446 # _install_accessors
447 #--------------------------------------------------------------------------#
448  
449 53 ▶sub _install_accessors {
Subroutine _install_accessors: called CC=10 SCAR=23.0
450 53 my ($package,$scalarref,$privacy,$classwide) = @_;
451 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
452  
453 # find name from reference
454 53 ▶ my $symbol = _findsym($package, $scalarref) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $symbol = _findsym($package, $scalarref) or die 53 0 53
455 53 my $name = *{$symbol}{NAME};
456  
457 # make the property exist to be found by give_methods()
458 53 ▶ if ($classwide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classwide) { } 14 39 53
459 14 ${$package."::CLASSDATA"}{$name} = undef;
460 }
461 else {
462 39 %{$package."::DATA::".$name} = ();
463 }
464  
465 # determine names for accessor/mutator
466 53 my $get = $prefixes_for{ $package }{get};
467 53 my $set = $prefixes_for{ $package }{set};
468 53 ▶ my $acc = ( defined $get ? $get : q{} ) . $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $get ? : 6 47 53
469 53 ▶ my $mut = ( defined $set ? $set : q{set_} ) . $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $set ? : 6 47 53
470  
471 # install accessors
472 53 ▶ return if $privacy eq "private"; # unless private
Branch: 1 branch
BranchTrueFalseTotal
if $privacy eq "private" 13 40 53
473 40 ▶ my $accessor_privacy = $privacy eq 'readonly' ? 'public' : $privacy;
Branch: 1 branch
BranchTrueFalseTotal
$privacy eq 'readonly' ? : 2 38 40
474 40 ▶ my $mutator_privacy = $privacy eq 'readonly' ? 'protected' : $privacy;
Branch: 1 branch
BranchTrueFalseTotal
$privacy eq 'readonly' ? : 2 38 40
475 40 my $evaltext;
476 40 ▶ if ( $acc ne $mut ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acc ne $mut) { } 38 2 40
477 38 $evaltext =
478 "*${package}::${acc} = sub { \n" .
479 _gen_privacy( $package, $name, $accessor_privacy ) .
480 _gen_accessor( $package, $name, $classwide ) .
481 "\n}; \n\n" .
482 "*${package}::${mut} = sub { \n" .
483 _gen_privacy( $package, "set_$name", $mutator_privacy ) .
484 _gen_mutator( $package, $name, $classwide ) .
485 "\n}; "
486 ; # $evaltext
487 }
488 else {
489 2 $evaltext =
490 "*${package}::${mut} = sub { \n" .
491 _gen_privacy( $package, "set_$name", $mutator_privacy ) .
492 _gen_acc_mut( $package, $name, $classwide ) .
493 "\n}; "
494 ; # $evaltext
495 }
496
497 40 ▶ eval $evaltext; ## no critic
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
498 40 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 40 40
499 40 return;
500 }
501  
502 #--------------------------------------------------------------------------#
503 # _install_wrapper
504 #--------------------------------------------------------------------------#
505  
506 72 ▶sub _install_wrapper {
Subroutine _install_wrapper: called CC=3 SCAR=11.0
507 72 my ($package,$coderef,$privacy) = @_;
508 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
509 20 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
510 72 ▶ my $symbol = _findsym($package, $coderef) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $symbol = _findsym($package, $coderef) or die 72 0 72
511 72 my $name = *{$symbol}{NAME};
512 72 *{$package."::METHODS::$name"} = $coderef;
513 72 my $evaltext = "*${package}::${name} = sub {\n".
514 _gen_privacy( $package, $name, $privacy ) .
515 " my \$obj = shift;\n" .
516 " local \$${package}::self = \$obj;\n" .
517 _gen_class_locals($package) .
518 _gen_object_locals($package) .
519 " local \$Carp::CarpLevel = \$Carp::CarpLevel + 2;\n".
520 " ${package}::METHODS::${name}(\@_);\n".
521 "}\n"
522 ; # my
523 # XXX print "\n\n$evaltext\n\n";
524 72 ▶ eval $evaltext; ## no critic
Branch: 2 branches
BranchTrueFalseTotal
if ref $obj 36 5 41
if $id 34 1 35
525 72 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 72 72
526 72 return;
527 }
528  
529 7 1;
530  
531 __END__
↑ Summary