AutoGenerate.pm

filter: Statementstmt 99.1% 123 / 124 Branchbran 88.2% 30 / 34 Conditioncond 55.0% 11 / 20 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 100% 19 / 19 Podpod 100% 4 / 4 totaltotal 88.6% 188 / 212 CC 29 SCAR 15.3
CC
29
Cov
92%
_match_and_generate
8.0
new
7.0
autogenerator_of
6.3
CRAP 4.6
↑ Summary AutoGenerate/Declare.pm »
1 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Class::AutoGenerate;
5  
6 18 ▶use Class::AutoGenerate::Declare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Scalar::Util qw/ blessed reftype /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 our $VERSION = 0.05;
10  
11 18 our %AUTOGENERATED;
12  
13 =head1 NAME
14  
15 Class::AutoGenerate - Automatically generate code upon require or use
16  
17 =head1 SYNOPSIS
18  
19 # Create a customized class loader (auto-generator)
20 package My::ClassLoader;
21 use Class::AutoGenerate -base;
22  
23 # Define a matching rule that generates some code...
24 requiring 'Some::**::Class' => generates { qq{
25 sub print_my_middle_names { print $1,"\n" }
26 } };
27  
28 # In some other file, let's use the class loader
29 package main;
30  
31 # Create the class loader, which adds itself to @INC
32 use My::ClassLoader;
33 BEGIN { My::ClassLoader->new( match_only => '**::Freaking::Class' ); }
34  
35 # These class will be generated on the fly...
36 use Some::Freaking::Class;
37 use Some::Other::Freaking::Class;
38  
39 Some::Freaking::Class->print_my_middle_names;
40 Some::Other::Freaking::Class->print_my_middle_names;
41  
42 # Output is:
43 # Freaking
44 # Other::Freaking
45  
46 =head1 DESCRIPTION
47  
48 B<EXPERIMENTAL.> I'm trying this idea out. Please let me know what you think by contacting me using the information listed under L</AUTHOR>. This is an experiment and any and all aspects of the API are up for revision at this point and I'm not even sure I'll maintain it, but I hope it will be found useful to myself and others.
49  
50 Sometimes it's nice to be able to generate code on the fly. This tool does just that. You declare a few rules that can be used to define the class names you want to auto-generate and then the code that is to be built from it. Later you create your auto-generator object and start using the auto-generated classes.
51  
52 This is a generalization baed upon L<Jifty::ClassLoader>. If this experiment is successful in the way I'm testing it out for, it may be used to re-implement that class.
53  
54 =head1 METHODS
55  
56 =head2 import
57  
58 When you are creating a new auto-generating class loader, you will include this statement in your package definition:
59  
60 package My::ClassLoader;
61 use Class::AutoGenerate -base;
62  
63 This statement tells L<Class::AutoGenerate> to import all the subroutines in L<Class::AutoGenerate::Declare> into the current package so that a new class loader can be declared.
64  
65 Later, when you use your class loader, you will use the undecorated form:
66  
67 use My::ClassLoader;
68  
69 In this case, the import method does nothing special.
70  
71 =cut
72  
73 21 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
74 21 my $class = shift;
75 21 my $base = shift;
76  
77 21 my $package = caller;
78  
79 21 ▶ if (defined $base and $base eq '-base') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base and $base eq "-base") 18 3 21
Condition66%2/3
defined $base and $base eq "-base"
!ll&&!rl&&r
3018
MC/DC50%1/2
defined $base and $base eq "-base"
defined $base $base eq "-base"
Truth table66%2/3
A and B
A= defined $baseB= $base eq "-base"
ABresult
0X0
100
111
80 18 Class::AutoGenerate::Declare->export_to_level(1, undef);
81
82 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 18 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
84 18 push @{ $package . '::ISA' }, $class;
85 18 @{ $package . '::RULES' } = ();
86  
87 18 *{ $package . '::autogenerated' } = *autogenerated;
88 18 *{ $package . '::autogenerator_of' } = *autogenerator_of;
89 }
90  
91 21 return 1;
92 }
93  
94 =head2 new
95  
96 Creates a new instance of the auto-generating class loader object you've built. The class loader automatically adds itself to the C<@INC> array to start loading classes.
97  
98 If you want to immediately start using the class loader at compile time, you may wish to call this method within a C<BEGIN> block:
99  
100 use My::Custom::ClassLoader;
101 BEGIN { My::Custom::ClassLoader->new };
102  
103 The constructor also recognizes the following options, passed in a hash, that can modify the behavior of the class loader.
104  
105 =over
106  
107 =item match_only
108  
109 This argument may be passed as anything that would be accepted in a L<Class::AutoGenerate::Declare/requiring> clause and is used to prequalify which classes may actually be generated by this class loader. Using this, you can build one generic class loader that may be limited in how it is applied.
110  
111 A module will only be generated if it first matches at least one of the patterns provided to "match_only".
112  
113 For example,
114  
115 package My::ClassLoader;
116 use Class::AutoGenerate -base;
117  
118 requiring '**' => generates {};
119  
120 1;
121  
122 BEGIN {
123 My::ClassLoader->new( match_only => [ 'Prefix1::**', 'Prefix2::*' ] );
124 }
125  
126 use Prefix1::Thing;
127 use Prefix2::Thing;
128 use Prefix3::Thing; # <--- ERROR: does not match the match_only clause
129  
130 =back
131  
132 =cut
133  
134 21 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
135 21 my $class = shift;
136 21 my %args = @_;
137  
138 # Create the class
139 21 my $self = bless \%args, $class;
140  
141 # if the match_only is given, remember that
142 21 ▶ if (defined $self->{match_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"match_only"}) 7 14 21
143 $self->{match_only} = [ $self->{match_only} ]
144 7 ▶ unless ref $self->{match_only};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"match_only"} 6 1 7
145  
146 $_ = Class::AutoGenerate::Declare::_compile_glob_pattern($_)
147 7 foreach (@{ $self->{match_only} });
148  
149 $self->{match_only} = $self->{match_only}
150 7 ▶ if defined $self->{match_only};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"match_only"} 7 0 7
151 }
152  
153 # Now, load the rule set from the declarations
154 21 for my $declaration (@{ $self->_declarations }) {
155  
156 # Handle declare { ... } blocks
157 47 ▶ if (reftype $declaration eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($declaration) eq 'CODE') { } 2 45 47
158 2 my @declarations = $declaration->($self);
159 2 push @{ $self->{rules} }, @declarations;
160 }
161  
162 # Handle top-level requiring ... => rules
163 else {
164 45 push @{ $self->{rules} }, $declaration;
165 }
166 }
167  
168 # place ourself into @INC
169 21 push @INC, $self;
170  
171 21 return $self;
172 }
173  
174 =head2 INC
175  
176 This is the subroutine called by Perl during a L<perlfunc/require|require> or L<perlfunc/use|use> and evaluates the rules defined in your class loader. See L<perlfunc/require|require> (towards the end) to see how this works.
177  
178 It should be noted, however, that we cheat the system a little bit. According ot the require hook API, this method should return either a filehandle containing the code to be read or C<undef> indicating that the hook does not know about the file being required.
179  
180 This is done, except that only an empty stub package like this is ever returned when a class is auto-generated:
181  
182 use strict;
183 use warnings;
184  
185 package The::Included::Package::Name;
186  
187 1;
188  
189 Instead of having the import mechanism within Perl compile the code, most of the work is handled through symbol table manipulations and code evaluation before the file handle is returned. This allows for some earlier compile-time checking via closures and the like.
190  
191 =cut
192  
193 # Use the fully-qualified name since Perl ignores "sub INC"
194 # (see perldoc require)
195 104 ▶sub Class::AutoGenerate::INC {
Subroutine INC: called CC=1 SCAR=0.0
Pod documented
196 104 my $self = shift;
197 104 my $module = shift;
198  
199 # Canonicalize $module to :: style rather than / and .pm style
200 104 $module =~ s{\.pm$}{};
201 104 $module =~ s{/}{::}g;
202  
203 # Pass off control to _match_and_generate() to do the real work
204 104 return $self->_match_and_generate($module);
205 }
206  
207 =head2 autogenerated MODULE
208  
209 This method may be called in any of the following ways:
210  
211 Class::AutoGenerate::autogenerated 'Some::Module';
212 Class::AutoGenerate->autogenerated('Some::Module');
213  
214 # Where My::AutoGenerator->isa('Class::AutoGenerate')
215 My::AutoGenerator::autogenerated 'Some::Module';
216 My::AutoGenerator->autogenerated('Some::Module');
217  
218 # Where $autogenerator->isa('Class::AutoGenerate');
219 $autogenerator->autogenerated('Some::Module');
220  
221 Returns true if the package named was autogenerated by a L<Class::AutoGenerate> class loader. Returns C<undef> in any other case.
222  
223 =cut
224  
225 18 ▶sub autogenerated($) {
Subroutine autogenerated: called CC=5 SCAR=17.2
Pod documented
226 18 my $class = shift;
227 18 ▶ if (blessed $class or (not ref $class and $class =~ /^[:\w]+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) or not ref $class and $class =~ /^[:\w]+$/) 18 0 18
Condition50%3/6
builtin::blessed($class) or not ref $class and $class =~ /^[:\w]+$/
l!l&&r!l&&!r
6120
not ref $class and $class =~ /^[:\w]+$/
!ll&&!rl&&r
0012
MC/DC0%0/3
builtin::blessed($class) or not ref $class and $class =~ /^[:\w]+$/
builtin::blessed($class) not ref $class $class =~ /^[:\w]+$/
Truth table50%2/4
A or not(B) and C
A= builtin::blessed($class)B= not ref $classC= $class =~ /^[:\w]+$/
ABCresult
1XX1
0111
00X0
0100
228 18 ▶ $class = shift if $class->isa('Class::AutoGenerate');
Branch: 1 branch
BranchTrueFalseTotal
if $class->isa("Class::AutoGenerate") 12 6 18
229 }
230  
231 18 return exists $AUTOGENERATED{ $class };
232 }
233  
234 =head2 autogenerator_of MODULE
235  
236 This method may be called in any of the following ways:
237  
238 Class::AutoGenerate::autogenerator_of 'Some::Module';
239 Class::AutoGenerate->autogenerator_of('Some::Module');
240  
241 # Where My::AutoGenerator->isa('Class::AutoGenerate')
242 My::AutoGenerator::autogenerator_of 'Some::Module';
243 My::AutoGenerator->autogenerator_of('Some::Module');
244  
245 # Where $autogenerator->isa('Class::AutoGenerate');
246 $autogenerator->autogenerator_of('Some::Module');
247  
248 Returns the object that was used to autogenerate the module. This is really just a shortcut for looking up the information in C<%INC>, but saves some work of converting Perl module names into package file names and the cryptic use of the C<%INC> variable.
249  
250 =cut
251  
252 18 ▶sub autogenerator_of($) {
Subroutine autogenerator_of: called CC=6 SCAR=18.5
Pod documented
253 18 my $class = shift;
254 18 ▶ if (blessed $class or (not ref $class and $class =~ /^[:\w]+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) or not ref $class and $class =~ /^[:\w]+$/) 18 0 18
Condition50%3/6
builtin::blessed($class) or not ref $class and $class =~ /^[:\w]+$/
l!l&&r!l&&!r
6120
not ref $class and $class =~ /^[:\w]+$/
!ll&&!rl&&r
0012
MC/DC0%0/3
builtin::blessed($class) or not ref $class and $class =~ /^[:\w]+$/
builtin::blessed($class) not ref $class $class =~ /^[:\w]+$/
Truth table50%2/4
A or not(B) and C
A= builtin::blessed($class)B= not ref $classC= $class =~ /^[:\w]+$/
ABCresult
1XX1
0111
00X0
0100
255 18 ▶ $class = shift if $class->isa('Class::AutoGenerate');
Branch: 1 branch
BranchTrueFalseTotal
if $class->isa("Class::AutoGenerate") 12 6 18
256 }
257  
258 # Convert the module name into a package file, Some::Thing -> Some/Thing.pm
259 18 my $package_file = $class;
260 18 $package_file =~ s{::}{/}g;
261 18 $package_file .= '.pm';
262  
263 18 ▶ return exists($INC{ $package_file }) ? $INC{ $package_file } : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $INC{$package_file} ? : 12 6 18
264 }
265  
266 =head2 _match_and_generate MODULE
267  
268 This method is used internally to match L<Class::AutoGenerate::Declare/requiring> statements and automatically generate code upon a match.
269  
270 =cut
271  
272 104 ▶sub _match_and_generate {
Subroutine _match_and_generate: called CC=8 SCAR=20.8
273 104 my $self = shift;
274 104 my $module = shift;
275  
276 # If match_only is specified, make sure it matches that first
277 104 ▶ if (defined $self->{match_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"match_only"}) 34 70 104
278 59 return unless grep { $self->_match_requiring($module, $_) }
279 34 ▶ @{ $self->{match_only} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->_match_requiring($module, $_);} @{$$self{"match_only"};} 3 31 34
280 }
281  
282 # Get the requiring/generates rules
283 101 my $rules = $self->_rules;
284 #use Data::Dumper;
285 #$Data::Dumper::Deparse = 1;
286 #Test::More::diag(Dumper($rules));
287  
288 # Iterate through the rules
289 RULE:
290 101 for my $rule (@$rules) {
291  
292 # Does it match? First match wins...
293 252 ▶ if ($self->_match_requiring($module, $rule->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match_requiring($module, $rule->[0])) 86 166 252
294 86 my $conclude_with = eval {
295 86 $self->_autogenerate($module, $rule->[0], $rule->[1]);
296 };
297  
298 # Handle a call to next_rule
299 86 ▶ if ($@ eq "NEXT_RULE\n") {
Branch: 3 branches
BranchTrueFalseTotal
if ($@ eq "NEXT_RULE\n") { } 1 85 86
elsif ($@ eq "LAST_RULE\n") { } 1 84 85
elsif ($@) { } 0 84 84
300 1 next RULE;
301 }
302  
303 # Handle a call to last_rule
304 elsif ($@ eq "LAST_RULE\n") {
305 1 last RULE;
306 }
307  
308 # Handle a regular exception
309 elsif ($@) {
310 0 die $@;
311 }
312  
313 # Return the empty stub to signal class found
314 84 return $self->_stub_file_handle($module, $conclude_with);
315 }
316 }
317  
318 # Return undef to signal no such file found
319 17 return;
320 }
321  
322 =head2 _declarations
323  
324 Used internally to reference the L<Class::AutoGenerate::Declare/declare> blocks and top-level L<Class::AutoGenerate::Declare/requiring> rules in the auto-generating class loader's definition.
325  
326 These are, then, instantiated to build the L</_rules> for the object when L</new> is called.
327  
328 =cut
329  
330 65 ▶sub _declarations {
Subroutine _declarations: called CC=2 SCAR=6.9
331 65 my $self = shift;
332 65 ▶ my $package = blessed $self || $self;
Condition66%2/3
builtin::blessed($self) || $self
l!l&&r!l&&!r
21440
MC/DC0%0/2
builtin::blessed($self) || $self
builtin::blessed($self) $self
Truth table66%2/3
A || B
A= builtin::blessed($self)B= $self
ABresult
1X1
011
000
333  
334 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
335 18 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
336 65 return \@{ $package . '::DECLARATIONS' };
337 }
338  
339 =head2 _rules
340  
341 Used internally to reference the rules declared and instantiated within the auto-generating class loader.
342  
343 =cut
344  
345 101 ▶sub _rules {
Subroutine _rules: called CC=1 SCAR=0.0
346 101 my $self = shift;
347 101 return $self->{rules};
348 }
349  
350 =head2 _match_requiring MODULE, PATTERN
351  
352 Used internally to match a L<Class::AutoGenerate::Declare/requiring> declaration to a package name. Returns true if there's a match, or false otherwise.
353  
354 =cut
355  
356 311 ▶sub _match_requiring {
Subroutine _match_requiring: called CC=2 SCAR=6.9
357 311 my $self = shift;
358 311 my $module = shift;
359 311 my $pattern = shift;
360  
361 311 ▶ if ($module =~ $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /$pattern/) { } 117 194 311
362 #Test::More::diag("$module matches $pattern");
363 117 return 1;
364 }
365 else {
366 #Test::More::diag("$module misses $pattern");
367 194 return;
368 }
369 }
370  
371 =head2 _autogenerate MODULE, PATTERN, GENERATES
372  
373 This method performs the action of taking the work in the generates declration and stuffing that work into the named package.
374  
375 =cut
376  
377 18 our ($package, $conclude_with);
378 86 ▶sub _autogenerate {
Subroutine _autogenerate: called CC=1 SCAR=0.0
379 86 my $self = shift;
380 86 my $module = shift;
381 86 my $pattern = shift;
382 86 my $generates = shift;
383  
384 # match again to setup $1, $2, etc...
385 86 $module =~ $pattern;
386  
387 # Setup the $package variable used inside the various declarations
388 86 local $package = $module;
389 86 local $conclude_with = [];
390  
391 # Call the code to generate the various codes
392 86 $generates->();
393  
394 # Remember that it was generated
395 84 $AUTOGENERATED{ $module } = 1;
396  
397 84 return $conclude_with;
398 }
399  
400 =head2 _stub_file_handle MODULE, CONCLUSIONS
401  
402 Returns a basic stub class that is handed off to the import infrastructure of Perl to let it know that we succeeded, even though we already did most of the work for it.
403  
404 =cut
405  
406 84 ▶sub _stub_file_handle {
Subroutine _stub_file_handle: called CC=3 SCAR=11.0
407 84 my $self = shift;
408 84 my $module = shift;
409 84 ▶ my $conclusions = shift || [];
Condition50%1/2
shift() || []
l!l
840
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
410  
411 # Here's the stub code...
412 84 my $code = qq{use strict; use warnings; package $module; };
413 84 for my $conclusion (@$conclusions) {
414 2 $code .= "{ $conclusion } ";
415 }
416 84 $code .= "1; ";
417  
418 # Magick that code into a file handle
419 84 open my $fh, '<', \$code;
420 84 return $fh;
421 }
422  
423 =head1 SEE ALSO
424  
425 L<Class::AutoGenerate::Declare>
426  
427 =head1 AUTHOR
428  
429 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
430  
431 =head1 COPYRIGHT AND LICENSE
432  
433 Copyright 2007 Boomer Consulting, Inc.
434  
435 This program is free software and may be modified and distributed under the same terms as Perl itself.
436  
437 =cut
438  
439 18 1;
↑ Summary AutoGenerate/Declare.pm »