lib/FP/Interface.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 60.0% 12 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 6 totaltotal 82.7% 101 / 122 CC 13 SCAR 10.7
CC
13
Cov
86%
package_check_possible_interface
6.4
implemented_with_caller
4.8
require_package
3.0
CRAP 2.9
« lib/FP/Currying.pm ↑ Summary lib/FP/JSON.pm »
1 #
2 # Copyright (c) 2019-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Interface - implement an interface
13  
14 =head1 SYNOPSIS
15  
16 Also see L<FP::Interfaces>.
17  
18 {
19 package FP::Abstract::Sequence;
20 3 ▶ sub FP_Interface__method_names {
Subroutine FP_Interface__method_names: called CC=1 SCAR=0.0
Pod undocumented
21 3 qw(fold)
22 }
23 }
24  
25 {
26 package FP::Abstract::ExtendedSequence;
27 1 ▶ use base qw(FP::Abstract::Sequence);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶ sub FP_Interface__method_names {
Subroutine FP_Interface__method_names: called CC=1 SCAR=0.0
29 2 my $class = shift;
30 2 (qw(sum), $class->SUPER::FP_Interface__method_names)
31 }
32 }
33  
34 {
35 package Foo;
36  
37 sub foo { }
38 sub fold { }
39  
40 1 ▶ use FP::Interface;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 FP::Interface::implemented qw(FP::Abstract::ExtendedSequence);
42 FP::Interface::implemented qw(FP::Abstract::Pure);
43  
44 # but, the recommended way is to instead:
45 1 ▶ use FP::Interfaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 FP::Interfaces::implemented qw(FP::Abstract::ExtendedSequence
47 FP::Abstract::Pure);
48  
49 # FP::Interface*::implemented add the given arguments to @ISA
50 # and check that the methods required by those interfaces are
51 # actually implemented. It issues warnings for missing methods,
52 # in this case that 'sum' is not implemented.
53 }
54  
55 =head1 DESCRIPTION
56  
57 =head1 SEE ALSO
58  
59 L<FP::Interfaces>
60  
61 This implements: L<FP::Abstract::Interface>
62  
63 =head1 NOTE
64  
65 This is alpha software! Read the status section in the package README
66 or on the L<website|http://functional-perl.org/>.
67  
68 =cut
69  
70 package FP::Interface;
71 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 61 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 61 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 61 our @EXPORT = qw();
77 61 our @EXPORT_OK = qw(
78 package_is_populated
79 require_package
80 package_check_possible_interface);
81 61 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
82  
83 61 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
84 61 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 1597 ▶sub package_is_populated {
Subroutine package_is_populated: called CC=2 SCAR=6.9
Pod undocumented
87 1597 my ($package) = @_;
88 1597 my $pr = do {
89 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1597 *{ $package . "::" }
91 };
92 1597 ▶ %$pr ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
%$pr ? : 1404 193 1597
93 }
94  
95 1597 ▶sub require_package {
Subroutine require_package: called CC=3 SCAR=11.0
Pod undocumented
96 1597 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1597 0 1597
97 1597 my ($package) = @_;
98 1597 ▶ if (not package_is_populated $package) {
Branch: 1 branch
BranchTrueFalseTotal
if (not package_is_populated($package)) 193 1404 1597
99 193 $package =~ s|::|/|g;
100 193 $package .= ".pm";
101 193 require $package
102 }
103 }
104  
105 947 ▶sub package_check_possible_interface {
Subroutine package_check_possible_interface: called CC=6 SCAR=18.6
Pod undocumented
106 947 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 947 0 947
107 947 my ($caller, $possible_interface_package) = @_;
108 947 ▶ if (my $m = $possible_interface_package->can("FP_Interface__method_names"))
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $possible_interface_package->can('FP_Interface__method_names')) { } 926 21 947
109 {
110 926 my @missing;
111 926 for my $method (&$m($possible_interface_package)) {
112 9835 ▶ unless ($caller->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($caller->can($method)) 0 9835 9835
113 0 push @missing, $method
114 }
115 }
116 warn
117 926 ▶ "FP::Interface warning: '$caller' does not implement '$possible_interface_package' methods: @missing\n"
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 926 926
118 if @missing;
119 926 1
120 } else {
121  
122 # not an interface
123 undef
124 21 }
125 }
126  
127 652 ▶sub implemented_with_caller {
Subroutine implemented_with_caller: called CC=4 SCAR=15.7
Pod undocumented
128 652 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 652 0 652
129 652 my ($caller, $interface) = @_;
130 652 my ($caller_package, $caller_file, $caller_line) = @$caller;
131 652 require_package $interface;
132 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 652 push @{"${caller_package}::ISA"}, $interface;
134 652 ▶ package_check_possible_interface($caller_package, $interface) // do {
Condition33%1/3
package_check_possible_interface($caller_package, $interface) // do { my $suggestload = package_is_populated($interface) ? "" : qq[ (perhaps you forgot to load "$interface"?)]; die "'${interface}' does not have a 'FP_Interface__method_names' method hence is not an interface" . $suggestload . " at $caller_file line $caller_line.\n" }
l!l
65200
MC/DC0%0/2
package_check_possible_interface($caller_package, $interface) // do { my $suggestload = package_is_populated($interface) ? "" : qq[ (perhaps you forgot to load "$interface"?)]; die "'${interface}' does not have a 'FP_Interface__method_names' method hence is not an interface" . $suggestload . " at $caller_file line $caller_line.\n" }
package_check_possible_interface($caller_package, $interface) do { my $suggestload = package_is_populated($interface) ? "" : qq[ (perhaps you forgot to load "$interface"?)]; die "'${interface}' does not have a 'FP_Interface__method_names' method hence is not an interface" . $suggestload . " at $caller_file line $caller_line.\n" }
Truth table33%1/3
A // B
A= package_check_possible_interface($caller_package, $interface)B= do { my $suggestload = package_is_populated($interface) ? "" : qq[ (perhaps you forgot to load "$interface"?)]; die "'${interface}' does not have a 'FP_Interface__method_names' method hence is not an interface" . $suggestload . " at $caller_file line $caller_line.\n" }
ABresult
1X1
011
000
135 0 ▶ my $suggestload
Branch: 1 branch
BranchTrueFalseTotal
package_is_populated($interface) ? : 0 0 0
136 = package_is_populated($interface)
137 ? ""
138 : " (perhaps you forgot to load \"$interface\"?)";
139 0 die
140 "'$interface' does not have a 'FP_Interface__method_names' method hence is not an interface"
141 . $suggestload
142 . " at $caller_file line $caller_line.\n";
143 };
144 }
145  
146 # called fully qualified, i.e. FP::Interface::implemented (to avoid
147 # namespace pollution in classes)
148 2 ▶sub implemented {
Subroutine implemented: called CC=2 SCAR=7.1
Pod undocumented
149 2 ▶ @_ == 1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("FP::Interface::implemented: expecting 1 argument; use FP::Interfaces (note the s) instead") 2 0 2
150 or croak "FP::Interface::implemented: expecting 1 argument; "
151 . "use FP::Interfaces (note the s) instead";
152 2 my $caller = [caller];
153 2 implemented_with_caller($caller, $_[0])
154 }
155  
156 61 1
« lib/FP/Currying.pm ↑ Summary lib/FP/JSON.pm »