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