blib/lib/Method/Utils.pm

filter: Statementstmt 100% 48 / 48 Branchbran 90.0% 9 / 10 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 87.7% 79 / 90 CC 11 SCAR 9.2
CC
11
Cov
91%
__ANON__
4.1
__ANON__
4.0
maybe
3.0
CRAP 2.5
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2011-2014 -- leonerd@leonerd.org.uk
5  
6 package Method::Utils;
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.03';
12  
13 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our @EXPORT_OK = qw(
16 maybe
17 possibly
18  
19 inwardly
20 outwardly
21 );
22  
23 5 require mro;
24  
25 =head1 NAME
26  
27 C<Method::Utils> - functional-style utilities for method calls
28  
29 =cut
30  
31 =head1 SYNOPSIS
32  
33 use Method::Utils qw( maybe possibly inwardly );
34  
35 $obj->${maybe "do_thing"}(@args);
36 # equivalent to
37 # $obj->do_thing(@args) if defined $obj;
38  
39 $obj->${possibly "do_another"}(@args);
40 # equivalent to
41 # $obj->do_another(@args) if $obj->can( "do_another" );
42  
43 $obj->${inwardly "do_all_these"}();
44 # invokes the method on every subclass in 'mro' order
45  
46 =cut
47  
48 =head1 FUNCTIONS
49  
50 All of the following functions are intended to be used as method call
51 modifiers. That is, they return a C<SCALAR> reference to a C<CODE> reference
52 which allows them to be used in the following syntax
53  
54 $ball->${possibly "bounce"}( "10 metres" );
55  
56 Since the returned double-reference can be dereferenced by C<${ }> to obtain
57 the C<CODE> reference directly, it can be used to create new methods. For
58 example:
59  
60 *bounce_if_you_can = ${possibly "bounce"};
61  
62 This is especially useful for creating methods in base classes which
63 distribute across all the classes in a class heirarchy; for example
64  
65 *DESTROY = ${inwardly "COLLAPSE"};
66  
67 =cut
68  
69 =head2 maybe $method
70  
71 Invokes the named method on the object or class, if one is provided, and
72 return what it returned. If invoked on C<undef>, returns C<undef> in scalar
73 context or the empty list in list context.
74  
75 C<$method> here may also be a double-ref to a C<CODE>, such as returned by
76 the remaining utility functions given below. In this case, it will be
77 dereferenced automatically, allowing you to conveniently perform
78  
79 $obj->${maybe possibly 'method'}( @args )
80  
81 =cut
82  
83 sub maybe
84 5 ▶{
Subroutine maybe: called CC=3 SCAR=11.0
Pod documented
85 5 my $mth = shift;
86 5 ▶ $mth = $$mth if ref $mth eq "REF" and ref $$mth eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $mth eq "REF" and ref $$mth eq "CODE" 3 2 5
Condition66%2/3
ref $mth eq "REF" and ref $$mth eq "CODE"
!ll&&!rl&&r
203
MC/DC50%1/2
ref $mth eq "REF" and ref $$mth eq "CODE"
ref $mth eq "REF" ref $$mth eq "CODE"
Truth table66%2/3
A and B
A= ref $mth eq "REF"B= ref $$mth eq "CODE"
ABresult
0X0
100
111
87 5 ▶ \sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
88 5 my $self = shift;
89 5 ▶ defined $self or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self or return 3 2 5
90 3 $self->$mth( @_ );
91 5 };
92 }
93  
94 =head2 possibly $method
95  
96 Invokes the named method on the object or class and return what it returned,
97 if it exists. If the method does not exist, returns C<undef> in scalar context
98 or the empty list in list context.
99  
100 =cut
101  
102 sub possibly
103 11 ▶{
Subroutine possibly: called CC=1 SCAR=0.0
Pod documented
104 11 my $mth = shift;
105 10 ▶ \sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
106 10 my $self = shift;
107 10 ▶ return unless $self->can( $mth );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($mth) 5 5 10
108 5 $self->$mth( @_ );
109 11 };
110 }
111  
112 =head2 inwardly $method
113  
114 =head2 outwardly $method
115  
116 Invokes the named method on the object or class for I<every> class that
117 provides such a method in the C<@ISA> heirarchy, not just the first one that
118 is found. C<inwardly> searches all the classes in L<mro> order, finding the
119 class itself first and then its superclasses. C<outwardly> runs in reverse,
120 starting its search at the base-most superclass, searching upward before
121 finally ending at the class itself.
122  
123 =cut
124  
125 sub inwardly
126 2 ▶{
Subroutine inwardly: called CC=1 SCAR=0.0
Pod documented
127 2 my $mth = shift;
128 2 ▶ \sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
129 2 my $self = shift;
130 2 ▶ foreach my $class ( @{ mro::get_linear_isa( ref $self || $self ) } ) {
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
131 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 11 ▶ defined &{$class."::$mth"} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined &{$class . "::$mth";} or next 9 2 11
133 9 &{$class."::$mth"}( $self, @_ );
134 }
135 }
136 2 }
137  
138 sub outwardly
139 1 ▶{
Subroutine outwardly: called CC=1 SCAR=0.1
Pod documented
140 1 my $mth = shift;
141 1 ▶ \sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
142 1 my $self = shift;
143 1 ▶ foreach my $class ( reverse @{ mro::get_linear_isa( ref $self || $self ) } ) {
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
144 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
145 6 ▶ defined &{$class."::$mth"} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined &{$class . "::$mth";} or next 6 0 6
146 6 &{$class."::$mth"}( $self, @_ );
147 }
148 }
149 1 }
150  
151 =head1 TODO
152  
153 =over 4
154  
155 =item *
156  
157 Consider C<hopefully $method>, which would C<eval{}>-wrap the call, returning
158 C<undef>/empty if it failed.
159  
160 =item *
161  
162 Consider better ways to combine more of these. E.g. C<hopefully inwardly>
163 would C<eval{}>-wrap each subclass call. C<inwardly> without C<possibly> would
164 fail if no class provides the method.
165  
166 =back
167  
168 =cut
169  
170 =head1 SEE ALSO
171  
172 =over 4
173  
174 =item *
175  
176 L<http://shadow.cat/blog/matt-s-trout/madness-with-methods/> - Madness With Methods
177  
178 =back
179  
180 =cut
181  
182 =head1 AUTHOR
183  
184 Paul Evans <leonerd@leonerd.org.uk>
185  
186 =cut
187  
188 5 0x55AA;
↑ Summary