blib/lib/Callable.pm

filter: Statementstmt 100% 101 / 101 Branchbran 80.9% 34 / 42 Conditioncond 69.2% 18 / 26 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 85.2% 174 / 204 CC 31 SCAR 16.0
CC
31
Cov
91%
_validate_options
11.0
_make_scalar_handler
6.1
_first_arg
5.0
CRAP 4.9
↑ Summary Build »
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
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
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
BranchTrueFalseTotal
if (@options) { } 1 4 5
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
BranchTrueFalseTotal
if (@_ > 1) 9 18 27
53 9 $self->{__first_arg} = $value;
54 }
55  
56 27 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 18 9 27
57 18 ▶ return unless exists $self->{__first_arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__first_arg"} 9 9 18
58 9 return ( $self->{__first_arg} );
59 }
60  
61 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
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
BranchTrueFalseTotal
unless (exists $self->{"__handler"}) 18 17 35
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
BranchTrueFalseTotal
$ref eq 'CODE' ? : 2 16 18
$ref eq 'ARRAY' ? : 7 9 16
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
BranchTrueFalseTotal
if (@args) 12 6 18
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
BranchTrueFalseTotal
unless (builtin::blessed($object)) 6 1 7
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
BranchTrueFalseTotal
if defined $garbage 0 6 6
105  
106 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
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
BranchTrueFalseTotal
if @path > 2 0 9 9
121  
122 9 ▶ if ( @path == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path == 2) 2 7 9
123 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
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
BranchTrueFalseTotal
if (@path == 1) 4 5 9
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
BranchTrueFalseTotal
unless $handler 0 9 9
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
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
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
BranchTrueFalseTotal
unless @{$self->{"options"};} 0 18 18
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
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
164  
165 18 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 7 11 18
166 7 ▶ croak USAGE if @{$source} < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$source;} < 2 0 7 7
167 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
168 7 ▶ croak USAGE if ref $source->[1];
Branch: 1 branch
BranchTrueFalseTotal
if ref $source->[1] 0 7 7
169 }
170 }
171  
172 2 1;
173 __END__
↑ Summary Build »