| 1 | |||||||||||||||||||
| 2 | package Class::Interfaces; | ||||||||||||||||||
| 3 | |||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 2 | our $VERSION = '0.04'; | |||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 11 | ▶ | sub import { | ||||||||||||||||
|
Subroutine
import: called CC=15 SCAR=27.1
| |||||||||||||||||||
| 10 | 11 | my $class = shift; | |||||||||||||||||
| 11 | 11 | my %interfaces = @_; | |||||||||||||||||
| 12 | 11 | foreach my $interface (keys %interfaces) { | |||||||||||||||||
| 13 | # build the interface | ||||||||||||||||||
| 14 | 16 | my (@methods, @subclasses); | |||||||||||||||||
| 15 | 16 | ▶ | if (ref($interfaces{$interface}) eq 'HASH') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 16 | 5 | my $interface_spec = $interfaces{$interface}; | |||||||||||||||||
| 17 | # if we have an isa | ||||||||||||||||||
| 18 | 5 | ▶ | if (exists ${$interface_spec}{isa}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 19 | # if is an array (multiple inheritance) | ||||||||||||||||||
| 20 | 3 | ▶ | if (ref($interface_spec->{isa}) eq 'ARRAY') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 21 | 1 | @subclasses = @{$interface_spec->{isa}}; | |||||||||||||||||
| 22 | } | ||||||||||||||||||
| 23 | else { | ||||||||||||||||||
| 24 | # if its another kind of ref, its an error | ||||||||||||||||||
| 25 | 2 | ▶ | (!ref($interface_spec->{isa})) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 26 | || $class->_error_handler("Interface ($interface) isa list must be an array reference"); | ||||||||||||||||||
| 27 | # otherwise its just a single item | ||||||||||||||||||
| 28 | 1 | @subclasses = $interface_spec->{isa}; | |||||||||||||||||
| 29 | } | ||||||||||||||||||
| 30 | } | ||||||||||||||||||
| 31 | 4 | ▶ | if (exists ${$interface_spec}{methods}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 32 | 3 | ▶ | (ref($interface_spec->{methods}) eq 'ARRAY') | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 33 | || $class->_error_handler("Method list for Interface ($interface) must be an array reference"); | ||||||||||||||||||
| 34 | 2 | @methods = @{$interface_spec->{methods}}; | |||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | } | ||||||||||||||||||
| 37 | elsif (ref($interfaces{$interface}) eq 'ARRAY') { | ||||||||||||||||||
| 38 | 9 | @methods = @{$interfaces{$interface}}; | |||||||||||||||||
| 39 | } | ||||||||||||||||||
| 40 | elsif (!defined($interfaces{$interface})) { | ||||||||||||||||||
| 41 | # allow undefined here, this indicates an empty | ||||||||||||||||||
| 42 | # interface, sometimes called a marker interface | ||||||||||||||||||
| 43 | ; | ||||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | else { | ||||||||||||||||||
| 46 | 1 | $class->_error_handler("Cannot use a " . $interfaces{$interface} . " to build an interface"); | |||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | # now create the interfaces | ||||||||||||||||||
| 49 | 13 | my $package = $class->_build_interface_package($interface, @subclasses); | |||||||||||||||||
| 50 | 13 | ▶ | eval $package; | ||||||||||||||||
|
Subroutine
other_method: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 51 | 13 | ▶ | $class->_error_handler("Could not create Interface ($interface) because", $@) if $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | 11 | eval { | |||||||||||||||||
| 53 | 11 | my $method_stub = $class->can('_method_stub'); | |||||||||||||||||
| 54 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 55 | # without at least this VERSION declaration | ||||||||||||||||||
| 56 | # a Marker interface will not work with | ||||||||||||||||||
| 57 | # 'use base' basically it would complain | ||||||||||||||||||
| 58 | # that the package is empty. | ||||||||||||||||||
| 59 | # we only assign this if the VERSION is already | ||||||||||||||||||
| 60 | # empty too, so we don't step on any customizations | ||||||||||||||||||
| 61 | # done in subclasses. | ||||||||||||||||||
| 62 | 11 | ▶ | ${"${interface}::"}{VERSION} ||= -1; | ||||||||||||||||
|
Condition50%1/2
do {
"${interface}::"
}->{"VERSION"} ||= -1
MC/DC0%0/1
do {
"${interface}::"
}->{"VERSION"} ||= -1
do {
"${interface}::"
}->{"VERSION"}
Truth table50%1/2
A
A= do {
"${interface}::"
}->{"VERSION"}
| |||||||||||||||||||
| 63 | # now we create all our methods :) | ||||||||||||||||||
| 64 | 11 | foreach my $method (@methods) { | |||||||||||||||||
| 65 | 12 | ▶ | ($method !~ /^(BEGIN|INIT|CHECK|END|DESTORY|AUTOLOAD|import|bootstrap)$/) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 66 | || $class->_error_handler("Cannot create an interface using reserved perl methods"); | ||||||||||||||||||
| 67 | 11 | *{"${interface}::${method}"} = $method_stub; | |||||||||||||||||
| 68 | } | ||||||||||||||||||
| 69 | }; | ||||||||||||||||||
| 70 | 11 | ▶ | $class->_error_handler("Could not create sub methods for Interface ($interface) because", $@) if $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 13 | ▶ | sub _build_interface_package { | ||||||||||||||||
|
Subroutine
_build_interface_package: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 75 | 13 | my ($class, $interface, @subclasses) = @_; | |||||||||||||||||
| 76 | 13 | my $package = "package $interface;"; | |||||||||||||||||
| 77 | 13 | ▶ | $package .= "\@${interface}::ISA = qw(" . (join " " => @subclasses) . ");" if @subclasses; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 78 | 13 | return $package; | |||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | 6 | ▶ | sub _error_handler { | ||||||||||||||||
|
Subroutine
_error_handler: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 82 | 6 | my ($class, $message, $sub_exception) = @_; | |||||||||||||||||
| 83 | 6 | ▶ | die "$message : $sub_exception" if $sub_exception; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | 4 | die "$message"; | |||||||||||||||||
| 85 | } | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | 3 | ▶ | sub _method_stub { die "Method Not Implemented" } | ||||||||||||||||
|
Subroutine
_method_stub: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 2 | 1; | |||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||