lib/Class/Interface.pm

filter: Statementstmt 72.5% 111 / 153 Branchbran 48.6% 35 / 72 Conditioncond 60.0% 12 / 20 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 85.0% 17 / 20 Podpod 57.1% 4 / 7 totaltotal 64.5% 184 / 285 CC 54 SCAR 26.2
CC
54
Cov
64%
extends
27.4
implements
26.5
__ANON__
20.0
CRAP 13.8
↑ Summary test-lib/Car/Factory.pm »
1 package Class::Interface;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Class::Interface - A class for implementing/extending interfaces/abstracts in Perl.
8  
9 =head1 SYNOPSIS
10  
11 =head2 Declaring an interface
12  
13 package Bouncable;
14  
15 use Class::Interface;
16 &interface; # this actually declares the interface
17  
18 sub bounce;
19 sub getBounceBack;
20  
21 1;
22  
23 This creates an interface (a contract between classes if you like)
24 that specifies that each class implementing the Bouncable
25 interface must have an implementation of the routines bounce
26 and getBounceBack.
27  
28 =head2 Declaring an implementing class
29  
30 package Ball;
31  
32 use Class::Interface;
33 &implements( 'Bouncable' );
34  
35 sub bounce {
36 my $self = shift;
37 print "The ball is bouncing @ ".$self->getBounceBack." strength"
38 }
39  
40 sub getBounceBack {
41 return 10;
42 }
43  
44 1;
45  
46 =head2 Declaring an abstract
47  
48 package AbstractInterestCalculator;
49  
50 use Class::Interface;
51 &abstract; # this actually declares this class to be abstract;
52  
53 use Class::AccessorMaker {
54 interest => 5.1,
55 maxInterestValue => 0,
56 }
57  
58 # a hook for doing calculations
59 sub calculate {
60 my ( $self, $value ) = @_;
61  
62 $self->prepare();
63 $value += $self->getInterestValue( $value );
64  
65 return $value;
66 }
67  
68 sub prepare; # prepare calculations
69 sub getInterstValue; # get the interest value
70  
71 1;
72  
73 =head2 Extending from an abstract class
74  
75 package LowInterestCalculator;
76  
77 use Class::Interface;
78 &extends( 'AbstractInterestCalculator' );
79  
80 sub prepare {
81 my ( $self ) = @_;
82 $self->interest(1.3);
83  
84 # we don't give interest if the value of the account is or
85 # exceeds $10.000
86 $self->maxInterestValue(10000)
87 }
88  
89 sub getInterstValue {
90 my ( $self, $value ) = @_
91  
92 if ( $self->maxInterestValue &&
93 $value >= $self->maxInterestValue ) {
94 return 0;
95 }
96  
97 $value *= $self->interest;
98  
99 return $value;
100 }
101  
102 =head1 DESCRIPTION
103  
104 Performs some underwater perl-magic to ensure interfaces are
105 interfaces and classes that implement the interface actually do so.
106  
107 =head1 INTERFACE RULES
108  
109 =over 4
110  
111 =item * An interface must use the Class::Interface module.
112  
113 =item * An interface must call the 'interface' method.
114  
115 =item * An interface must declare at least one routine
116  
117 =item * Routines may not have an implementation
118  
119 =back
120  
121 =head1 ABSTRACT RULES
122  
123 =over 4
124  
125 =item * An abstract must use the Class::Interface module.
126  
127 =item * An abstract must call the 'abstract' method.
128  
129 =item * An abstract must declare at least one abstract routine.
130  
131 =back
132  
133 =head1 ROUTINE RULES
134  
135 =over 4
136  
137 =item * Routines must be declared as one of:
138  
139 =over 4
140  
141 =item - sub routine;
142  
143 =item - sub routine {}
144  
145 =back
146  
147 B<NOTE>: When using curly braces in routine declarations they must stay
148 on the same line. The amount of whitespace between them and/or the
149 routine name is free of ruling.
150  
151 =back
152  
153 =head1 ANNOTATIONS
154  
155 It helps to think of these methods as Java style annotations. But
156 instead of calling them with @interface you use &interface.
157  
158 =cut
159  
160 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 5 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
164 5 @Class::Interface::EXPORT = qw(implements interface extends abstract);
165  
166 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
167  
168 # some default class vars
169 5 $Class::Interface::VERSION = "1.01";
170  
171 # some class vars for changing behaviour
172 5 $Class::Interface::AUTO_CONSTRUCTOR = 0;
173 5 $Class::Interface::CONFESS = 0;
174  
175 # define a contract
176 sub error(*);
177  
178 =pod
179  
180 =head2 &interface()
181  
182 Turns the calling class into an interface.
183  
184 =cut
185 10 ▶sub interface() {
Subroutine interface: called CC=5 SCAR=16.7
Pod documented
186 10 my $caller = caller();
187  
188 10 ▶ return if !$caller || $caller eq "main";
Branch: 1 branch
BranchTrueFalseTotal
if not $caller or $caller eq "main" 0 7 7
Condition33%1/3
not $caller or $caller eq "main"
l!l&&r!l&&!r
007
MC/DC0%0/2
not $caller or $caller eq "main"
not $caller $caller eq "main"
Truth table33%1/3
not(A) or B
A= not $callerB= $caller eq "main"
ABresult
1X1
011
000
189  
190 # interfaces should be usable.
191 4 ▶ eval "use $caller";
Subroutine BEGIN: called CC=1 SCAR=0.0
192 10 ▶ error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
193  
194 10 my @subs = inspectInterface($caller);
195  
196 10 ▶ error "Interface $caller does not provide any methods" if $#subs < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#subs < 0 0 7 7
197  
198 # first prevent usage of interfaces (but allow it from me).
199 10 ▶ *{ $caller . "::import" } = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
200 19 my $caller = caller();
201  
202 19 ▶ if ( $caller ne "Class::Interface" and $caller ne "main" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller ne "Class::Interface" and $caller ne "main") 0 16 16
Condition66%2/3
$caller ne "Class::Interface" and $caller ne "main"
!ll&&!rl&&r
1510
MC/DC0%0/2
$caller ne "Class::Interface" and $caller ne "main"
$caller ne "Class::Interface" $caller ne "main"
Truth table66%2/3
A and B
A= $caller ne "Class::Interface"B= $caller ne "main"
ABresult
0X0
100
111
203 3 error "$caller is an interface. It can't be used";
204 }
205 10 };
206  
207 # tell any interface users this is an interface and return the
208 # expected routines.
209 10 ▶ *{ $caller . "::__get_interface_methods__" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
210 19 return @subs;
211 10 };
212 }
213  
214 =pod
215  
216 =head2 &abstract()
217  
218 Turns the calling class into an abstract.
219  
220 =cut
221 6 ▶sub abstract() {
Subroutine abstract: called CC=6 SCAR=18.7
Pod documented
222 6 my $caller = caller();
223  
224 3 ▶ return if !$caller || $caller eq "main";
Branch: 1 branch
BranchTrueFalseTotal
if not $caller or $caller eq "main" 0 3 3
Condition33%1/3
not $caller or $caller eq "main"
l!l&&r!l&&!r
003
MC/DC0%0/2
not $caller or $caller eq "main"
not $caller $caller eq "main"
Truth table33%1/3
not(A) or B
A= not $callerB= $caller eq "main"
ABresult
1X1
011
000
225  
226 # interfaces should be usable.
227 3 ▶ eval "use $caller";
Subroutine BEGIN: called CC=1 SCAR=0.0
228 3 ▶ error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
229  
230 3 my @subs = inspectInterface( $caller, 1 );
231  
232 # abstract classes must have abstract methods
233 3 ▶ error "Abstract interface $caller does not provide any methods" if $#subs < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#subs < 0 0 3 3
234  
235 # tell any abstract users this is an abstract and return the
236 # expected routines.
237 3 ▶ *{ $caller . "::__get_abstract_methods__" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
238 3 return @subs;
239 3 };
240  
241 # overwrite the abstract routines and make them die on invocation
242 3 foreach my $sub (@subs) {
243 3 ▶ *{ $caller . "::" . $sub } = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
244 0 die("You are trying to invoke the abstract method $sub from $caller");
245 3 };
246 }
247 }
248  
249 =pod
250  
251 =head2 &implements()
252  
253 Loads the given interfaces and checks the calling class for presence
254 of the wanted routines.
255  
256 If all goes well pushes the name of the interface to the ISA array of
257 the class.
258  
259 =cut
260 9 ▶sub implements(@) {
Subroutine implements: called CC=12 SCAR=32.8
Pod documented
261 9 my $caller = caller;
262  
263 9 my %missing;
264 9 foreach my $implements (@_) {
265 15 eval "use $implements;";
266 15 ▶ error
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 15 15
267 "$caller tries to implement non existing interface $implements -- $@"
268 if $@;
269  
270 15 ▶ unless ( defined ( &{ $implements . "::__get_interface_methods__" } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$implements . '::__get_interface_methods__';}) 0 15 15
271 0 error "$caller tries to implement non-interface $implements"
272 }
273  
274 # find the subs from the interface
275 15 foreach my $sub ( &{ $implements . "::__get_interface_methods__" } ) {
276 42 ▶ unless ( UNIVERSAL::can( $caller, $sub ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can($caller, $sub)) 0 42 42
277 0 ▶ $missing{$implements} = [] unless exists $missing{$implements};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $missing{$implements} 0 0 0
278 0 push @{ $missing{$implements} }, $sub;
279 }
280 }
281 }
282  
283 9 ▶ if ( keys %missing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %missing) 0 9 9
284 0 my $dieMessage = "";
285 0 foreach my $interface ( keys %missing ) {
286 0 foreach my $sub ( @{ $missing{$interface} } ) {
287 0 ▶ $dieMessage .= ",\n" if $dieMessage;
Branch: 1 branch
BranchTrueFalseTotal
if $dieMessage 0 0 0
288 0 $dieMessage .= "$caller fails to implement $sub from $interface";
289 }
290 }
291  
292 0 error $dieMessage;
293 }
294  
295 # make sure the import is not found through inheritance.
296 9 ▶ unless ( defined &{ $caller . "::import" } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$caller . '::import';}) 9 0 9
297 9 ▶ *{ $caller . "::import" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
298  
299 # don't cascade up to the interface.
300 }
301 9 }
302  
303 9 makeMagicConstructor($caller);
304  
305 9 push @{ $caller . "::ISA" }, @_;
306 }
307  
308 =pod
309  
310 =head2 &extends()
311  
312 Loads the given abstract class and checks the calling class for presence
313 of the abstract routines.
314  
315 If all goes well pushes the name of the abstract class to the ISA
316 array of the class.
317  
318 =cut
319 2 ▶sub extends(*) {
Subroutine extends: called CC=11 SCAR=33.1
Pod documented
320 2 my $caller = caller();
321  
322 2 my %missing;
323 2 foreach my $extends (@_) {
324 2 eval "use $extends;";
325 2 ▶ error
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
326 "$caller tries to implement non existing abstract class $extends -- $@"
327 if $@;
328  
329 2 ▶ unless ( defined ( &{ $extends . "::__get_abstract_methods__" } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$extends . '::__get_abstract_methods__';}) 0 2 2
330 0 error "$caller tries to implement non-abstract $extends"
331 }
332  
333  
334 # find the subs from the interface
335 2 foreach my $sub ( &{ $extends . "::__get_abstract_methods__" } ) {
336 2 ▶ unless ( UNIVERSAL::can( $caller, $sub ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can($caller, $sub)) 0 2 2
337 0 ▶ $missing{$extends} = [] unless exists $missing{$extends};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $missing{$extends} 0 0 0
338 0 push @{ $missing{$extends} }, $sub;
339 }
340 }
341 }
342  
343 2 ▶ if ( keys %missing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %missing) 0 2 2
344 0 my $dieMessage = "";
345 0 foreach my $abstract ( keys %missing ) {
346 0 foreach my $sub ( @{ $missing{$abstract} } ) {
347 0 ▶ $dieMessage .= ",\n" if $dieMessage;
Branch: 1 branch
BranchTrueFalseTotal
if $dieMessage 0 0 0
348 0 $dieMessage .=
349 "$caller fails to implement $sub from abstract class $abstract";
350 }
351 }
352  
353 0 error $dieMessage;
354 }
355  
356 2 makeMagicConstructor($caller);
357  
358 2 push @{ $caller . "::ISA" }, @_;
359 }
360  
361 # private methods
362 #
363  
364 # perform interface inspections
365 10 ▶sub inspectInterface {
Subroutine inspectInterface: called CC=16 SCAR=28.2
Pod undocumented
366 10 my $interface = shift;
367 10 ▶ my $asAbstract = shift || 0;
Condition100%2/2
shift() || 0
l!l
37
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
368  
369 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
370  
371 10 ( my $keyName = $interface ) =~ s/\:\:/\//g;
372 10 $keyName .= ".pm";
373  
374 10 my $file = $INC{$keyName};
375 10 open( local *IN, "<$file" );
376  
377 10 my @subs = ();
378 10 my $usesInterfaces;
379 my $callsInterface;
380 10 ▶ while ( chomp( my $line = <IN> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (chomp(my $line = readline IN)) 166 10 176
381 # leave if the source file says so.
382 166 ▶ last if $line eq "__END__";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "__END__" 0 166 166
383  
384 166 ▶ $usesInterfaces = 1 if $line =~ /^use Class::Interface/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^use Class::Interface/i 10 156 166
385 166 ▶ $callsInterface = 1 if $line =~ /^\&?interface\(?\)?/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\&?interface\(?\)?/ 7 159 166
386 166 ▶ $callsInterface = 1 if ( $asAbstract && $line =~ /^\&?abstract\(?\)?/ );
Branch: 1 branch
BranchTrueFalseTotal
if $asAbstract and $line =~ /^\&?abstract\(?\)?/ 3 163 166
Condition100%3/3
$asAbstract and $line =~ /^\&?abstract\(?\)?/
!ll&&!rl&&r
611023
MC/DC100%2/2
$asAbstract and $line =~ /^\&?abstract\(?\)?/
$asAbstract $line =~ /^\&?abstract\(?\)?/
Truth table100%3/3
A and B
A= $asAbstractB= $line =~ /^\&?abstract\(?\)?/
ABresult
0X0
100
111
387  
388 166 ▶ if ( $line =~ /^sub/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^sub/) 25 141 166
389 # strip of any comments
390 25 ▶ unless ( ( my $commentChar = index($line, "#") ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $commentChar = index($line, "#")) < 0) 3 22 25
391 3 $line = substr($line, 0, $commentChar);
392 }
393  
394 # trim trailing whitespace
395 25 $line =~ s/\ +$//;
396  
397 25 my ($sub) = $line =~ /sub ([^\s]+)/;
398 25 my $lineEnd = substr( $line, length($line) - 1 );
399  
400 25 ▶ if ( $lineEnd ne ";" and $lineEnd ne "}" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineEnd ne ";" and $lineEnd ne "}") 3 22 25
Condition100%3/3
$lineEnd ne ";" and $lineEnd ne "}"
!ll&&!rl&&r
1843
MC/DC100%2/2
$lineEnd ne ";" and $lineEnd ne "}"
$lineEnd ne ";" $lineEnd ne "}"
Truth table100%3/3
A and B
A= $lineEnd ne ";"B= $lineEnd ne "}"
ABresult
0X0
100
111
401 # if this is an abstract, implementations are OK
402 3 ▶ next if $asAbstract;
Branch: 1 branch
BranchTrueFalseTotal
if $asAbstract 3 0 3
403  
404 # ai. The sub has an implementation
405 0 error
406 "$interface is not a valid interface. $sub has an implementation";
407 }
408  
409 # strip any funny chars from the routine name
410 22 $sub =~ tr/\;\{\}//d;
411  
412 22 push @subs, $sub;
413 }
414 }
415  
416 10 ▶ if ( !$usesInterfaces ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($usesInterfaces) 0 10 10
417 0 error("Interface $interface does not use the interface module.");
418 }
419 10 ▶ if ( !$callsInterface ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callsInterface) 0 10 10
420 0 ▶ error( ( $asAbstract ? "Abstract" : "Interface" ) . " $interface does not load the interface magic.");
Branch: 1 branch
BranchTrueFalseTotal
$asAbstract ? : 0 0 0
421 }
422  
423 10 return @subs;
424 }
425  
426 # add a default constructor to the caller
427 11 ▶sub makeMagicConstructor {
Subroutine makeMagicConstructor: called CC=3 SCAR=22.8
Pod undocumented
428 11 ▶ return if !$Class::Interface::AUTO_CONSTRUCTOR;
Branch: 1 branch
BranchTrueFalseTotal
unless $Class::Interface::AUTO_CONSTRUCTOR 11 0 11
429  
430 0 my $caller = shift;
431  
432 0 ▶ unless ( defined &{ $caller."::new" } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$caller . '::new';}) 0 0 0
433 0 ▶ *{ $caller."::new"} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
434 0 ▶ my $class = ref($_[0]) || $_[0]; shift;
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
435 0 my $self = bless({}, $class);
436  
437 0 my %value = @_;
438 0 foreach my $field ( keys %value ) {
439 0 ▶ $self->$field( $value{$field} ) if $self->can( $field )
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($field) 0 0 0
440 }
441  
442 0 return $self
443 0 };
444 }
445 }
446  
447 # die
448 0 ▶sub error(*) {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
449 0 my $strings = join("", @_);
450  
451 0 ▶ if ( $Class::Interface::CONFESS == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Interface::CONFESS == 1) { } 0 0 0
452 0 confess $strings;
453 } else {
454 0 croak $strings;
455 }
456 }
457  
458 =pod
459  
460 =head1 MAGIC CONSTRUCTORS
461  
462 To add even more Java behaviour to perl...
463  
464 Extending or implementing classes that do not already have a constructor
465 can get one injected automaticly.
466  
467 The code for such a routine is as follows:
468  
469 sub new {
470 my $class = ref($_[0]) || $_[0]; shift;
471 my $self = bless({}, $class);
472  
473 my %value = @_;
474 foreach my $field ( keys %value ) {
475 $self->$field( $value{$field} ) if $self->can( $field )
476 }
477  
478 return $self
479 }
480  
481 In english: An object with a hashref is setup. The constructor can be called
482 like this:
483  
484 my $object = Object->new( attribute1 => "value",
485 attribute2 => [ qw(a b c)],
486 );
487  
488 if attributeX exists as an accessor routine in the object it will be set by
489 calling the actual routine.
490  
491 I would strongly advice using something like Class::AccessorMaker though...
492  
493 If you want magic constructors; set $Class::Interface::AUTO_CONSTRUCTOR to 1
494  
495 =head1 ERROR HANDLING
496  
497 If anything fails uses Carp::croak. Once you set $Class::Interface::CONFESS
498 to 1 it will spill the guts using confess.
499  
500 =head1 FAQ
501  
502 =over 4
503  
504 =item Q: Will it see the routines I create dynamicly?
505  
506 Using things like Class::AccessorMaker accessors are dynamcly created.
507 Class contracts can specify some getters to be present. Does Class::Interface
508 recognize them?
509  
510 =item A: Yes.
511  
512 The checks implements() and extends() perform happen well after use time. So
513 using Class::AccessorMaker is save. It performs it magic in use time. Any class
514 that will dynamicly create methods in use time should be usable with
515 Class::Interface.
516  
517 =back
518  
519 =head1 CAVEATS, BUGS, ETC.
520  
521 =head2 Order of annotations
522  
523 If your class extends an abstract which provides methods for an interface you
524 are implementing you must first call the &extends annotation.
525  
526 So:
527  
528 &extends('Runner');
529 &implements('Runnable');
530  
531 And not:
532  
533 &implements('Runnable');
534 &extends('Runner');
535  
536  
537 =head1 SEE ALSO
538  
539 L<Carp>, L<UNIVERSAL>
540  
541 =head1 AUTHOR
542  
543 Hartog C. de Mik
544  
545 =head1 COPYRIGHT
546  
547 (cc-sa) 2008, Hartog C. de Mik
548  
549 cc-sa : L<http://creativecommons.org/licenses/by-sa/3.0/>
550  
551 =cut
552  
553 5 1;
554  
555  
↑ Summary test-lib/Car/Factory.pm »