Tiny/Base.pm

filter: Statementstmt 95.2% 60 / 63 Branchbran 64.2% 9 / 14 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 92.8% 13 / 14 Podpod 50.0% 2 / 4 totaltotal 80.9% 89 / 110 CC 11 SCAR 9.1
CC
11
Cov
86%
_get_call_stack
4.0
_get_strval
3.8
_check_overload
3.0
CRAP 2.5
↑ Summary Tiny.pm »
1 package X::Tiny::Base;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 my %CALL_STACK;
7  
8 my %PROPAGATIONS;
9  
10 =encoding utf-8
11  
12 =head1 NAME
13  
14 X::Tiny::Base - super-light exception base class
15  
16 =head1 SYNOPSIS
17  
18 package My::Module::X::Base;
19  
20 use parent qw( X::Tiny::Base );
21  
22 sub _new {
23 my ($class, @args) = @_;
24  
25 ...
26 }
27  
28 sub get {
29 my ($self, $attr_name) = @_;
30  
31 ...
32 }
33  
34 sub to_string { ... }
35  
36 #If you override this, be sure also to call the base method.
37 sub DESTROY {
38 my ($self) = @_;
39  
40 ...
41  
42 #vv This. Be sure to do this in your override method.
43 $self->SUPER::DESTROY();
44 }
45  
46 =head1 DESCRIPTION
47  
48 This base class is meant for you to subclass into your distribution’s own
49 exception base class (e.g., C<My::Module::X::Base>); you should then
50 subclass that base class for your distribution’s specific exception classes
51 (e.g., C<My::Module::X::BadInput>).
52  
53 C<X::Tiny::Base>, then, serves two functions:
54  
55 =over
56  
57 =item 1) It is a useful set of defaults for overridable methods.
58  
59 =item 2) Framework handling of L<overload> stringification behavior,
60 e.g., when an uncaught exception is printed.
61  
62 =back
63  
64 That stringification’s precise formatting is not defined; however, it
65 will always include:
66  
67 =over
68  
69 =item * A stack trace
70  
71 =item * Propagations
72  
73 =back
74  
75 There is currently no access provided in code to these; if that’s something
76 you’d like to have, let me know.
77  
78 B<NOTE:> The overload stringification doesn’t seem to work as implemented in
79 Perl 5.8 or earlier. Perl 5.8 went end-of-life on 14 December 2008. Yeah.
80  
81 =head1 SUBCLASS INTERFACE
82  
83 The default behaviors seem pretty usable and desirable to me, but there may
84 be circumstances where someone wants other behaviors. Toward that end,
85 the following methods are meant to be overridden in subclasses:
86  
87 =head2 I<CLASS>->OVERLOAD()
88  
89 Returns a boolean to indicate whether this exception class should load
90 L<overload> as part of creating exceptions. If you don’t want the
91 memory overhead of L<overload>, then make this return 0. It returns 1
92 by default.
93  
94 You might also make this 0 if, for example, you want to handle the
95 L<overload> behavior yourself. (But at that point, why use X::Tiny??)
96  
97 =cut
98  
99 2 ▶use constant OVERLOAD => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 =head2 I<CLASS>->_new( MESSAGE, KEY1 => VALUE1, .. )
102  
103 The main constructor. Whatever args this accepts are the args that
104 you should use to create exceptions via your L<X::Tiny> subclass’s
105 C<create()> method. You’re free to design whatever internal representation
106 you want for your class: hash reference, array reference, etc.
107  
108 The default implementation accepts a string message and, optionally, a
109 list of key/value pairs. It is useful that subclasses of your base class
110 define their own MESSAGE, so all you’ll pass in is a specific piece of
111 information about this instance—e.g., an error code, a parameter name, etc.
112  
113 =cut
114  
115 sub _new {
116 5 ▶ my ( $class, $string, %attrs ) = @_;
Subroutine _new: called CC=1 SCAR=0.0
117  
118 5 return bless [ $string, \%attrs ], $class;
119 }
120  
121 =head2 I<OBJ>->get( ATTRIBUTE_NAME )
122  
123 Retrieves the value of an attribute.
124  
125 =cut
126  
127 sub get {
128 0 ▶ my ( $self, $attr ) = @_;
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
129  
130 #Do we need to clone this? Could JSON suffice, or do we need Clone?
131 0 return $self->[1]{$attr};
132 }
133  
134 =head2 I<OBJ>->to_string()
135  
136 Creates a simple string representation of your exception. The default
137 implementation contains the class and the MESSAGE given on instantiation.
138  
139 This method’s return value should B<NOT> include a strack trace;
140 L<X::Tiny::Base>’s internals handle that one for you.
141  
142 =cut
143  
144 sub to_string {
145 6 ▶ my ($self) = @_;
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
146  
147 6 return sprintf '%s: %s', ref($self), $self->[0];
148 }
149  
150 #----------------------------------------------------------------------
151  
152 =head1 DESTRUCTOR METHODS
153  
154 If you define your own C<DESTROY()> method, make sure you also call
155 C<SUPER::DESTROY()>, or else you’ll get memory leaks as L<X::Tiny::Base>’s
156 internal tracking of object properties will never be cleared out.
157  
158 =cut
159  
160 sub DESTROY {
161 5 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=1 SCAR=0.0
162  
163 5 delete $CALL_STACK{$self->_get_strval()};
164 5 delete $PROPAGATIONS{$self->_get_strval()};
165  
166 5 return;
167 }
168  
169 #----------------------------------------------------------------------
170  
171 sub new {
172 5 ▶ my ($class, @args) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
173  
174 5 ▶ $class->_check_overload() if $class->OVERLOAD();
Branch: 1 branch
BranchTrueFalseTotal
if $class->OVERLOAD 5 0 5
175  
176 5 my $self = $class->_new(@args);
177  
178 5 $CALL_STACK{$self->_get_strval()} = [ _get_call_stack(2) ];
179  
180 5 return $self;
181 }
182  
183 #----------------------------------------------------------------------
184  
185 sub PROPAGATE {
186 1 ▶ my ($self, $file, $line) = @_;
Subroutine PROPAGATE: called CC=1 SCAR=0.0
Pod undocumented
187  
188 1 push @{ $PROPAGATIONS{$self->_get_strval()} }, [ $file, $line ];
189  
190 1 return $self;
191 }
192  
193 2 my %_OVERLOADED;
194  
195 sub _check_overload {
196 5 ▶ my ( $class, $str ) = @_;
Subroutine _check_overload: called CC=3 SCAR=11.1
197  
198 #cf. eval_bug.readme
199 5 my $eval_err = $@;
200  
201 2 ▶ $_OVERLOADED{$class} ||= eval qq{
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition66%2/3
$_OVERLOADED{$class} ||= eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
l!l&&r!l&&!r
320
MC/DC0%0/2
$_OVERLOADED{$class} ||= eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
$_OVERLOADED{$class} eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
Truth table66%2/3
A ||= B
A= $_OVERLOADED{$class}B= eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
ABresult
1X1
011
000
202 package $class;
203 use overload (q<""> => __PACKAGE__->can('__spew'));
204 1;
205 };
206  
207 #Should never happen as long as overload.pm is available.
208 5 ▶ warn if !$_OVERLOADED{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless $_OVERLOADED{$class} 0 5 5
209  
210 5 $@ = $eval_err;
211  
212 5 return;
213 }
214  
215 sub _get_strval {
216 29 ▶ my ($self) = @_;
Subroutine _get_strval: called CC=3 SCAR=13.3
217  
218 29 ▶ if ( overload->can('Overloaded') && overload::Overloaded($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("overload"->can("Overloaded") and overload::Overloaded($self)) 29 0 29
Condition33%1/3
"overload"->can("Overloaded") and overload::Overloaded($self)
!ll&&!rl&&r
0029
MC/DC0%0/2
"overload"->can("Overloaded") and overload::Overloaded($self)
"overload"->can("Overloaded") overload::Overloaded($self)
Truth table33%1/3
A and B
A= "overload"->can("Overloaded")B= overload::Overloaded($self)
ABresult
0X0
100
111
219 29 return overload::StrVal($self);
220 }
221  
222 0 return q<> . $self;
223 }
224  
225 sub _get_call_stack {
226 5 ▶ my ($level) = @_;
Subroutine _get_call_stack: called CC=4 SCAR=14.0
227  
228 5 my @stack;
229  
230 5 ▶ while ( my @call = (caller $level)[3, 1, 2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@call) = (caller $level)[3, 1, 2]) 12 5 17
231 12 my ($pkg) = ($call[0] =~ m<(.+)::>);
232  
233 12 ▶ if (!$pkg || !$pkg->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg and $pkg->isa(__PACKAGE__)) 12 0 12
Condition66%2/3
$pkg and $pkg->isa(__PACKAGE__)
!ll&&!rl&&r
2100
MC/DC0%0/2
$pkg and $pkg->isa(__PACKAGE__)
$pkg $pkg->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= $pkgB= $pkg->isa(__PACKAGE__)
ABresult
0X0
100
111
234 12 push @stack, \@call;
235 }
236  
237 12 $level++;
238 }
239  
240 5 return @stack;
241 }
242  
243 sub __spew {
244 6 ▶ my ($self) = @_;
Subroutine __spew: called CC=3 SCAR=11.0
245  
246 6 my $spew = $self->to_string();
247  
248 6 ▶ if ( rindex($spew, $/) != (length($spew) - length($/)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($spew, $/) != length($spew) - length($/)) 6 0 6
249 6 $spew .= $/ . join( q<>, map { "\t==> $_->[0] (called in $_->[1] at line $_->[2])$/" } @{ $CALL_STACK{$self->_get_strval()} } );
250 }
251  
252 6 ▶ if ( $PROPAGATIONS{ $self->_get_strval() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PROPAGATIONS{$self->_get_strval}) 1 5 6
253 1 $spew .= join( q<>, map { "\t...propagated at $_->[0], line $_->[1]$/" } @{ $PROPAGATIONS{$self->_get_strval()} } );
254 }
255  
256 6 return $spew;
257 }
258  
259 2 1;
↑ Summary Tiny.pm »