blib/lib/Class/Inner.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 50.0% 4 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 87.0% 74 / 85 CC 9 SCAR 16.1
CC
9
Cov
88%
new
9.4
BEGIN
1.0
BEGIN
1.0
CRAP 5.0
↑ Summary
1 package Class::Inner;
2  
3 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 $VERSION = 0.200_001;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Class::Inner - A perlish implementation of Java like inner classes
13  
14 =head1 SYNOPSIS
15  
16 use Class::Inner;
17  
18 my $object = Class::Inner->new(
19 parent => 'ParentClass',
20 methods => { method => sub { ... } }, },
21 constructor => 'new',
22 args => [@constructor_args],
23 );
24  
25 =head1 DESCRIPTION
26  
27 Yet another implementation of an anonymous class with per object
28 overrideable methods, but with the added attraction of sort of working
29 dispatch to the parent class's method.
30  
31 =head2 METHODS
32  
33 =over 4
34  
35 =item B<new HASH>
36  
37 Takes a hash like argument list with the following keys.
38  
39 =over 4
40  
41 =item B<parent>
42  
43 The name of the parent class. Note that you can only get single
44 inheritance with this or B<SUPER> won't work.
45  
46 =item B<methods>
47  
48 A hash, keys are method names, values are CODEREFs.
49  
50 =item B<constructor>
51  
52 The name of the constructor method. Defaults to 'new'.
53  
54 =item B<args>
55  
56 An anonymous array of arguments to pass to the constructor. Defaults
57 to an empty list.
58  
59 =back
60  
61 Returns an object in an 'anonymous' class which inherits from the
62 parent class. This anonymous class has a couple of 'extra' methods:
63  
64 =over 4
65  
66 =item B<SUPER>
67  
68 If you were to pass something like
69  
70 $obj = Class::Inner->new(
71 parent => 'Parent',
72 methods => { method => sub { ...; $self->SUPER::method(@_) } },
73 );
74  
75 then C<$self-C<gt>SUPER::method> almost certainly wouldn't do what you expect,
76 so we provide the C<SUPER> method which dispatches to the parent
77 implementation of the current method. There seems to be no good way of
78 getting the full C<SUPER::> functionality, but I'm working on it.
79  
80 =item B<DESTROY>
81  
82 Because B<Class::Inner> works by creating a whole new class name for your
83 object, it could potentially leak memory if you create a lot of them. So we
84 add a C<DESTROY> method that removes the class from the symbol table once
85 it's finished with.
86  
87 If you need to override a parent's DESTROY method, adding a call to
88 C<Class::Inner::clean_symbol_table(ref $self)> to it. Do it at the
89 end of the method or your other method calls won't work.
90  
91 =back
92  
93 =cut
94  
95 #'
96  
97 1 ▶sub new {
Subroutine new: called CC=9 SCAR=22.4
Pod documented
98 1 my $class = shift;
99 0 ▶ my %args = ref($_[0]) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
100 my $parent = $args{parent} or
101 1 ▶ croak "Can't work without a parent class\n";
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $args{"parent"} or croak("Can't work without a parent class\n") 1 0 1
102 1 ▶ my %methods = %{$args{methods}||{}};
Condition50%1/2
$args{'methods'} || {}
l!l
10
MC/DC0%0/1
$args{'methods'} || {}
$args{'methods'}
Truth table50%1/2
A
A= $args{'methods'}
Aresult
11
00
103 1 ▶ my $constructor = $args{constructor} || 'new';
Condition50%1/2
$args{'constructor'} || 'new'
l!l
01
MC/DC0%0/1
$args{'constructor'} || 'new'
$args{'constructor'}
Truth table50%1/2
A
A= $args{'constructor'}
Aresult
11
00
104 1 ▶ my @constructor_args = @{$args{args} || []};
Condition50%1/2
$args{'args'} || []
l!l
10
MC/DC0%0/1
$args{'args'} || []
$args{'args'}
Truth table50%1/2
A
A= $args{'args'}
Aresult
11
00
105  
106 1 my $anon_class = $class->new_classname;
107  
108 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 1 @{"$anon_class\::ISA"} = $parent;
111  
112 1 foreach my $methodname (keys %methods) {
113 2 ▶ *{"$anon_class\::$methodname"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
114 3 local $Class::Inner::target_method = $methodname;
115 3 $methods{$methodname}->(@_);
116 2 };
117 }
118  
119 # Add the SUPER method.
120  
121 1 ▶ unless (exists $methods{SUPER}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $methods{"SUPER"}) 1 0 1
122 1 ▶ *{"$anon_class\::SUPER"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 2 my $self = shift;
124 2 my $target_method =
125 join '::', $parent, $Class::Inner::target_method;
126 2 $self->$target_method(@_);
127 1 };
128 }
129  
130 1 ▶ unless (exists $methods{DESTROY}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $methods{"DESTROY"}) 1 0 1
131 1 ▶ *{"$anon_class\::DESTROY"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 1 my $self = shift;
133 1 Class::Inner::clean_symbol_table($anon_class);
134 1 bless $self, $parent;
135 }
136 1 }
137 # Instantiate
138 1 my $obj = $anon_class->new(@constructor_args);
139 }
140  
141 =item B<clean_symbol_table>
142  
143 The helper subroutine that DESTROY uses to remove the class from the
144 symbol table.
145  
146 =cut
147  
148 1 ▶sub clean_symbol_table {
Subroutine clean_symbol_table: called CC=1 SCAR=0.0
Pod documented
149 1 my $class = shift;
150 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 undef %{"${class}::"};
152 }
153  
154 =item B<new_classname>
155  
156 Returns a name for the next anonymous class.
157  
158 =cut
159  
160 {
161 1 my $class_counter;
162  
163 1 ▶ sub new_classname {
Subroutine new_classname: called CC=1 SCAR=0.0
Pod documented
164 1 my $baseclass = shift;
165 1 return "$baseclass\::__A" . $class_counter++;
166 }
167 }
168  
169 1 1;
170 __END__
↑ Summary