| 1 | package Fukurama::Class::Abstract; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use Fukurama::Class::Version(0.01); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Fukurama::Class::Rigid; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Fukurama::Class::Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Fukurama::Class::Tree(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | my $CLASS = {}; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | my $DECORATED_SUBS = {}; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | our $DISABLE = 0; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | Fukurama::Class::Abstract - Pragma to provide abstract classes | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | Version 0.01 (beta) | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | package MyClass; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | use Fukurama::Class::Abstract; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | This pragma-like module provides functions to check the usage of all class-methods. All calls from childs, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | which inherits from this class are ok, all other will croak at runtime. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | Use Fukurama::Class instead, to get all the features for OO. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 CONFIG | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | You can disable the whole behavior of this class by setting | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | $Fukurama::Class::Abstract::DISABLE = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | =head1 EXPORT | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | All methods of your abstract class would be decorated with a caller-check method. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =item abstract( abstract_class:STRING ) return:VOID | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | Set the given class as abstract. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =item run_check( ) return:VOID | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | Helper method for static perl (see Fukurama::Class > BUGS) | ||||||||||||||||||||||||||||||||||||||||||
| 51 | This method decorates all non-special subroutines in the registered, abstract classes | ||||||||||||||||||||||||||||||||||||||||||
| 52 | that all calls would be checked. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =head1 AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | see perldoc of L<Fukurama::Class> | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # AUTOMAGIC void | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | my $class = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my ($caller_class) = caller(0); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | $class->abstract($caller_class); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # STATIC void | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub abstract { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abstract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | my $class = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | my $caller_class = $_[1]; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | $CLASS->{$caller_class} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # STATIC void | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | sub run_check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_check: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | my $class = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | return if($DISABLE); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | foreach my $obj_class (keys(%$CLASS)) { | |||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | foreach my $identifier (@{$class->_get_subs($obj_class)}) { | |||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | $class->_decorate_sub($obj_class, $identifier); | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # STATIC void | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | ▶ | sub _decorate_sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decorate_sub: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | my $class = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | my $obj_class = $_[1]; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | my $identifier = $_[2]; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | return if(exists($DECORATED_SUBS->{$identifier})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | my ($subname) = $identifier =~ m/([^:]+)$/; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | return if(Fukurama::Class::Tree->is_special_sub($subname)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | my $old = *{$identifier}{CODE}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | *{$identifier} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | ▶ | my $used_obj = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $_[0] || $_[0]
MC/DC50%1/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 8 | ▶ | if(!$used_obj || $used_obj eq $obj_class || !UNIVERSAL::isa($used_obj, $obj_class)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $used_obj or $used_obj eq $obj_class or not UNIVERSAL::isa($used_obj, $obj_class)
not $used_obj or $used_obj eq $obj_class
MC/DC66%2/3
not $used_obj or $used_obj eq $obj_class or not UNIVERSAL::isa($used_obj, $obj_class)
not $used_obj
$used_obj eq $obj_class
not UNIVERSAL::isa($used_obj, $obj_class)
Truth table75%3/4
not(A) or B or not(C)
A= not $used_objB= $used_obj eq $obj_classC= not UNIVERSAL::isa($used_obj, $obj_class)
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | $class->_throw_error($used_obj, $obj_class, $identifier); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | goto $old; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | $DECORATED_SUBS->{$identifier} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # STATIC void | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | ▶ | sub _throw_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_throw_error: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | my $class = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | my $obj_class = $_[1]; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | my $caller_class = $_[2]; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | my $identifier = $_[3]; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | ▶ | $obj_class = '' if(!defined($obj_class)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | _croak("Abstract class '$obj_class' used in class '$caller_class'. Sub '$identifier' called.", 2); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # STATIC array | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | sub _get_subs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_subs: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | my $class = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | my $obj_class = $_[1]; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | my $subs = []; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | foreach my $name (%{$obj_class . '::'}) { | |||||||||||||||||||||||||||||||||||||||||
| 136 | 12 | my $identifier = $obj_class . '::' . $name; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 12 | ▶ | next if(!*{$identifier}{'CODE'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | push(@$subs, $identifier); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | return $subs; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 3 | ▶ | no warnings 'void'; # avoid 'Too late to run CHECK/INIT block' | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # AUTOMAGIC void | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | ▶ | CHECK { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CHECK: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 3 | __PACKAGE__->run_check(); | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||