blib/lib/Callable.pm

filter: Statementstmt 100% 105 / 105 Branchbran 80.9% 34 / 42 Conditioncond 69.2% 18 / 26 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 20 / 20 Podpod 100% 1 / 1 totaltotal 85.6% 179 / 209 CC 31 SCAR 15.8
CC
31
Cov
91%
_validate_options
11.0
_make_scalar_handler
6.1
_first_arg
5.0
CRAP 4.9
↑ Summary
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
BranchTrueFalseTotal
if (@options and builtin::blessed($options[0]) and $options[0]->isa(__PACKAGE__)) 5 18 23
Condition66%4/6
@options and builtin::blessed($options[0]) and $options[0]->isa(__PACKAGE__)
!ll&&!rl&&r
1805
@options and builtin::blessed($options[0])
!ll&&!rl&&r
0185
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__)
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if (@options) { } 1 4 5
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
BranchTrueFalseTotal
if (@_ > 1) 9 18 27
54 9 $self->{__first_arg} = $value;
55 }
56  
57 27 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 18 9 27
58 18 ▶ return unless exists $self->{__first_arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__first_arg"} 9 9 18
59 9 return ( $self->{__first_arg} );
60 }
61  
62 9 ▶ return $self->{__first_arg} // undef;
Condition50%1/2
$self->{"__first_arg"} // undef
l!l
90
MC/DC0%0/1
$self->{"__first_arg"} // undef
$self->{"__first_arg"}
Truth table50%1/2
A
A= $self->{"__first_arg"}
Aresult
11
00
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
BranchTrueFalseTotal
unless (exists $self->{"__handler"}) 18 17 35
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
BranchTrueFalseTotal
$ref eq 'CODE' ? : 2 16 18
$ref eq 'ARRAY' ? : 7 9 16
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
BranchTrueFalseTotal
if (@args) 12 6 18
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
BranchTrueFalseTotal
unless (builtin::blessed($object)) 6 1 7
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
BranchTrueFalseTotal
if defined $garbage 0 6 6
106  
107 6 load $class;
108  
109 6 ▶ $constructor //= $DEFAULT_CLASS_CONSTRUCTOR;
Condition66%2/3
$constructor //= $Callable::DEFAULT_CLASS_CONSTRUCTOR
l!l&&r!l&&!r
150
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if @path > 2 0 9 9
124  
125 9 ▶ if ( @path == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path == 2) 2 7 9
126 2 ▶ $path[0] ||= $caller;
Condition66%2/3
$path[0] ||= $caller
l!l&&r!l&&!r
110
MC/DC0%0/2
$path[0] ||= $caller
$path[0] $caller
Truth table66%2/3
A ||= B
A= $path[0]B= $caller
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (@path == 1) 4 5 9
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
BranchTrueFalseTotal
unless $handler 0 9 9
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
l!l&&r!l&&!r
17180
MC/DC0%0/2
$caller //= caller
$caller caller
Truth table66%2/3
A //= B
A= $callerB= caller
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless @{$self->{"options"};} 0 18 18
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
BranchTrueFalseTotal
unless $ref eq "CODE" or $ref eq "ARRAY" or $ref eq "" 0 18 18
Condition83%5/6
$ref eq "CODE" or $ref eq "ARRAY" or $ref eq ""
l!l&&r!l&&!r
990
$ref eq "CODE" or $ref eq "ARRAY"
l!l&&r!l&&!r
279
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 ""
ABCresult
1XX1
01X1
0011
0000
167  
168 18 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 7 11 18
169 7 ▶ croak USAGE if @{$source} < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$source;} < 2 0 7 7
170 7 ▶ croak USAGE unless blessed $source->[0] || ref( $source->[0] ) eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source->[0]) or ref $source->[0] eq "" 0 7 7
Condition66%2/3
builtin::blessed($source->[0]) or ref $source->[0] eq ""
l!l&&r!l&&!r
160
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 ""
ABresult
1X1
011
000
171 7 ▶ croak USAGE if ref $source->[1];
Branch: 1 branch
BranchTrueFalseTotal
if ref $source->[1] 0 7 7
172 }
173 }
174  
175 2 1;
176 __END__
↑ Summary