| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$IS_DECORATED or $USAGE_ERROR
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
$Fukurama::Class::HideCaller::DISABLE || $IS_DECORATED || $USAGE_ERROR
$Fukurama::Class::HideCaller::DISABLE or $IS_DECORATED
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table0%0/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $caller[3] = $called_sub if($called_sub); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $called_sub = ((${__PACKAGE__ . '::REGISTER'}->{$caller[0]} && !${__PACKAGE__ . '::DISABLE'}) ? $caller[3] : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} and not ${"Fukurama::Class::HideCaller::DISABLE";}
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";}
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | next if($called_sub || $level-- != 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$called_sub or $level-- != 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | return (wantarray ? (@_ ? @caller : @caller[0..2]) : $caller[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$_[0] || 0
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 341 | ▶ | $caller[3] = $called_sub if($called_sub); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 341 | ▶ | $called_sub = ((${__PACKAGE__ . '::REGISTER'}->{$caller[0]} && !${__PACKAGE__ . '::DISABLE'}) ? $caller[3] : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$${"Fukurama::Class::HideCaller::REGISTER";}{$caller[0]} and not ${"Fukurama::Class::HideCaller::DISABLE";}
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";}
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 341 | ▶ | next if($called_sub || $level-- != 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$called_sub or $level-- != 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 105 | ▶ | return (wantarray ? (@_ ? @caller : @caller[0..2]) : $caller[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||