Class/HideCaller.pm

filter: Statementstmt 77.6% 52 / 67 Branchbran 52.7% 19 / 36 Conditioncond 52.0% 13 / 25 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 65.1% 101 / 155 CC 28 SCAR 35.0
CC
28
Cov
66%
__ANON__
110.0
__ANON__
10.0
END
6.6
CRAP 33.2
« Class.pm ↑ Summary Class/Attributes/OOStandard/InheritationCheck.pm »
1 package Fukurama::Class::HideCaller;
2 4 ▶use Fukurama::Class::Version(0.01);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Fukurama::Class::Rigid;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Fukurama::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 my $IS_DECORATED = undef;
7 4 our $REGISTER = {};
8 4 our $DISABLE;
9 4 my $USAGE_ERROR;
10  
11 =head1 NAME
12  
13 Fukurama::Class::HideCaller - Pragma to hide wrapper-classes in callers stack
14  
15 =head1 VERSION
16  
17 Version 0.01 (beta)
18  
19 =head1 SYNOPSIS
20  
21 package MyWrapperClass;
22 use Fukurama::Class::HideCaller('MyWrapperClass');
23
24 sub wrap_around_test {
25 my $sub = \&MyClass::test;
26 no warnings;
27 *MyClass::test = sub {
28 print "before, ";
29 &{$sub}(@_);
30 print "after";
31 }
32 }
33
34 package MyClass;
35 sub test {
36 no warnings;
37 print "middle, caller: " . [caller(0)]->[0] . ", ";
38 }
39
40 package main;
41 MyWrapperClass->wrap_around_test();
42 MyClass->test();
43 # will print: before, middle, caller: main, after
44 # without the HideCaller, it will print: before, middle, caller: MyWrapper, after
45  
46 =head1 DESCRIPTION
47  
48 This pragma-like module provides functions to hide a wrapper-class in callers stack. It's a helper class
49 to provide parameter and return value checking without changings in any caller stack.
50  
51 =head1 CONFIG
52  
53 You can disable the whole behavior of this class by setting
54  
55 $Fukurama::Class::HideCaller::DISABLE = 1;
56
57 =head1 EXPORT
58  
59 =over 4
60  
61 =item CORE::GLOBAL::caller
62  
63 would be decorated
64  
65 =back
66  
67 =head1 METHODS
68  
69 =over 4
70  
71 =item register_class( hidden_wrapper_class:STRING ) return:VOID
72  
73 Register a wrapper class to competely hide in caller stack.
74  
75 =back
76  
77 =head1 AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE
78  
79 see perldoc of L<Fukurama::Class>
80  
81 =cut
82  
83 # AUTOMAGIC void
84 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.2
85 4 my $class = $_[0];
86 4 my $hidden_class = $_[1];
87
88 4 ▶ if(!$IS_DECORATED) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IS_DECORATED) 4 0 4
89 4 $class->_decorate_caller();
90 4 $IS_DECORATED = 1;
91 }
92 4 ▶ $class->register_class($hidden_class) if(defined($hidden_class));
Branch: 1 branch
BranchTrueFalseTotal
if defined $hidden_class 0 4 4
93 4 return;
94 }
95 # void
96 5 ▶sub register_class {
Subroutine register_class: called CC=4 SCAR=14.1
Pod documented
97 5 my $class = $_[0];
98 5 my $hidden_class = $_[1];
99
100 5 ▶ if(!$IS_DECORATED && !$USAGE_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IS_DECORATED or $USAGE_ERROR) 1 4 5
Condition66%2/3
$IS_DECORATED or $USAGE_ERROR
l!l&&r!l&&!r
401
MC/DC50%1/2
$IS_DECORATED or $USAGE_ERROR
$IS_DECORATED $USAGE_ERROR
Truth table66%2/3
A or B
A= $IS_DECORATEDB= $USAGE_ERROR
ABresult
1X1
011
000
101 1 $USAGE_ERROR = 1;
102 1 _croak("Wrong usage: you have to say\n\t'use " . __PACKAGE__ . ";' or\n\t'use " . __PACKAGE__ . "('CLASSNAME')'");
103 }
104 4 ▶ if(!UNIVERSAL::isa($hidden_class, $hidden_class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($hidden_class, $hidden_class)) 0 4 4
105 0 _croak("Class '$hidden_class' is not a valid class");
106 }
107 4 $REGISTER->{$hidden_class} = 1;
108 4 return;
109 }
110 # AUTOMAGIC void
111 4 ▶END {
Subroutine END: called CC=4 SCAR=18.9
112
113 4 ▶ if(!$DISABLE && !$IS_DECORATED && !$USAGE_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Fukurama::Class::HideCaller::DISABLE || $IS_DECORATED || $USAGE_ERROR) 0 4 4
Condition50%3/6
$Fukurama::Class::HideCaller::DISABLE || $IS_DECORATED || $USAGE_ERROR
l!l&&r!l&&!r
400
$Fukurama::Class::HideCaller::DISABLE or $IS_DECORATED
l!l&&r!l&&!r
130
MC/DC0%0/3
$Fukurama::Class::HideCaller::DISABLE || $IS_DECORATED || $USAGE_ERROR
$Fukurama::Class::HideCaller::DISABLE $IS_DECORATED $USAGE_ERROR
Truth table50%2/4
A or B || C
A= $Fukurama::Class::HideCaller::DISABLEB= $IS_DECORATEDC= $USAGE_ERROR
ABCresult
1XX1
01X1
0011
0000
114 0 $USAGE_ERROR = 1;
115 0 _croak("Wrong usage: you have to say\n\t'use " . __PACKAGE__ . ";' or\n\t'use " . __PACKAGE__ . "('CLASSNAME')'");
116 }
117 }
118 # void
119 4 ▶sub _decorate_caller {
Subroutine _decorate_caller: called CC=2 SCAR=8.4
120 4 my $class = $_[0];
121
122 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
123
124 4 my $old = *CORE::GLOBAL::caller{'CODE'};
125 4 ▶ if($old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) { } 0 4 4
126
127 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
128
129 # inspired by Hook::LexWrap code
130 0 ▶ *CORE::GLOBAL::caller = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
131 0 ▶ my $level = $_[0] || 0;
Condition0%0/2
$_[0] || 0
l!l
00
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
132 0 my $i = 1;
133 0 my $called_sub = undef;
134 0 while(1) {
135 0 ▶ my @caller = &$old($i++) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@caller) = &$old($i++) or return 0 0 0
136 0 ▶ $caller[3] = $called_sub if($called_sub);
Branch: 1 branch
BranchTrueFalseTotal
if $called_sub 0 0 0
137 0 ▶ $called_sub = ((${__PACKAGE__ . '::REGISTER'}->{$caller[0]} && !${__PACKAGE__ . '::DISABLE'}) ? $caller[3] : undef);
Branch: 1 branch
BranchTrueFalseTotal
$${'Fukurama::Class::HideCaller::REGISTER';}{$caller[0]} && !${'Fukurama::Class::HideCaller::DISABLE';} ? : 0 0 0
Condition0%0/3
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} and not ${"Fukurama::Class::HideCaller::DISABLE";}
!ll&&!rl&&r
000
MC/DC0%0/2
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} and not ${"Fukurama::Class::HideCaller::DISABLE";}
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} not ${"Fukurama::Class::HideCaller::DISABLE";}
Truth table0%0/3
A and not(B)
A= $${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]}B= not ${"Fukurama::Class::HideCaller::DISABLE";}
ABresult
0X0
100
111
138 0 ▶ next if($called_sub || $level-- != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $called_sub or $level-- != 0 0 0 0
Condition0%0/3
$called_sub or $level-- != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$called_sub or $level-- != 0
$called_sub $level-- != 0
Truth table0%0/3
A or B
A= $called_subB= $level-- != 0
ABresult
1X1
011
000
139 0 ▶ return (wantarray ? (@_ ? @caller : @caller[0..2]) : $caller[0]);
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
@_ ? : 0 0 0
140 }
141 0 };
142 } else {
143 # inspired by Hook::LexWrap code
144 111 ▶ *CORE::GLOBAL::caller = sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
145 111 ▶ my $level = $_[0] || 0;
Condition100%2/2
$_[0] || 0
l!l
8526
MC/DC100%1/1
$_[0] || 0
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
146 111 my $i = 1;
147 111 my $called_sub = undef;
148 111 while(1) {
149 347 ▶ my @caller = CORE::caller($i++) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@caller) = caller $i++ or return 341 6 347
150 341 ▶ $caller[3] = $called_sub if($called_sub);
Branch: 1 branch
BranchTrueFalseTotal
if $called_sub 12 329 341
151 341 ▶ $called_sub = ((${__PACKAGE__ . '::REGISTER'}->{$caller[0]} && !${__PACKAGE__ . '::DISABLE'}) ? $caller[3] : undef);
Branch: 1 branch
BranchTrueFalseTotal
$${'Fukurama::Class::HideCaller::REGISTER';}{$caller[0]} && !${'Fukurama::Class::HideCaller::DISABLE';} ? : 12 329 341
Condition100%3/3
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} and not ${"Fukurama::Class::HideCaller::DISABLE";}
!ll&&!rl&&r
325412
MC/DC100%2/2
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} and not ${"Fukurama::Class::HideCaller::DISABLE";}
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} not ${"Fukurama::Class::HideCaller::DISABLE";}
Truth table100%3/3
A and not(B)
A= $${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]}B= not ${"Fukurama::Class::HideCaller::DISABLE";}
ABresult
0X0
100
111
152 341 ▶ next if($called_sub || $level-- != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $called_sub or $level-- != 0 236 105 341
Condition100%3/3
$called_sub or $level-- != 0
l!l&&r!l&&!r
12224105
MC/DC100%2/2
$called_sub or $level-- != 0
$called_sub $level-- != 0
Truth table100%3/3
A or B
A= $called_subB= $level-- != 0
ABresult
1X1
011
000
153 105 ▶ return (wantarray ? (@_ ? @caller : @caller[0..2]) : $caller[0]);
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 23 82 105
@_ ? : 14 9 23
154 }
155 4 };
156 }
157 4 return;
158 }
159 4 1;
« Class.pm ↑ Summary Class/Attributes/OOStandard/InheritationCheck.pm »