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 lib/Badger/Codec/HTML.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,
Subroutine BEGIN: called CC=1 SCAR=0.0
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 };
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 491 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
50 491 my $class = shift;
51  
52 # install warning handling for odd number of parameters when DEBUG enabled
53 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 Badger::Utils::odd_params(@_);
55 491 } if DEBUG;
56  
57 491 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 103 388 491
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
201187103
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 491 ▶ my $self = bless { }, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
04910
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 491 $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 488 ▶ return defined $self
Branch: 1 branch
BranchTrueFalseTotal
defined $self ? : 488 0 488
64 ? $self
65 : $self->error("init() method failed\n");
66 }
67  
68 63 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
69 63 my $self = shift;
70 # default action is to store reference to entire configuration so
71 # that methods can examine it later if they need to
72 63 $self->{ config } = shift;
73 63 return $self;
74 }
75  
76 8 ▶sub warn {
Subroutine warn: called CC=4 SCAR=15.0
Pod documented
77 8 my $self = shift;
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 69 ▶sub error {
Subroutine error: called CC=10 SCAR=23.1
Pod documented
92 69 my $self = shift;
93 69 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
6450
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 69 ▶ my $type = reftype $self || BLANK;
Condition100%2/2
builtin::reftype($self) || ''
l!l
645
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 69 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 56 13 69
elsif ($type eq 'HASH') { } 11 2 13
99 # don't stringify objects passed as argument
100 56 ▶ my $message = ref $_[0] ? shift : join(BLANK, map { defined($_) ? $_ : BLANK } @_);
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] ? : 0 56 56
defined $_ ? : 61 0 61
101 56 my $handlers = $self->on_error;
102  
103 # set package variable
104 56 ${ $class.PKG.ERROR } = $message;
105 56 ${ $class.PKG.DECLINED } = 0;
106  
107 56 ▶ if ($type eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 53 3 56
108 # set ERROR and DECLINED items in object
109 53 $self->{ ERROR } = $message;
110 53 $self->{ DECLINED } = 0;
111 }
112  
113 56 ▶ ($message) = $self->_dispatch_handlers( error => $handlers => $message )
Branch: 1 branch
BranchTrueFalseTotal
if $handlers and @$handlers 2 54 56
Condition66%2/3
$handlers and @$handlers
!ll&&!rl&&r
0542
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 56 $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 50 ▶sub decline {
Subroutine decline: called CC=5 SCAR=16.4
Pod documented
128 50 my $self = shift;
129 50 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
5000
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 50 ▶ my $type = reftype $self || BLANK;
Condition50%1/2
builtin::reftype($self) || ''
l!l
500
MC/DC0%0/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table50%1/2
A
A= builtin::reftype($self)
Aresult
11
00
131 50 ▶ my $reason = @_ == 1 ? shift : join(BLANK, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 50 0 50
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 50 ${ $class.PKG.ERROR } = $reason;
136 50 ${ $class.PKG.DECLINED } = 1;
137  
138 50 ▶ if ($type eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 50 0 50
139 50 $self->{ ERROR } = $reason;
140 50 $self->{ DECLINED } = 1;
141 }
142  
143 50 return undef;
144 }
145  
146 2 ▶sub declined {
Subroutine declined: called CC=4 SCAR=15.2
Pod documented
147 2 my $self = shift;
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 0 ▶ : ${ $class.PKG.DECLINED };
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' ? : 2 0 2
154 }
155  
156 27 ▶sub reason {
Subroutine reason: called CC=4 SCAR=15.2
Pod documented
157 27 my $self = shift;
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 0 ▶ : ${ $class.PKG.ERROR };
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' ? : 27 0 27
164 }
165  
166 59 ▶sub throw {
Subroutine throw: called CC=10 SCAR=24.3
Pod documented
167 59 my $self = shift;
168 59 my $type = shift;
169 59 my $emod = $self->exception;
170 59 my $e;
171  
172 # TODO: grok file/line/sub from caller and add to exceptions
173  
174 59 ▶ if (! @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_) { } 56 3 59
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 56 ▶ if (blessed $type && $type->isa($emod)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($type) and $type->isa($emod)) { } 0 56 56
Condition33%1/3
builtin::blessed($type) and $type->isa($emod)
!ll&&!rl&&r
5600
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 56 $self->debug("creating new exception object chain: info => $type\n") if DEBUG;
187 56 $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 59 $e->throw;
215 }
216  
217 364 ▶sub try {
Subroutine try: called CC=5 SCAR=16.1
Pod documented
218 364 my $self = shift;
219 364 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 16 348 364
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 348 return TRIAL->_bind_($self);
234 }
235 }
236  
237 0 ▶sub catch {
Subroutine catch: not called CC=1 SCAR=6.9
Pod documented
238 # this depends on some code in Badger::Exception which I haven't
239 # written yet...
240 0 shift->todo;
241 }
242  
243 63 ▶sub throws {
Subroutine throws: called CC=9 SCAR=22.0
Pod documented
244 63 my $self = shift;
245 63 ▶ my $type = reftype $self || BLANK;
Condition100%2/2
builtin::reftype($self) || ''
l!l
5310
MC/DC100%1/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table100%2/2
A
A= builtin::reftype($self)
Aresult
11
00
246 63 my $class = class($self);
247 63 my $throws;
248  
249 63 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 4 59 63
elsif ($type eq 'HASH') { } 52 7 59
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 52 ▶ && $self->{ config }->{ throws };
Condition83%5/6
$self->{"THROWS"} ||= $self->{"config"} && $self->{"config"}{"throws"}
l!l&&r!l&&!r
1348
$self->{'config'} && $self->{'config'}{'throws'}
!ll&&!rl&&r
0483
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 63 ▶ return $throws
Condition83%5/6
$throws || $class->any_var('THROWS') || $class->id
l!l&&r!l&&!r
17460
$throws or $class->any_var("THROWS")
l!l&&r!l&&!r
8946
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 59 ▶sub exception {
Subroutine exception: called CC=8 SCAR=25.4
Pod documented
271 59 my $self = shift;
272 59 ▶ my $type = reftype $self || BLANK;
Condition100%2/2
builtin::reftype($self) || ''
l!l
563
MC/DC100%1/1
builtin::reftype($self) || ''
builtin::reftype($self)
Truth table100%2/2
A
A= builtin::reftype($self)
Aresult
11
00
273 59 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 59 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 59 59
elsif ($type eq 'HASH') { } 56 3 59
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 56 ▶ && $self->{ config }->{ exception };
Condition33%2/6
$self->{"EXCEPTION"} ||= $self->{"config"} && $self->{"config"}{"exception"}
l!l&&r!l&&!r
0056
$self->{'config'} && $self->{'config'}{'exception'}
!ll&&!rl&&r
0560
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 59 ▶ return $emod
Condition33%1/3
$emod || Badger::Base::class($self)->any_var('EXCEPTION')
l!l&&r!l&&!r
0590
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 3 ▶sub fatal {
Subroutine fatal: called CC=4 SCAR=13.9
Pod documented
296 3 my $self = shift;
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 1982 ▶sub message {
Subroutine message: called CC=10 SCAR=24.5
Pod documented
319 1982 my $self = shift;
320 1982 ▶ my $name = shift
Condition33%1/3
shift() || $self->fatal('message() called without format name')
l!l&&r!l&&!r
198200
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 1982 ▶ my $ref = $self && reftype $self;
Condition66%2/3
$self && builtin::reftype($self)
!ll&&!rl&&r
011981
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 1982 my $format;
324  
325 # allow $self object to have an internal messages hash
326 1982 ▶ if ($self && $ref && $ref eq HASH && $self->{ messages }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $ref and $ref eq "HASH" and $self->{"messages"}) 1 1981 1982
Condition77%7/9
$self and $ref and $ref eq "HASH" and $self->{"messages"}
!ll&&!rl&&r
119801
$self and $ref and $ref eq "HASH"
!ll&&!rl&&r
101981
$self and $ref
!ll&&!rl&&r
011981
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 1982 ▶ $format = class($self)->hash_value( MESSAGES => $name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 1981 1 1982
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
198100
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 1982 xprintf($format, @_);
336 }
337  
338 1 ▶sub warn_msg {
Subroutine warn_msg: called CC=1 SCAR=0.0
Pod documented
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(@_) );
342 }
343  
344 27 ▶sub error_msg {
Subroutine error_msg: called CC=1 SCAR=0.0
Pod documented
345 27 $_[0]->error( message(@_) );
346 }
347  
348 0 ▶sub fatal_msg {
Subroutine fatal_msg: not called CC=1 SCAR=6.9
Pod documented
349 0 $_[0]->fatal( message(@_) );
350 }
351  
352 24 ▶sub decline_msg {
Subroutine decline_msg: called CC=1 SCAR=0.0
Pod documented
353 24 $_[0]->decline( message(@_) );
354 }
355  
356 0 ▶sub debug_msg {
Subroutine debug_msg: not called CC=1 SCAR=6.9
Pod documented
357 0 $_[0]->debug( message(@_) );
358 }
359  
360 1 ▶sub throw_msg {
Subroutine throw_msg: called CC=1 SCAR=0.0
Pod documented
361 1 my $self = shift;
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 10 ▶ $name => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
374 10 my $self = shift;
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 67 ▶ $on_event => sub {
Subroutine __ANON__: called CC=10 SCAR=23.4
397 67 my $self = shift;
398 67 my $class = class($self);
399 67 my $list;
400  
401 67 ▶ if (ref $self && reftype $self eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and builtin::reftype($self) eq 'HASH') { } 62 5 67
Condition66%2/3
ref $self and builtin::reftype($self) eq "HASH"
!ll&&!rl&&r
5062
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 62 ▶ ||= $self->{ config }->{ $on_event }
Condition66%4/6
$self->{$ON_EVENT} ||= $self->{"config"}{$on_event} || $class->list_vars($ON_EVENT)
l!l&&r!l&&!r
21410
$self->{'config'}{$on_event} || $class->list_vars($ON_EVENT)
l!l&&r!l&&!r
1400
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 62 ▶ $list = $self->{ $ON_EVENT } = [$list]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 1 61 62
409 unless ref $list eq ARRAY;
410 62 $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 5 $list = $class->var_default($ON_EVENT, []);
415 5 ▶ $list = $class->var($ON_EVENT, [$list])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 1 4 5
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 67 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 64 67
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 67 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 10 ▶sub _dispatch_handlers {
Subroutine _dispatch_handlers: called CC=10 SCAR=23.4
449 10 my ($self, $type, $handlers, @args) = @_;
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 348 ▶sub _bind_ {
Subroutine _bind_: called CC=2 SCAR=8.1
496 348 my ($class, $object) = @_;
497 348 ▶ bless \$object, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
03480
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 696 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=16.1
501 696 my $self = shift;
502 696 my ($method) = ($AUTOLOAD =~ /([^:]+)$/ );
503 696 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 348 348 696
504  
505 # call method on target object in eval block, and downgrade
506 348 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 347 348
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 347 my $result = eval { $$self->$method(@_) };
513 347 ▶ $$self->decline($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 10 337 347
514 347 return $result;
515 }
516  
517 # TODO: catch missing error methods
518 }
519  
520  
521 70 1;
522 __END__
« lib/Badger/Factory.pm ↑ Summary lib/Badger/Codec/HTML.pm »