line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Inner; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23723
|
use vars qw/$VERSION/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
80
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
$VERSION = 0.200_001; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
8
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
235
|
|
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 |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Takes a hash like argument list with the following keys. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=over 4 |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=item B |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
The name of the parent class. Note that you can only get single |
44
|
|
|
|
|
|
|
inheritance with this or B won't work. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item B |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
A hash, keys are method names, values are CODEREFs. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item B |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
The name of the constructor method. Defaults to 'new'. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item B |
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 |
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-CSUPER::method> almost certainly wouldn't do what you expect, |
76
|
|
|
|
|
|
|
so we provide the C 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 functionality, but I'm working on it. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item B |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Because B 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 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 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
|
|
|
|
|
|
|
sub new { |
98
|
1
|
|
|
1
|
1
|
1611
|
my $class = shift; |
99
|
1
|
50
|
|
|
|
6
|
my %args = ref($_[0]) ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
100
|
1
|
50
|
|
|
|
3
|
my $parent = $args{parent} or |
101
|
|
|
|
|
|
|
croak "Can't work without a parent class\n"; |
102
|
1
|
50
|
|
|
|
2
|
my %methods = %{$args{methods}||{}}; |
|
1
|
|
|
|
|
8
|
|
103
|
1
|
|
50
|
|
|
6
|
my $constructor = $args{constructor} || 'new'; |
104
|
1
|
50
|
|
|
|
2
|
my @constructor_args = @{$args{args} || []}; |
|
1
|
|
|
|
|
4
|
|
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
5
|
my $anon_class = $class->new_classname; |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
316
|
|
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
2
|
@{"$anon_class\::ISA"} = $parent; |
|
1
|
|
|
|
|
19
|
|
111
|
|
|
|
|
|
|
|
112
|
1
|
|
|
|
|
3
|
foreach my $methodname (keys %methods) { |
113
|
2
|
|
|
|
|
17
|
*{"$anon_class\::$methodname"} = sub { |
114
|
3
|
|
|
3
|
|
3622
|
local $Class::Inner::target_method = $methodname; |
115
|
3
|
|
|
|
|
9
|
$methods{$methodname}->(@_); |
116
|
2
|
|
|
|
|
5
|
}; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Add the SUPER method. |
120
|
|
|
|
|
|
|
|
121
|
1
|
50
|
|
|
|
9
|
unless (exists $methods{SUPER}) { |
122
|
1
|
|
|
|
|
4
|
*{"$anon_class\::SUPER"} = sub { |
123
|
2
|
|
|
2
|
|
8
|
my $self = shift; |
124
|
2
|
|
|
|
|
5
|
my $target_method = |
125
|
|
|
|
|
|
|
join '::', $parent, $Class::Inner::target_method; |
126
|
2
|
|
|
|
|
7
|
$self->$target_method(@_); |
127
|
1
|
|
|
|
|
4
|
}; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
1
|
50
|
|
|
|
4
|
unless (exists $methods{DESTROY}) { |
131
|
1
|
|
|
|
|
4
|
*{"$anon_class\::DESTROY"} = sub { |
132
|
1
|
|
|
1
|
|
355
|
my $self = shift; |
133
|
1
|
|
|
|
|
5
|
Class::Inner::clean_symbol_table($anon_class); |
134
|
1
|
|
|
|
|
12
|
bless $self, $parent; |
135
|
|
|
|
|
|
|
} |
136
|
1
|
|
|
|
|
3
|
} |
137
|
|
|
|
|
|
|
# Instantiate |
138
|
1
|
|
|
|
|
7
|
my $obj = $anon_class->new(@constructor_args); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item B |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
The helper subroutine that DESTROY uses to remove the class from the |
144
|
|
|
|
|
|
|
symbol table. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub clean_symbol_table { |
149
|
1
|
|
|
1
|
1
|
2
|
my $class = shift; |
150
|
1
|
|
|
1
|
|
6
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
114
|
|
151
|
1
|
|
|
|
|
2
|
undef %{"${class}::"}; |
|
1
|
|
|
|
|
24
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item B |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Returns a name for the next anonymous class. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
{ |
161
|
|
|
|
|
|
|
my $class_counter; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub new_classname { |
164
|
1
|
|
|
1
|
1
|
1
|
my $baseclass = shift; |
165
|
1
|
|
|
|
|
4
|
return "$baseclass\::__A" . $class_counter++; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
1; |
170
|
|
|
|
|
|
|
__END__ |