lib/Badger/Base.pm

filter: Statementstmt 94.3% 216 / 229 Branchbran 78.5% 77 / 98 Conditioncond 60.5% 72 / 119 MC/DCmcdc 22.0% 15 / 68 Subroutinesub 88.8% 32 / 36 Podpod 100% 20 / 20 totaltotal 75.7% 432 / 570 CC 92 SCAR 19.9
CC
92
Cov
82%
exception
12.6
message
11.6
throw
11.3
CRAP 7.3
« lib/Badger/Factory.pm ↑ Summary blib/lib/Badger/Data/Type/Class.pm »
1 #========================================================================
2 #
3 # Badger::Base
4 #
5 # DESCRIPTION
6 # Base class module implementing common functionality for various
7 # other Badger modules.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 #========================================================================
13  
14 package Badger::Base;
15  
16 use Badger::Class
17 70 version => 0.01,
18 debug => 0,
19 constants => 'CODE HASH ARRAY BLANK SPACE PKG REFS ONCE WARN NONE',
20 import => 'class classes',
21 utils => 'blessed reftype xprintf',
22 words => 'ID EXCEPTION THROWS ERROR DECLINED before after',
23 constant => {
24 base_id => 'Badger', # stripped from class name to make id
25 TRIAL => 'Badger::Base::Trial',
26 70 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 70 ▶use Badger::Exception; # TODO: autoload
Subroutine BEGIN: called CC=1 SCAR=0.0
29 70 ▶use Badger::Debug 'debug debug_up dump_data_inline dump_data dump_list dump_hash';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 70 ▶our $EXCEPTION = 'Badger::Exception' unless defined $EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Base::EXCEPTION 70 0 70
32 70 our $ON_WARN = WARN;
33 70 our $MESSAGES = {
34 not_found => '%s not found: %s',
35 not_found_in => '%s not found in %s',
36 not_implemented => '%s is not implemented %s',
37 no_component => 'No %s component defined',
38 bad_method => "Invalid method '%s' called on %s at %s line %s",
39 invalid => 'Invalid %s specified: %s',
40 unexpected => 'Invalid %s specified: %s (expected a %s)',
41 missing_to => 'No %s specified to %s',
42 missing => 'No %s specified',
43 todo => '%s is TODO %s',
44 at_line => '%s at line %s',
45 at_file_line => '%s in %s at line %s',
46 };
47  
48  
49 sub new {
50 456 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.7
Pod documented
51  
52 # install warning handling for odd number of parameters when DEBUG enabled
53 local $SIG{__WARN__} = sub {
54 0 ▶ Badger::Utils::odd_params(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 456 } if DEBUG;
56  
57 456 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 102 354 456
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
194160102
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
58 456 ▶ my $self = bless { }, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
04560
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
59 456 $self = $self->init($args);
60  
61 # be careful to account for object that overload the boolean comparison
62 # operator and may return false to a simple truth test.
63 453 ▶ return defined $self
Branch: 1 branch
BranchTrueFalseTotal
defined $self ? : 453 0 453
64 ? $self
65 : $self->error("init() method failed\n");
66 }
67  
68 sub init {
69 60 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
70 # default action is to store reference to entire configuration so
71 # that methods can examine it later if they need to
72 60 $self->{ config } = shift;
73 60 return $self;
74 }
75  
76 sub warn {
77 8 ▶ my $self = shift;
Subroutine warn: called CC=4 SCAR=15.0
Pod documented
78 8 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 8 8
79  
80 8 my $message = join(BLANK, @_);
81 8 my $handlers = $self->on_warn;
82  
83 8 $self->debug("dispatching handlers for warn: ", $self->dump_data_inline($handlers), "\n") if DEBUG;
84 8 ▶ $self->_dispatch_handlers( warn => $handlers => $message )
Branch: 1 branch
BranchTrueFalseTotal
if $handlers and @$handlers 8 0 8
Condition33%1/3
$handlers and @$handlers
!ll&&!rl&&r
008
MC/DC0%0/2
$handlers and @$handlers
$handlers @$handlers
Truth table33%1/3
A and B
A= $handlersB= @$handlers
ABresult
0X0
100
111
85 if $handlers && @$handlers;
86  
87 # Warning is usually raised by the last handler in the chain which
88 # defaults to 'warn', so it's OK to just drop out here.
89 }
90  
91 sub error {
92 71 ▶ my $self = shift;
Subroutine error: called CC=10 SCAR=23.1
Pod documented
93 71 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
6380
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
94 71 ▶ my $type = reftype $self || BLANK;
Condition100%2/2
builtin::reftype($self) || ''
l!l
638
MC/DC100%1/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table100%2/2
A
A= builtin::reftype($self)
Aresult
11
00
95 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 71 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 58 13 71
elsif ($type eq 'HASH') { } 11 2 13
99 # don't stringify objects passed as argument
100 58 ▶ my $message = ref $_[0] ? shift : join(BLANK, map { defined($_) ? $_ : BLANK } @_);
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] ? : 0 58 58
defined $_ ? : 63 0 63
101 58 my $handlers = $self->on_error;
102  
103 # set package variable
104 58 ${ $class.PKG.ERROR } = $message;
105 58 ${ $class.PKG.DECLINED } = 0;
106  
107 58 ▶ if ($type eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 52 6 58
108 # set ERROR and DECLINED items in object
109 52 $self->{ ERROR } = $message;
110 52 $self->{ DECLINED } = 0;
111 }
112  
113 58 ▶ ($message) = $self->_dispatch_handlers( error => $handlers => $message )
Branch: 1 branch
BranchTrueFalseTotal
if $handlers and @$handlers 2 56 58
Condition66%2/3
$handlers and @$handlers
!ll&&!rl&&r
0562
MC/DC50%1/2
$handlers and @$handlers
$handlers @$handlers
Truth table66%2/3
A and B
A= $handlersB= @$handlers
ABresult
0X0
100
111
114 if $handlers && @$handlers;
115  
116 58 $self->throw($message);
117 }
118 elsif ($type eq HASH) {
119 11 return $self->{ ERROR };
120 }
121 else {
122 2 return ${ $class.PKG.ERROR };
123 }
124 # not reached
125 }
126  
127 sub decline {
128 37 ▶ my $self = shift;
Subroutine decline: called CC=5 SCAR=16.4
Pod documented
129 37 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
3700
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
130 37 ▶ my $type = reftype $self || BLANK;
Condition50%1/2
builtin::reftype($self) || ''
l!l
370
MC/DC0%0/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table50%1/2
A
A= builtin::reftype($self)
Aresult
11
00
131 37 ▶ my $reason = @_ == 1 ? shift : join(BLANK, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 37 0 37
132 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 37 ${ $class.PKG.ERROR } = $reason;
136 37 ${ $class.PKG.DECLINED } = 1;
137  
138 37 ▶ if ($type eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 37 0 37
139 37 $self->{ ERROR } = $reason;
140 37 $self->{ DECLINED } = 1;
141 }
142  
143 37 return undef;
144 }
145  
146 sub declined {
147 2 ▶ my $self = shift;
Subroutine declined: called CC=4 SCAR=15.2
Pod documented
148 2 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
149 2 ▶ my $type = reftype $self || BLANK;
Condition50%1/2
builtin::reftype($self) || ''
l!l
20
MC/DC0%0/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table50%1/2
A
A= builtin::reftype($self)
Aresult
11
00
150 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 return ($type eq HASH)
152 ? $self->{ DECLINED }
153 2 ▶ : ${ $class.PKG.DECLINED };
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' ? : 2 0 2
154 }
155  
156 sub reason {
157 27 ▶ my $self = shift;
Subroutine reason: called CC=4 SCAR=15.2
Pod documented
158 27 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
2700
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
159 27 ▶ my $type = reftype $self || BLANK;
Condition50%1/2
builtin::reftype($self) || ''
l!l
270
MC/DC0%0/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table50%1/2
A
A= builtin::reftype($self)
Aresult
11
00
160 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 return $type eq HASH
162 ? $self->{ ERROR }
163 27 ▶ : ${ $class.PKG.ERROR };
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' ? : 27 0 27
164 }
165  
166 sub throw {
167 61 ▶ my $self = shift;
Subroutine throw: called CC=10 SCAR=24.3
Pod documented
168 61 my $type = shift;
169 61 my $emod = $self->exception;
170 61 my $e;
171  
172 # TODO: grok file/line/sub from caller and add to exceptions
173  
174 61 ▶ if (! @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_) { } 58 3 61
175 # single argument can be an exception object or an error message
176 # which is given whatever type is returned by throws()
177 #
178 # throw($exception)
179 # throw($info)
180  
181 58 ▶ if (blessed $type && $type->isa($emod)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($type) and $type->isa($emod)) { } 0 58 58
Condition33%1/3
builtin::blessed($type) and $type->isa($emod)
!ll&&!rl&&r
5800
MC/DC0%0/2
builtin::blessed($type) and $type->isa($emod)
builtin::blessed($type) $type->isa($emod)
Truth table33%1/3
A and B
A= builtin::blessed($type)B= $type->isa($emod)
ABresult
0X0
100
111
182 0 $self->debug("returning exception object: ", ref $type, " => [$type]\n") if DEBUG;
183 0 $e = $type;
184 }
185 else {
186 58 $self->debug("creating new exception object chain: info => $type\n") if DEBUG;
187 58 $e = $emod->new( type => $self->throws, info => $type );
188 }
189 }
190 else {
191 # Next argument can also be an exception object (e.g. when chaining
192 # exceptions) or a regular info message. In the first case, we don't
193 # re-throw the exception if it's already of the correct $type (but we
194 # do if any extra arguments are provided)
195 #
196 # throw($type, $exception)
197 # throw($type, $info)
198  
199 3 my $info = shift;
200  
201 3 ▶ if (! @_ && blessed $info && $info->isa($emod) && $info->type eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ and builtin::blessed($info) and $info->isa($emod) and $info->type eq $type) { } 1 2 3
Condition77%7/9
not @_ and builtin::blessed($info) and $info->isa($emod) and $info->type eq $type
!ll&&!rl&&r
111
not @_ and builtin::blessed($info) and $info->isa($emod)
!ll&&!rl&&r
102
not @_ and builtin::blessed($info)
!ll&&!rl&&r
012
MC/DC50%2/4
not @_ and builtin::blessed($info) and $info->isa($emod) and $info->type eq $type
not @_ builtin::blessed($info) $info->isa($emod) $info->type eq $type
Truth table60%3/5
not(A) and B and C and D
A= not @_B= builtin::blessed($info)C= $info->isa($emod)D= $info->type eq $type
ABCDresult
0XXX0
10XX0
110X0
11100
11111
202 # second argument is already an exception of type $type
203 1 $e = $info;
204 }
205 else {
206 2 ▶ my $config = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 0 2 2
Condition33%1/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
207 # construct a new exception from $type and $info fields
208 2 $config->{ type } = $type;
209 2 $config->{ info } = $info;
210 2 $self->debug("creating new exception object: ", $self->dump_hash($config), "\n") if DEBUG;
211 2 $e = $emod->new($config);
212 }
213 }
214 61 $e->throw;
215 }
216  
217 sub try {
218 294 ▶ my $self = shift;
Subroutine try: called CC=5 SCAR=16.1
Pod documented
219 294 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 16 278 294
220 16 my $method = shift;
221 16 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 15 16
222 1 my @result = eval { $self->$method(@_) };
223 1 ▶ $self->decline($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
224 1 return @result;
225 }
226 else {
227 15 my $result = eval { $self->$method(@_) };
228 15 ▶ $self->decline($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 14 1 15
229 15 return $result;
230 }
231 }
232 else {
233 278 return TRIAL->_bind_($self);
234 }
235 }
236  
237 sub catch {
238 # this depends on some code in Badger::Exception which I haven't
239 # written yet...
240 0 ▶ shift->todo;
Subroutine catch: not called CC=1 SCAR=6.9
Pod documented
241 }
242  
243 sub throws {
244 65 ▶ my $self = shift;
Subroutine throws: called CC=9 SCAR=22.0
Pod documented
245 65 ▶ my $type = reftype $self || BLANK;
Condition100%2/2
builtin::reftype($self) || ''
l!l
5213
MC/DC100%1/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table100%2/2
A
A= builtin::reftype($self)
Aresult
11
00
246 65 my $class = class($self);
247 65 my $throws;
248  
249 65 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 4 61 65
elsif ($type eq 'HASH') { } 51 10 61
250 # hash objects store exception type in $self->{ THROWS }, anything
251 # else (classes and non-hash objects) use the $THROWS package var
252 $throws = $type eq HASH
253 ? ($self->{ THROWS } = shift)
254 4 ▶ : $class->var(THROWS, shift);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' ? : 1 3 4
255 }
256 elsif ($type eq HASH) {
257 # we also look in $self->{ config } to see if a 'throws' was
258 # specified as a constructor argument.
259 $throws = $self->{ THROWS }
260 ||= $self->{ config }
261 51 ▶ && $self->{ config }->{ throws };
Condition83%5/6
$self->{"THROWS"} ||= $self->{"config"} && $self->{"config"}{"throws"}
l!l&&r!l&&!r
1347
$self->{'config'} && $self->{'config'}{'throws'}
!ll&&!rl&&r
0473
MC/DC0%0/3
$self->{"THROWS"} ||= $self->{"config"} && $self->{"config"}{"throws"}
$self->{"THROWS"} $self->{'config'} $self->{'config'}{'throws'}
Truth table0%0/4
A ||= B && C
A= $self->{"THROWS"}B= $self->{'config'}C= $self->{'config'}{'throws'}
ABCresult
1XX1
0111
00X0
0100
262 }
263  
264 # fall back on looking for any package variable in class / base classes
265 65 ▶ return $throws
Condition83%5/6
$throws || $class->any_var('THROWS') || $class->id
l!l&&r!l&&!r
17480
$throws or $class->any_var("THROWS")
l!l&&r!l&&!r
8948
MC/DC0%0/3
$throws || $class->any_var('THROWS') || $class->id
$throws $class->any_var("THROWS") $class->id
Truth table75%3/4
A or B || C
A= $throwsB= $class->any_var("THROWS")C= $class->id
ABCresult
1XX1
01X1
0011
0000
266 || $class->any_var(THROWS)
267 || $class->id;
268 }
269  
270 sub exception {
271 61 ▶ my $self = shift;
Subroutine exception: called CC=8 SCAR=25.4
Pod documented
272 61 ▶ my $type = reftype $self || BLANK;
Condition100%2/2
builtin::reftype($self) || ''
l!l
556
MC/DC100%1/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table100%2/2
A
A= builtin::reftype($self)
Aresult
11
00
273 61 my $emod;
274  
275 # TODO: Move this into Template::Class. It's so rare that you want to
276 # set an exception type this way. Then we can have throw() pass the $type
277 # to exception() and allow subclasses to make a decision about what kind
278 # of exception to return based on the $type.
279 61 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 61 61
elsif ($type eq 'HASH') { } 55 6 61
280 # as per throws() above, we have to be careful to only treat $self
281 # like a hash when it is a hash-based object
282 $emod = $type eq HASH
283 ? ($self->{ EXCEPTION } = shift)
284 0 ▶ : class($self)->var(EXCEPTION, shift);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' ? : 0 0 0
285 }
286 elsif ($type eq HASH) {
287 $emod = $self->{ EXCEPTION }
288 ||= $self->{ config }
289 55 ▶ && $self->{ config }->{ exception };
Condition33%2/6
$self->{"EXCEPTION"} ||= $self->{"config"} && $self->{"config"}{"exception"}
l!l&&r!l&&!r
0055
$self->{'config'} && $self->{'config'}{'exception'}
!ll&&!rl&&r
0550
MC/DC0%0/3
$self->{"EXCEPTION"} ||= $self->{"config"} && $self->{"config"}{"exception"}
$self->{"EXCEPTION"} $self->{'config'} $self->{'config'}{'exception'}
Truth table0%0/4
A ||= B && C
A= $self->{"EXCEPTION"}B= $self->{'config'}C= $self->{'config'}{'exception'}
ABCresult
1XX1
0111
00X0
0100
290 }
291 61 ▶ return $emod
Condition33%1/3
$emod || Badger::Base::class($self)->any_var('EXCEPTION')
l!l&&r!l&&!r
0610
MC/DC0%0/2
$emod || Badger::Base::class($self)->any_var('EXCEPTION')
$emod Badger::Base::class($self)->any_var('EXCEPTION')
Truth table33%1/3
A || B
A= $emodB= Badger::Base::class($self)->any_var('EXCEPTION')
ABresult
1X1
011
000
292 || class($self)->any_var(EXCEPTION);
293 }
294  
295 sub fatal {
296 3 ▶ my $self = shift;
Subroutine fatal: called CC=4 SCAR=13.9
Pod documented
297 3 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
210
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
298 3 my $error = join(BLANK, @_);
299 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
300  
301 # set package variable
302 3 ${ $class.PKG.ERROR } = $error;
303  
304 3 ▶ if (ref $self && reftype $self eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and builtin::reftype($self) eq "HASH") 2 1 3
Condition66%2/3
ref $self and builtin::reftype($self) eq "HASH"
!ll&&!rl&&r
102
MC/DC50%1/2
ref $self and builtin::reftype($self) eq "HASH"
ref $self builtin::reftype($self) eq "HASH"
Truth table66%2/3
A and B
A= ref $selfB= builtin::reftype($self) eq "HASH"
ABresult
0X0
100
111
305 2 $self->{ ERROR } = $error;
306 2 $self->{ DECLINED } = 0;
307 }
308  
309 3 require Carp;
310 3 Carp::confess("Fatal badger error: ", @_);
311 }
312  
313  
314 #-----------------------------------------------------------------------
315 # messages
316 #-----------------------------------------------------------------------
317  
318 sub message {
319 1770 ▶ my $self = shift;
Subroutine message: called CC=10 SCAR=24.5
Pod documented
320 1770 ▶ my $name = shift
Condition33%1/3
shift() || $self->fatal('message() called without format name')
l!l&&r!l&&!r
177000
MC/DC0%0/2
shift() || $self->fatal('message() called without format name')
shift() $self->fatal('message() called without format name')
Truth table33%1/3
A || B
A= shift()B= $self->fatal('message() called without format name')
ABresult
1X1
011
000
321 || $self->fatal("message() called without format name");
322 1770 ▶ my $ref = $self && reftype $self;
Condition66%2/3
$self && builtin::reftype($self)
!ll&&!rl&&r
011769
MC/DC50%1/2
$self && builtin::reftype($self)
$self builtin::reftype($self)
Truth table66%2/3
A && B
A= $selfB= builtin::reftype($self)
ABresult
0X0
100
111
323 1770 my $format;
324  
325 # allow $self object to have an internal messages hash
326 1770 ▶ if ($self && $ref && $ref eq HASH && $self->{ messages }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $ref and $ref eq "HASH" and $self->{"messages"}) 1 1769 1770
Condition77%7/9
$self and $ref and $ref eq "HASH" and $self->{"messages"}
!ll&&!rl&&r
117681
$self and $ref and $ref eq "HASH"
!ll&&!rl&&r
101769
$self and $ref
!ll&&!rl&&r
011769
MC/DC50%2/4
$self and $ref and $ref eq "HASH" and $self->{"messages"}
$self $ref $ref eq "HASH" $self->{"messages"}
Truth table60%3/5
A and B and C and D
A= $selfB= $refC= $ref eq "HASH"D= $self->{"messages"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
327 $format = $self->{ messages }->{ $name }
328 1 ▶ if reftype $self->{ messages } eq HASH;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($self->{"messages"}) eq "HASH" 1 0 1
329 }
330  
331 1770 ▶ $format = class($self)->hash_value( MESSAGES => $name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 1769 1 1770
Condition33%1/3
Badger::Base::class($self)->hash_value('MESSAGES', $name) || $self->fatal("message() called with invalid message type: $name")
l!l&&r!l&&!r
176900
MC/DC0%0/2
Badger::Base::class($self)->hash_value('MESSAGES', $name) || $self->fatal("message() called with invalid message type: $name")
Badger::Base::class($self)->hash_value('MESSAGES', $name) $self->fatal("message() called with invalid message type: $name")
Truth table33%1/3
A || B
A= Badger::Base::class($self)->hash_value('MESSAGES', $name)B= $self->fatal("message() called with invalid message type: $name")
ABresult
1X1
011
000
332 || $self->fatal("message() called with invalid message type: $name")
333 unless defined $format;
334  
335 1770 xprintf($format, @_);
336 }
337  
338 sub warn_msg {
339 # explicitly quantify local message() method in case a subclass decides
340 # to re-implement the message() method to do something else
341 1 ▶ $_[0]->warn( message(@_) );
Subroutine warn_msg: called CC=1 SCAR=0.0
Pod documented
342 }
343  
344 sub error_msg {
345 26 ▶ $_[0]->error( message(@_) );
Subroutine error_msg: called CC=1 SCAR=0.0
Pod documented
346 }
347  
348 sub fatal_msg {
349 0 ▶ $_[0]->fatal( message(@_) );
Subroutine fatal_msg: not called CC=1 SCAR=6.9
Pod documented
350 }
351  
352 sub decline_msg {
353 11 ▶ $_[0]->decline( message(@_) );
Subroutine decline_msg: called CC=1 SCAR=0.0
Pod documented
354 }
355  
356 sub debug_msg {
357 0 ▶ $_[0]->debug( message(@_) );
Subroutine debug_msg: not called CC=1 SCAR=6.9
Pod documented
358 }
359  
360 sub throw_msg {
361 1 ▶ my $self = shift;
Subroutine throw_msg: called CC=1 SCAR=0.0
Pod documented
362 1 $self->throw( shift, message($self, @_) );
363 }
364  
365  
366 #-----------------------------------------------------------------------
367 # generate not_implemented() and todo() methods
368 #-----------------------------------------------------------------------
369  
370 class->methods(
371 map {
372 70 my $name = $_;
373 $name => sub {
374 10 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.1
375 10 ▶ my $ref = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
376 10 my ($pkg, $file, $line, $sub) = caller(0);
377 10 $sub = (caller(1))[3]; # subroutine the caller was called from
378 10 $sub =~ s/(.*):://;
379 10 ▶ my $msg = @_ ? join(BLANK, SPACE, @_) : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 6 10
380 10 return $self->error_msg( $name => "$sub()$msg", "for $ref in $file at line $line" );
381 140 };
382 }
383 qw( not_implemented todo )
384 );
385  
386  
387 #-----------------------------------------------------------------------
388 # generate on_warn() and on_error() methods
389 #-----------------------------------------------------------------------
390  
391 class->methods(
392 map {
393 70 my $on_event = $_;
394 140 my $ON_EVENT = uc $on_event;
395  
396 $on_event => sub {
397 69 ▶ my $self = shift;
Subroutine __ANON__: called CC=10 SCAR=23.4
398 69 my $class = class($self);
399 69 my $list;
400  
401 69 ▶ if (ref $self && reftype $self eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and builtin::reftype($self) eq 'HASH') { } 61 8 69
Condition66%2/3
ref $self and builtin::reftype($self) eq "HASH"
!ll&&!rl&&r
8061
MC/DC50%1/2
ref $self and builtin::reftype($self) eq "HASH"
ref $self builtin::reftype($self) eq "HASH"
Truth table66%2/3
A and B
A= ref $selfB= builtin::reftype($self) eq "HASH"
ABresult
0X0
100
111
402 # look in $self->{ config }->{ on_xxx } or in $ON_XXX pkg
403 # var for one or more event handlers
404 $list = $self->{ $ON_EVENT }
405 61 ▶ ||= $self->{ config }->{ $on_event }
Condition66%4/6
$self->{$ON_EVENT} ||= $self->{"config"}{$on_event} || $class->list_vars($ON_EVENT)
l!l&&r!l&&!r
21400
$self->{'config'}{$on_event} || $class->list_vars($ON_EVENT)
l!l&&r!l&&!r
1390
MC/DC0%0/3
$self->{$ON_EVENT} ||= $self->{"config"}{$on_event} || $class->list_vars($ON_EVENT)
$self->{$ON_EVENT} $self->{'config'}{$on_event} $class->list_vars($ON_EVENT)
Truth table0%0/4
A ||= B || C
A= $self->{$ON_EVENT}B= $self->{'config'}{$on_event}C= $class->list_vars($ON_EVENT)
ABCresult
1XX1
01X1
0011
0000
406 || $class->list_vars($ON_EVENT);
407 # careful! the config value might be a single handler
408 61 ▶ $list = $self->{ $ON_EVENT } = [$list]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 1 60 61
409 unless ref $list eq ARRAY;
410 61 $self->debug("got $on_event handlers: ", $self->dump_data_inline($list), "\n") if DEBUG;
411 }
412 else {
413 # class method or non-hash objects use pkg vars only
414 8 $list = $class->var_default($ON_EVENT, []);
415 8 ▶ $list = $class->var($ON_EVENT, [$list])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 1 7 8
416 unless ref $list eq ARRAY;
417 }
418  
419 # Add to the list any extra handlers passed as args. First
420 # argument can be 'before' or 'after' to add remaining args
421 # to start or end of list, otherwise the entire list is replaced.
422 69 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 66 69
423 3 ▶ if ($_[0] eq before) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq 'before') { } 0 3 3
elsif ($_[0] eq 'after') { } 1 2 3
424 0 shift;
425 0 unshift(@$list, @_);
426 }
427 elsif ($_[0] eq after) {
428 1 shift;
429 1 push(@$list, @_);
430 }
431 else {
432 2 @$list = @_;
433 }
434 }
435 # push(@$list, @_);
436  
437 69 return $list;
438 }
439 140 }
440 qw( on_warn on_error )
441 );
442  
443  
444 #-----------------------------------------------------------------------
445 # internal method to dispatch on_error/on_warning handlers
446 #-----------------------------------------------------------------------
447  
448 sub _dispatch_handlers {
449 10 ▶ my ($self, $type, $handlers, @args) = @_;
Subroutine _dispatch_handlers: called CC=10 SCAR=23.4
450  
451 10 $self->debug("_dispatch handlers: ", $self->dump_data_inline($handlers), "\n") if DEBUG;
452  
453 10 foreach (@$handlers) {
454 12 my $handler = $_; # don't alias list items
455 12 $self->debug("dispatch handler: $handler\n") if DEBUG;
456 12 ▶ if (! ref $handler) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $handler) { } 6 6 12
elsif (ref $handler eq 'CODE') { } 6 0 6
457 6 ▶ if ($handler eq WARN) { # 'warn' - we make sure that the
Branch: 2 branches
BranchTrueFalseTotal
if ($handler eq 'warn') { } 2 4 6
elsif ($handler eq 'none') { } 0 4 4
458 2 my $msg = join('', @args); # message is newline terminated
459 2 chomp($msg); # to stop Perl from adding a line
460 2 CORE::warn $msg, "\n"; # number that'll be wrong.
461 }
462 elsif ($handler eq NONE) { # NONE/0 - bail out
463 0 last;
464 }
465 else {
466 4 ▶ $handler = $self->can($handler)
Condition100%2/2
$self->can($handler) || (return $self->fatal("Invalid on_$type method: $handler"))
l!l
31
MC/DC100%1/1
$self->can($handler) || (return $self->fatal("Invalid on_$type method: $handler"))
$self->can($handler)
Truth table100%2/2
A
A= $self->can($handler)
Aresult
11
00
467 || return $self->fatal("Invalid on_$type method: $handler");
468 3 @args = $handler->($self, @args);
469 }
470 }
471 elsif (ref $handler eq CODE) {
472 6 @args = $handler->(@args);
473 }
474 else {
475 0 $self->fatal("Invalid on_$type handler: $handler");
476 }
477 10 $self->debug("mid-dispatch args: [", join(', ', @args), "]\n") if DEBUG;
478 # bail out if we got an empty list of return values or a single
479 # false value
480 10 ▶ last if ! @args || @args == 1 && ! $args[0];
Branch: 1 branch
BranchTrueFalseTotal
if not @args or @args == 1 and not $args[0] 2 8 10
Condition66%4/6
not @args or @args == 1 and not $args[0]
l!l&&r!l&&!r
028
@args == 1 and not $args[0]
!ll&&!rl&&r
082
MC/DC33%1/3
not @args or @args == 1 and not $args[0]
not @args @args == 1 not $args[0]
Truth table50%2/4
not(A) or B and not(C)
A= not @argsB= @args == 1C= not $args[0]
ABCresult
1XX1
0111
00X0
0100
481 }
482 8 $self->debug("returning ", join(', ', @args), "\n") if DEBUG;
483 8 return @args;
484 }
485  
486  
487  
488 #-----------------------------------------------------------------------
489 # Badger::Base::Trial - nomadic object for $object->try operation
490 #-----------------------------------------------------------------------
491  
492 package Badger::Base::Trial;
493 70 our $AUTOLOAD;
494  
495 sub _bind_ {
496 278 ▶ my ($class, $object) = @_;
Subroutine _bind_: called CC=2 SCAR=8.1
497 278 ▶ bless \$object, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
02780
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
498 }
499  
500 sub AUTOLOAD {
501 556 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=5 SCAR=16.1
502 556 my ($method) = ($AUTOLOAD =~ /([^:]+)$/ );
503 556 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 278 278 556
504  
505 # call method on target object in eval block, and downgrade
506 278 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 277 278
507 1 my @result = eval { $$self->$method(@_) };
508 1 ▶ $$self->decline($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
509 1 return @result;
510 }
511 else {
512 277 my $result = eval { $$self->$method(@_) };
513 277 ▶ $$self->decline($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 10 267 277
514 277 return $result;
515 }
516  
517 # TODO: catch missing error methods
518 }
519  
520  
521 70 1;
522 __END__
« lib/Badger/Factory.pm ↑ Summary blib/lib/Badger/Data/Type/Class.pm »