line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::C3::Componentised; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Class::C3::Componentised |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 DESCRIPTION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Load mix-ins or components to your C3-based class. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package MyModule; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use strict; |
16
|
|
|
|
|
|
|
use warnings; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use base 'Class::C3::Componentised'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub component_base_class { "MyModule::Component" } |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
package main; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
MyModule->load_components( qw/Foo Bar/ ); |
25
|
|
|
|
|
|
|
# Will load MyModule::Component::Foo and MyModule::Component::Bar |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 DESCRIPTION |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
This will inject base classes to your module using the L method |
30
|
|
|
|
|
|
|
resolution order. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Please note: these are not plugins that can take precedence over methods |
33
|
|
|
|
|
|
|
declared in MyModule. If you want something like that, consider |
34
|
|
|
|
|
|
|
L. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 METHODS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
7
|
|
|
7
|
|
49159
|
use strict; |
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
860
|
|
41
|
7
|
|
|
7
|
|
52
|
use warnings; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
248
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# This will prime the Class::C3 namespace (either by loading it proper on 5.8 |
44
|
|
|
|
|
|
|
# or by installing compat shims on 5.10+). A user might have a reasonable |
45
|
|
|
|
|
|
|
# expectation that using Class::C3:: will give him access to |
46
|
|
|
|
|
|
|
# Class::C3 itself, and this module has been providing this historically. |
47
|
|
|
|
|
|
|
# Therefore leaving it in indefinitely. |
48
|
7
|
|
|
7
|
|
8781
|
use MRO::Compat; |
|
7
|
|
|
|
|
34597
|
|
|
7
|
|
|
|
|
329
|
|
49
|
|
|
|
|
|
|
|
50
|
7
|
|
|
7
|
|
63
|
use Carp (); |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
110
|
|
51
|
7
|
|
|
7
|
|
38
|
use List::Util (); |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
3351
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
our $VERSION = '1.001000'; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $invalid_class = qr/(?: \b:\b | \:{3,} | \:\:$ )/x; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 load_components( @comps ) |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Loads the given components into the current module. If a module begins with a |
62
|
|
|
|
|
|
|
C<+> character, it is taken to be a fully qualified class name, otherwise |
63
|
|
|
|
|
|
|
C<< $class->component_base_class >> is prepended to it. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Calling this will call C. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub load_components { |
70
|
10
|
|
|
10
|
1
|
16776
|
my $class = shift; |
71
|
10
|
100
|
|
|
|
99
|
$class->_load_components( map { |
72
|
10
|
|
|
|
|
40
|
/^\+(.*)$/ |
73
|
|
|
|
|
|
|
? $1 |
74
|
|
|
|
|
|
|
: join ('::', $class->component_base_class, $_) |
75
|
10
|
|
|
|
|
34
|
} grep { $_ !~ /^#/ } @_ |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 load_own_components( @comps ) |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Similar to L, but assumes every class is C<"$class::$comp">. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub load_own_components { |
86
|
0
|
|
|
0
|
1
|
0
|
my $class = shift; |
87
|
0
|
|
|
|
|
0
|
$class->_load_components( map { "${class}::$_" } grep { $_ !~ /^#/ } @_ ); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _load_components { |
91
|
10
|
|
|
10
|
|
76
|
my $class = shift; |
92
|
10
|
100
|
|
|
|
38
|
return unless @_; |
93
|
|
|
|
|
|
|
|
94
|
9
|
|
|
|
|
60
|
$class->ensure_class_loaded($_) for @_; |
95
|
7
|
|
|
|
|
57
|
$class->inject_base($class => @_); |
96
|
7
|
|
|
|
|
72
|
Class::C3::reinitialize(); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 load_optional_components |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
As L, but will silently ignore any components that cannot be |
102
|
|
|
|
|
|
|
found. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub load_optional_components { |
107
|
2
|
|
|
2
|
1
|
1256
|
my $class = shift; |
108
|
1
|
|
|
|
|
9
|
$class->_load_components( grep |
109
|
2
|
100
|
|
|
|
29
|
{ $class->load_optional_class( $_ ) } |
110
|
|
|
|
|
|
|
( map |
111
|
2
|
|
|
|
|
8
|
{ /^\+(.*)$/ |
112
|
|
|
|
|
|
|
? $1 |
113
|
|
|
|
|
|
|
: join ('::', $class->component_base_class, $_) |
114
|
|
|
|
|
|
|
} |
115
|
2
|
|
|
|
|
4
|
grep { $_ !~ /^#/ } @_ |
116
|
|
|
|
|
|
|
) |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 ensure_class_loaded |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Given a class name, tests to see if it is already loaded or otherwise |
123
|
|
|
|
|
|
|
defined. If it is not yet loaded, the package is require'd, and an exception |
124
|
|
|
|
|
|
|
is thrown if the class is still not loaded. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
BUG: For some reason, packages with syntax errors are added to %INC on |
127
|
|
|
|
|
|
|
require |
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub ensure_class_loaded { |
131
|
19
|
|
|
19
|
1
|
55
|
my ($class, $f_class) = @_; |
132
|
|
|
|
|
|
|
|
133
|
7
|
|
|
7
|
|
46
|
no strict 'refs'; |
|
7
|
|
|
|
|
567
|
|
|
7
|
|
|
|
|
3012
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# ripped from Class::Inspector for speed |
136
|
|
|
|
|
|
|
# note that the order is important (faster items are first) |
137
|
19
|
100
|
|
|
|
28
|
return if ${"${f_class}::VERSION"}; |
|
19
|
|
|
|
|
211
|
|
138
|
|
|
|
|
|
|
|
139
|
18
|
100
|
|
|
|
31
|
return if @{"${f_class}::ISA"}; |
|
18
|
|
|
|
|
348
|
|
140
|
|
|
|
|
|
|
|
141
|
16
|
|
|
|
|
87
|
my $file = (join ('/', split ('::', $f_class) ) ) . '.pm'; |
142
|
16
|
50
|
|
|
|
64
|
return if $INC{$file}; |
143
|
|
|
|
|
|
|
|
144
|
16
|
|
|
|
|
24
|
for ( keys %{"${f_class}::"} ) { |
|
16
|
|
|
|
|
67
|
|
145
|
33
|
100
|
|
|
|
41
|
return if ( *{"${f_class}::$_"}{CODE} ); |
|
33
|
|
|
|
|
152
|
|
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# require always returns true on success |
149
|
|
|
|
|
|
|
# ill-behaved modules might very well obliterate $_ |
150
|
13
|
100
|
|
|
|
52
|
eval { local $_; require($file) } or do { |
|
13
|
|
|
|
|
21
|
|
|
13
|
|
|
|
|
16109
|
|
151
|
|
|
|
|
|
|
|
152
|
8
|
100
|
|
|
|
1403
|
$@ = "Invalid class name '$f_class'" if $f_class =~ $invalid_class; |
153
|
|
|
|
|
|
|
|
154
|
8
|
50
|
|
|
|
173
|
if ($class->can('throw_exception')) { |
155
|
0
|
|
|
|
|
0
|
$class->throw_exception($@); |
156
|
|
|
|
|
|
|
} else { |
157
|
8
|
|
|
|
|
199
|
Carp::croak $@; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
}; |
160
|
|
|
|
|
|
|
|
161
|
5
|
|
|
|
|
7365
|
return; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 ensure_class_found |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Returns true if the specified class is installed or already loaded, false |
167
|
|
|
|
|
|
|
otherwise. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Note that the underlying mechanism (Class::Inspector->installed()) used by this |
170
|
|
|
|
|
|
|
sub will not, at the time of writing, correctly function when @INC includes |
171
|
|
|
|
|
|
|
coderefs. Since PAR relies upon coderefs in @INC, this function should be |
172
|
|
|
|
|
|
|
avoided in modules that are likely to be included within a PAR. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub ensure_class_found { |
177
|
|
|
|
|
|
|
#my ($class, $f_class) = @_; |
178
|
4
|
|
|
4
|
1
|
4433
|
require Class::Inspector; |
179
|
4
|
|
100
|
|
|
26
|
return Class::Inspector->loaded($_[1]) || |
180
|
|
|
|
|
|
|
Class::Inspector->installed($_[1]); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 inject_base |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Does the actual magic of adjusting @ISA on the target module. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=cut |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub inject_base { |
191
|
7
|
|
|
7
|
1
|
17
|
my $class = shift; |
192
|
7
|
|
|
|
|
12
|
my $target = shift; |
193
|
|
|
|
|
|
|
|
194
|
7
|
|
|
|
|
30
|
mro::set_mro($target, 'c3'); |
195
|
|
|
|
|
|
|
|
196
|
7
|
|
|
|
|
16
|
for my $comp (reverse @_) { |
197
|
7
|
|
|
|
|
16
|
my $apply = do { |
198
|
7
|
|
|
7
|
|
42
|
no strict 'refs'; |
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
2589
|
|
199
|
7
|
|
|
7
|
|
57
|
sub { unshift ( @{"${target}::ISA"}, $comp ) }; |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
206
|
|
200
|
|
|
|
|
|
|
}; |
201
|
7
|
50
|
33
|
|
|
154
|
unless ($target eq $comp || $target->isa($comp)) { |
202
|
7
|
|
|
|
|
13
|
our %APPLICATOR_FOR; |
203
|
7
|
100
|
|
|
|
27
|
if (my $apply_class |
204
|
8
|
|
|
8
|
|
78
|
= List::Util::first { $APPLICATOR_FOR{$_} } @{mro::get_linear_isa($comp)} |
|
7
|
|
|
|
|
90
|
|
205
|
|
|
|
|
|
|
) { |
206
|
4
|
|
|
|
|
29
|
$APPLICATOR_FOR{$apply_class}->_apply_component_to_class($comp,$target,$apply); |
207
|
|
|
|
|
|
|
} else { |
208
|
3
|
|
|
|
|
9
|
$apply->(); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head2 load_optional_class |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Returns a true value if the specified class is installed and loaded |
217
|
|
|
|
|
|
|
successfully, throws an exception if the class is found but not loaded |
218
|
|
|
|
|
|
|
successfully, and false if the class is not installed |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub load_optional_class { |
223
|
9
|
|
|
9
|
1
|
8033
|
my ($class, $f_class) = @_; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
# ensure_class_loaded either returns a () (*not* true) or throws |
226
|
9
|
100
|
|
|
|
19
|
eval { |
227
|
9
|
|
|
|
|
39
|
$class->ensure_class_loaded($f_class); |
228
|
3
|
|
|
|
|
19
|
1; |
229
|
|
|
|
|
|
|
} && return 1; |
230
|
|
|
|
|
|
|
|
231
|
6
|
|
|
|
|
3329
|
my $err = $@; # so we don't lose it |
232
|
|
|
|
|
|
|
|
233
|
6
|
100
|
|
|
|
236
|
if ($f_class =~ $invalid_class) { |
234
|
1
|
|
|
|
|
4
|
$err = "Invalid class name '$f_class'"; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
else { |
237
|
5
|
|
|
|
|
36
|
my $fn = quotemeta( (join ('/', split ('::', $f_class) ) ) . '.pm' ); |
238
|
5
|
100
|
|
|
|
130
|
return 0 if ($err =~ /Can't locate ${fn} in \@INC/ ); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
3
|
50
|
|
|
|
21
|
if ($class->can('throw_exception')) { |
242
|
0
|
|
|
|
|
0
|
$class->throw_exception($err); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
else { |
245
|
3
|
|
|
|
|
18
|
die $err; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head1 AUTHORS |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Matt S. Trout and the L |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Pulled out into seperate module by Ash Berlin C<< >> |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Optimizations and overall bolt-tightening by Peter "ribasushi" Rabbitson |
256
|
|
|
|
|
|
|
C<< >> |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head1 COPYRIGHT |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Copyright (c) 2006 - 2011 the Class::C3::Componentised L as listed |
261
|
|
|
|
|
|
|
above. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head1 LICENSE |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
You may distribute this code under the same terms as Perl itself. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=cut |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
1; |