blib/lib/Devel/EnforceEncapsulation.pm

filter: Statementstmt 100% 72 / 72 Branchbran 100% 10 / 10 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 100% 107 / 107 CC 8 SCAR 7.3
CC
8
Cov
100%
apply_to
5.0
remove_from
2.0
_deref_overload_croak
2.0
CRAP 2.1
↑ Summary
1 #######################################################################
2 # $URL: svn+ssh://equilibrious@equilibrious.net/home/equilibrious/Mirror/medialandscape/repos/clotho/Devel-EnforceEncapsulation/lib/Devel/EnforceEncapsulation.pm $
3 # $Date: 2014-03-27 18:57:41 -0500 (Thu, 27 Mar 2014) $
4 # $Author: equilibrious $
5 # $Revision: 3356 $
6 ########################################################################
7  
8 package Devel::EnforceEncapsulation;
9  
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '0.51';
17  
18 7 ▶sub apply_to {
Subroutine apply_to: called CC=5 SCAR=16.1
Pod documented
19 7 my $pkg = shift;
20 7 my $dest_pkg = shift;
21 7 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
25
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
22  
23 7 ▶ $opts->{policy} ||= 'croak';
Condition100%2/2
$opts->{"policy"} ||= "croak"
l!l
25
MC/DC100%1/1
$opts->{"policy"} ||= "croak"
$opts->{"policy"}
Truth table100%2/2
A
A= $opts->{"policy"}
Aresult
11
00
24 7 ▶ if (!eval { $pkg->can('_deref_overload_' . $opts->{policy}) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pkg->can('_deref_overload_' . $opts->{"policy"}) } }) 1 6 7
25 1 croak "Unknown encapsulation policy '$opts->{policy}'";
26 }
27  
28 ## no critic(ProhibitStringyEval,RequireCarping)
29 6 my $fn = __PACKAGE__ . '::_deref_overload_' . $opts->{policy};
30 6 my $overloads = join q{,}, map { "'$_' => \\&$fn" } $pkg->_ops;
31 1 ▶ eval "{package $dest_pkg; use overload $overloads, fallback => 1;}";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶ die $EVAL_ERROR if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::EnforceEncapsulation::EVAL_ERROR 1 5 6
33 5 return;
34 }
35  
36 3 ▶sub remove_from {
Subroutine remove_from: called CC=2 SCAR=6.9
Pod documented
37 3 my $pkg = shift;
38 3 my $dest_pkg = shift;
39  
40 ## no critic(ProhibitStringyEval,RequireCarping)
41 3 my $overloads = join q{,}, map { "'$_'" } $pkg->_ops;
42 1 ▶ eval "{package $dest_pkg; no overload $overloads, 'fallback';}";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶ die $EVAL_ERROR if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::EnforceEncapsulation::EVAL_ERROR 1 2 3
44 2 return;
45 }
46  
47 ## possible callbacks to be installed via overload ##
48  
49 53 ▶sub _deref_overload_croak {
Subroutine _deref_overload_croak: called CC=2 SCAR=6.9
50 53 my $self = shift;
51  
52 53 my $caller_pkg = caller;
53 53 ▶ if (!$self->isa($caller_pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->isa($caller_pkg)) 16 37 53
54 16 my $pkg = ref $self;
55 16 croak "Illegal attempt to access $pkg internals from $caller_pkg";
56 }
57 37 return $self;
58 }
59  
60 3 ▶sub _deref_overload_carp {
Subroutine _deref_overload_carp: called CC=2 SCAR=6.9
61 3 my $self = shift;
62  
63 3 my $caller_pkg = caller;
64 3 ▶ if (!$self->isa($caller_pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->isa($caller_pkg)) 1 2 3
65 1 my $pkg = ref $self;
66 1 carp "Illegal attempt to access $pkg internals from $caller_pkg";
67 }
68 3 return $self;
69 }
70  
71 # get a list of overloadable derefs ('%{}', '@{}', '${}', ...)
72 9 ▶sub _ops {
Subroutine _ops: called CC=1 SCAR=0.0
73 9 my $pkg = shift;
74  
75 ## no critic(ProhibitPackageVars)
76 9 return split m/\s/xms, $overload::ops{dereferencing};
77 }
78  
79 1 1;
80  
81 __END__
↑ Summary