| 1 | package Callable; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Module::Load; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use overload '&{}' => '_to_sub', '""' => '_to_string'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use constant ( USAGE => | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 'Usage: Callable->new(&|$|[object|"class"|"class->constructor", "method"])' | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | our $VERSION = "0.02"; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | our $DEFAULT_CLASS_CONSTRUCTOR = 'new'; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 23 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 23 | my ( $class, @options ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 23 | ▶ | if ( @options | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@options and builtin::blessed($options[0]) and $options[0]->isa(__PACKAGE__)
@options and builtin::blessed($options[0])
MC/DC33%1/3
@options and builtin::blessed($options[0]) and $options[0]->isa(__PACKAGE__)
@options
builtin::blessed($options[0])
$options[0]->isa(__PACKAGE__)
Truth table50%2/4
A and B and C
A= @optionsB= builtin::blessed($options[0])C= $options[0]->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | && blessed( $options[0] ) | ||||||||||||||||||||||||||||||||||||||||||
| 26 | && $options[0]->isa(__PACKAGE__) ) | ||||||||||||||||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 5 | return $options[0]->_clone( splice @options, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 18 | my $self = bless { options => \@options }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 18 | $self->_validate_options(); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 18 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | ▶ | sub _clone { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clone: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | my ( $self, @options ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | if (@options) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | unshift @options, $self->{options}->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | @options = @{ $self->{options} }; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | return bless { options => \@options }, ref($self); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 27 | ▶ | sub _first_arg { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_first_arg: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 27 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 27 | ▶ | if ( @_ > 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 9 | $self->{__first_arg} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 27 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 18 | ▶ | return unless exists $self->{__first_arg}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 9 | return ( $self->{__first_arg} ); | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 9 | ▶ | return $self->{__first_arg} // undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"__first_arg"} // undef
MC/DC0%0/1
$self->{"__first_arg"} // undef
$self->{"__first_arg"}
Truth table50%1/2
A
A= $self->{"__first_arg"}
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 35 | ▶ | sub _handler { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handler: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 35 | my ( $self, $caller ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 35 | ▶ | unless ( exists $self->{__handler} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 18 | $self->{__handler} = $self->_make_handler($caller); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 35 | return $self->{__handler}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 18 | ▶ | sub _make_handler { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_handler: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 18 | my ( $self, $caller ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 18 | my ( $source, @default_args ) = @{ $self->{options} }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 18 | my $ref = ref $source; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 18 | ▶ | my $handler = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | $ref eq 'CODE' ? $source | ||||||||||||||||||||||||||||||||||||||||||
| 83 | : ( | ||||||||||||||||||||||||||||||||||||||||||
| 84 | $ref eq 'ARRAY' ? $self->_make_object_handler( $source, $caller ) | ||||||||||||||||||||||||||||||||||||||||||
| 85 | : $self->_make_scalar_handler( $source, $caller ) | ||||||||||||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 18 | my @args = ( $self->_first_arg, @default_args ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 18 | ▶ | if (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 12 | my $inner = $handler; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 12 | ▶ | $handler = sub { $inner->( @args, @_ ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 18 | return $handler; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 7 | ▶ | sub _make_object_handler { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_object_handler: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 7 | my ( $self, $source, $caller ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 7 | my ( $object, $method, @args ) = @{$source}; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 7 | ▶ | unless ( blessed $object) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 6 | my ( $class, $constructor, $garbage ) = split /\Q->\E/, $object; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 6 | ▶ | croak "Wrong class name format: $object" if defined $garbage; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 6 | load $class; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | ▶ | $constructor //= $DEFAULT_CLASS_CONSTRUCTOR; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$constructor //= $Callable::DEFAULT_CLASS_CONSTRUCTOR
MC/DC0%0/2
$constructor //= $Callable::DEFAULT_CLASS_CONSTRUCTOR
$constructor
$Callable::DEFAULT_CLASS_CONSTRUCTOR
Truth table66%2/3
A //= B
A= $constructorB= $Callable::DEFAULT_CLASS_CONSTRUCTOR
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | $object = $class->$constructor(@args); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 7 | $self->_first_arg($object); | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 7 | return $object->can($method); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 9 | ▶ | sub _make_scalar_handler { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_scalar_handler: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 9 | my ( $self, $name, $caller ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 9 | my @path = split /\Q->\E/, $name; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 9 | ▶ | croak "Wrong subroutine name format: $name" if @path > 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 9 | ▶ | if ( @path == 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | ▶ | $path[0] ||= $caller; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$path[0] ||= $caller
MC/DC0%0/2
$path[0] ||= $caller
$path[0]
$caller
Truth table66%2/3
A ||= B
A= $path[0]B= $caller
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 2 | $self->_first_arg( $path[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | $name = join '::', @path; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 9 | @path = split /::/, $name; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 9 | ▶ | if ( @path == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 4 | unshift @path, $caller; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 9 | $name = join( '::', @path ); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 9 | my $handler = \&{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 9 | ▶ | croak "Unable to find subroutine: $name" if not $handler; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 9 | return $handler; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 17 | ▶ | sub _to_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 17 | return $self->_to_sub( scalar caller )->(); | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 35 | ▶ | sub _to_sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_sub: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 35 | my ( $self, $caller ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 35 | ▶ | $caller //= caller; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$caller //= caller
MC/DC0%0/2
$caller //= caller
$caller
caller
Truth table66%2/3
A //= B
A= $callerB= caller
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 35 | return $self->_handler($caller); | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 18 | ▶ | sub _validate_options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_options: called CC=10 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 18 | ▶ | croak USAGE unless @{ $self->{options} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 18 | my $source = $self->{options}->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 18 | my $ref = ref($source); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 18 | ▶ | croak USAGE unless $ref eq 'CODE' || $ref eq 'ARRAY' || $ref eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$ref eq "CODE" or $ref eq "ARRAY" or $ref eq ""
$ref eq "CODE" or $ref eq "ARRAY"
MC/DC0%0/3
$ref eq "CODE" or $ref eq "ARRAY" or $ref eq ""
$ref eq "CODE"
$ref eq "ARRAY"
$ref eq ""
Truth table75%3/4
A or B or C
A= $ref eq "CODE"B= $ref eq "ARRAY"C= $ref eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 18 | ▶ | if ( $ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 7 | ▶ | croak USAGE if @{$source} < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | ▶ | croak USAGE unless blessed $source->[0] || ref( $source->[0] ) eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($source->[0]) or ref $source->[0] eq ""
MC/DC0%0/2
builtin::blessed($source->[0]) or ref $source->[0] eq ""
builtin::blessed($source->[0])
ref $source->[0] eq ""
Truth table66%2/3
A or B
A= builtin::blessed($source->[0])B= ref $source->[0] eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | ▶ | croak USAGE if ref $source->[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||||||||||||||||