Base.pm

filter: Statementstmt 100% 257 / 257 Branchbran 98.6% 75 / 76 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 41 / 41 Podpod 100% 22 / 22 totaltotal 99.7% 398 / 399 CC 44 SCAR 16.5
CC
44
Cov
100%
_trace
13.0
_late
11.0
_fuss
6.0
CRAP 5.2
↑ Summary Base/Cookbook.pm »
1 package Error::Base;
2 #=========# MODULE USAGE
3 #~ use Error::Base; # Simple structured errors with full backtrace
4 #~
5  
6 #=========# PACKAGE BLOCK
7 { #=====# Entire package inside bare block, not indented....
8  
9 24 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use version; our $VERSION = qv('v1.0.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Core modules
15 use overload # Overload Perl operations
16 24 ▶ '""' => \&_stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 ;
18 24 ▶use Scalar::Util; # General-utility scalar subroutines
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # CPAN modules
21  
22 # Alternate uses
23 #~ use Devel::Comments '###', ({ -file => 'debug.log' }); #~
24  
25 ## use
26 #============================================================================#
27  
28 # Pseudo-globals
29  
30 # Compiled regexes
31 24 our $QRFALSE = qr/\A0?\z/ ;
32 24 our $QRTRUE = qr/\A(?!$QRFALSE)/ ;
33  
34 24 our $BASETOP = 2; # number of stack frames generated internally
35 # see also global defaults set in accessors
36  
37 #----------------------------------------------------------------------------#
38  
39 #=========# OPERATOR OVERLOADING
40 #
41 # _stringify(); # short
42 #
43 # Purpose : Overloads stringification.
44 # Parms : ____
45 # Reads : ____
46 # Returns : ____
47 # Writes : ____
48 # Throws : ____
49 # See also : ____
50 #
51 # ____
52 #
53 99 ▶sub _stringify {
Subroutine _stringify: called CC=2 SCAR=6.9
54 # my ($self, $other, $swap) = @_;
55 99 my ($self, undef, undef) = @_;
56
57 24 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 99 ▶ if ( defined $self->{-lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-lines'}) { } 62 37 99
59 62 return join qq{\n}, @{ $self->{-lines} }, q{};
60 }
61 else {
62 37 return 'Error::Base internal error: stringifying unthrown object';
63 };
64
65 }; ## _stringify
66  
67 #=========# INTERNAL ROUTINE
68 #
69 # @lines = $self->_trace( # dump full backtrace
70 # -top => 2, # starting stack frame
71 # );
72 #
73 # Purpose : Full backtrace dump.
74 # Parms : -top : integer : usually set at init-time
75 # Returns : ____
76 # Writes : $self->{-frames} : unformatted backtrace
77 # Throws : 'excessive backtrace'
78 # See also : _fuss(), _paired()
79 #
80 # ____
81 #
82 112 ▶sub _trace {
Subroutine _trace: called CC=13 SCAR=25.7
83 112 my $self = shift;
84 112 my %args = _paired(@_);
85 112 ▶ my $i = defined $args{-top} ? $args{-top} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-top'} ? : 111 1 112
86
87 112 my $bottomed ;
88 112 my @maxlen = ( 1, 1, 1 ); # starting length of each field
89 112 my @f = ( # order in which keys will be dumped
90 '-sub',
91 '-line',
92 '-file',
93 );
94 112 my $pad = q{ }; # padding for better formatting
95 112 my $in ; # usually 'in '
96
97 my @frames ; # unformatted AoH
98 112 my @lines ; # formatted array of strings
99
100 # Get each stack frame.
101 112 ▶ while ( not $bottomed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bottomed) 1972 0 1972
102 1972 my $frame ;
103
104 # Get info for current frame.
105 (
106 $frame->{-package},
107 $frame->{-file},
108 $frame->{-line},
109 undef,
110 undef,
111 undef,
112 $frame->{-eval}
113 1972 ) = caller( $i );
114
115 # caller returns this from the "wrong" viewpoint
116 (
117 undef,
118 undef,
119 undef,
120 $frame->{-sub},
121 undef,
122 undef,
123 undef,
124 1972 ) = caller( $i + 1 );
125
126 # Normal exit from while loop.
127 1972 ▶ if ( not $frame->{-package} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->{"-package"}) 111 1861 1972
128 111 $bottomed++;
129 111 last;
130 };
131
132 # Clean up bottom frame.
133 1861 ▶ if ( not $frame->{-sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->{"-sub"}) 111 1750 1861
134 111 $frame->{-sub} = q{};
135 111 $frame->{-bottom} = 1;
136 };
137
138 # Get maximum length of each field.
139 1861 for my $fc ( 0..$#f ) {
140 $maxlen[$fc] = $maxlen[$fc] > length $frame->{$f[$fc]}
141 ? $maxlen[$fc]
142 5583 ▶ : length $frame->{$f[$fc]}
Branch: 1 branch
BranchTrueFalseTotal
$maxlen[$fc] > length $$frame{$f[$fc]} ? : 3164 2419 5583
143 ;
144 };
145
146 # Clean up any eval text.
147 1861 ▶ if ($frame->{-eval}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"-eval"}) 8 1853 1861
148 # fake newlines for hard newlines
149 8 $frame->{-eval} =~ s/\n/\\n/g;
150 };
151 1861 push @frames, $frame;
152
153 # Safety exit from while loop.
154 1861 $i++;
155 1861 ▶ die 'Error::Base internal error: excessive backtrace'
Branch: 1 branch
BranchTrueFalseTotal
if $i > 99 1 1860 1861
156 if $i > 99;
157 #~ last if $i > 9; # DEBUG ONLY #~
158
159 }; ## while not bottomed
160
161 # Stash unformatted stack frames.
162 111 $self->{-frames} = \@frames;
163
164 # Format each stack frame.
165 111 for my $frame (@frames) {
166
167 # Pad each field to maximum length (found in while)
168 1763 for my $fc ( 0..$#f ) {
169 5289 my $diff = $maxlen[$fc] - length $frame->{$f[$fc]};
170 5289 $frame->{$f[$fc]} = $frame->{$f[$fc]} . ($pad x $diff);
171 };
172
173 # Fix up bottom.
174 1763 ▶ if ( $frame->{-bottom} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{'-bottom'}) { } 111 1652 1763
175 111 $frame->{-sub} =~ s/ /_/g; # all underbars
176 111 $in = q{___}; # *THREE* underbars
177 }
178 else {
179 1652 $in = q{in }; # a three-char string
180 };
181
182 # Format printable line.
183 1763 my $line = qq*$in$frame->{-sub} at line $frame->{-line}*
184 . qq* [$frame->{-file}]*
185 ;
186
187 # Append any eval text.
188 1763 ▶ if ($frame->{-eval}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"-eval"}) 8 1755 1763
189 # hard newlines so number of frames doesn't change
190 8 $line = $line
191 . qq{\n}
192 . qq* string eval: "$frame->{-eval}"*
193 . qq{\n}
194 ;
195 };
196
197 1763 push @lines, $line;
198 }; ## for each frame
199
200 111 return @lines;
201 }; ## _trace
202  
203 #=========# CLASS OR OBJECT METHOD
204 #
205 # Error::Base->crash( $text ); # class method; error text required
206 # $err->crash; # object method
207 # $err->crash( $text ); # object method; error text optional
208 # $err->crash( -base => $base ); # named argument okay
209 # $err->crash( -foo => 'bar' ); # set Error::Base options now
210 # $err->crash( mybit => 'baz' ); # set your private stuff now
211 #
212 # Purpose : Fatal out of your program's errors
213 # Parms : $text : string : final part of error message [odd arg]
214 # : -type : string : middle part of error message
215 # : -base : string : initial part of error message
216 # : -top : integer : starting backtrace frame
217 # : -quiet : boolean : TRUE for no backtrace at all
218 # Returns : never
219 # Throws : $self : die will stringify
220 # See also : _fuss(), crank(), cuss(), init()
221 #
222 # The first arg is tested to see if it's a class or object reference.
223 # Then the next test is to see if an odd number of args remain.
224 # If so, then the next arg is shifted off and considered -base.
225 # All remaining args are considered key/value pairs and passed to new().
226 #
227 38 ▶sub crash{
Subroutine crash: called CC=1 SCAR=0.0
Pod documented
228 38 my $self = _fuss(@_);
229
230 38 die $self;
231 }; ## crash
232  
233 #=========# INTERNAL FUNCTION
234 #
235 # This does all the work for crash(), crank(), and cuss().
236 # See crash() for more info.
237 #
238 126 ▶sub _fuss {
Subroutine _fuss: called CC=6 SCAR=17.9
239 126 my $self = shift;
240 126 ▶ if ( Scalar::Util::blessed $self ) { # called on existing object
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 58 68 126
241 58 $self->init(@_); # initialize or overwrite
242 }
243 else { # called as class method
244 68 $self = $self->new(@_);
245 };
246
247 126 my $max = 78; # maximum line length
248 126 my $message ; # user-defined error message
249 my @lines ; # to stringify $self
250
251 # Deal with array values.
252 126 $self->{-mesg} = _expand_ref( $self->{-mesg} );
253
254 # Collect all the texts into one message.
255 $message = _join_local(
256 $self->{-base},
257 $self->{-type},
258 $self->{-mesg},
259 126 );
260 #~ ### $self
261
262 # Late interpolate.
263 126 $message = $self->_late( $message );
264
265 # If still no text in there, finally default.
266 125 ▶ if ( not $message ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($message) 46 79 125
267 46 $message = 'Undefined error.';
268 };
269 125 $self->{-all} = $message; # keep for possible inspection
270  
271 # Accumulate.
272 125 @lines = ( $message );
273
274 # Stack backtrace by default.
275 125 ▶ if ( not $self->{-quiet} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"-quiet"}) 107 18 125
276 107 my @trace = $self->_trace( -top => $self->{-top} );
277 106 push @lines, @trace;
278 };
279
280 # Optionally prepend some stuff.
281 124 ▶ if ( defined $self->{-prepend} ) { # prepended to first line
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-prepend'}) { } 10 114 124
282 10 @{ $self->{-lines} }
283 10 = _join_local( $self->{-prepend}, shift @lines );
284 }
285 else {
286 114 @{ $self->{-lines} } = shift @lines;
287 };
288 124 ▶ if ( defined $self->{-indent} ) { # prepended to all others
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-indent'}) { } 10 114 124
289 10 push @{ $self->{-lines} },
290 10 map { _join_local( $self->{-indent}, $_ ) } @lines;
291 }
292 else {
293 114 push @{ $self->{-lines} }, @lines;
294 };
295
296 ### @lines
297 124 return $self;
298
299 #~ # Do something to control line length and deal with multi-line $all.
300 #~ my @temp = split /\n/, $all; # in case it's multi-line
301 #~ my $limit = $max - length $prepend;
302 #~ @temp = map { s//\n/ if length > $limit }
303 #~ @temp; # avoid excessive line length
304 #~ my $infix = qq{\n} . $indent;
305 #~ $all = join $infix, @temp;
306
307 }; ## _fuss
308  
309 #=========# CLASS OR OBJECT METHOD
310 #
311 # Just like crash() except it warn()-s and does not die().
312 # See crash() for more info.
313 5 ▶sub crank{
Subroutine crank: called CC=1 SCAR=0.0
Pod documented
314 5 my $self = _fuss(@_);
315
316 5 warn $self;
317 }; ## crank
318  
319 #=========# CLASS OR OBJECT METHOD
320 #
321 # Just like crash() except it just returns $self (after expansion).
322 # See crash() for more info.
323 83 ▶sub cuss{
Subroutine cuss: called CC=1 SCAR=0.0
Pod documented
324 83 my $self = _fuss(@_);
325
326 81 return $self;
327 }; ## crank
328  
329 #=========# INTERNAL FUNCTION
330 #
331 # $string =_expand_ref( $var ); # expand reference if any
332 #
333 # Purpose : ____
334 # Parms : ____
335 # Reads : ____
336 # Returns : ____
337 # Invokes : ____
338 # Writes : ____
339 # Throws : ____
340 # See also : ____
341 #
342 # ____
343 #
344 132 ▶sub _expand_ref {
Subroutine _expand_ref: called CC=4 SCAR=13.9
345 132 my $in = shift;
346 132 my $rt = Scalar::Util::reftype $in; # returns no class
347
348 132 ▶ if ( not $rt ) { # simple scalar...
Branch: 3 branches
BranchTrueFalseTotal
if (not $rt) { } 125 7 132
elsif ($rt eq 'SCALAR') { } 1 6 7
elsif ($rt eq 'ARRAY') { } 4 2 6
349 # ... don't deref
350 125 return $in # unchanged
351 }
352 elsif ( $rt eq 'SCALAR' ) { # scalar ref
353 1 return $$in # dereference
354 }
355 elsif ( $rt eq 'ARRAY' ) { # array ref
356 4 return _join_local(@$in); # deref and join
357 }
358 #~ elsif ( $rt eq 'HASH' ) { # hash ref
359 #~ my @sorted = map { $_, $in->{$_} } sort keys %$in;
360 #~ return _join_local(@sorted); # deref, sort, and join
361 #~ }
362 else {
363 2 die 'Error::Base internal error: bad reftype in _expand_ref';
364 };
365
366 }; ## _expand_ref
367  
368 #=========# INTERNAL FUNCTION
369 #
370 # $string = _join_local(@_); # short
371 #
372 # Purpose : Like builtin join() but with local list separator.
373 # Parms : @_ : strings to join
374 # Returns : $string : joined strings
375 # Throws : ____
376 # See also : init()
377 #
378 # Buitin join() does not take $" (or anything else) by default.
379 # We splice out empty strings to avoid useless runs of spaces.
380 #
381 288 ▶sub _join_local {
Subroutine _join_local: called CC=1 SCAR=0.0
382 288 my @parts = @_;
383
384 # Splice out empty strings.
385 288 @parts = grep { $_ ne q** } @parts;
386
387 288 return join $", @parts;
388 }; ## _join_local
389  
390 #=========# INTERNAL FUNCTION
391 #
392 # my %args = _paired(@_); # check for unpaired arguments
393 #
394 # Purpose : ____
395 # Parms : ____
396 # Reads : ____
397 # Returns : ____
398 # Writes : ____
399 # Throws : ____
400 # See also : ____
401 #
402 # ____
403 #
404 117 ▶sub _paired {
Subroutine _paired: called CC=2 SCAR=6.9
405 117 ▶ if ( scalar @_ % 2 ) { # an odd number modulo 2 is one: true
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ % 2) 2 115 117
406 2 die 'Error::Base internal error: unpaired args';
407 };
408 115 return @_;
409 }; ## _paired
410  
411 #=========# CLASS METHOD
412 #
413 # my $obj = $class->new();
414 # my $obj = $class->new({ -a => 'x' });
415 #
416 # Purpose : Object constructor
417 # Parms : $class : Any subclass of this class
418 # anything else will be passed to init()
419 # Returns : $self
420 # Invokes : init()
421 #
422 # Good old-fashioned hashref-based object constructor.
423 #
424 238 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
425 238 my $class = shift;
426 238 my $self = {}; # always hashref
427
428 238 bless ($self => $class);
429 238 $self->init(@_); # init remaining args
430
431 238 return $self;
432 }; ## new
433  
434 #=========# OBJECT METHOD
435 #
436 # $err->init( k => 'v', f => $b );
437 # $err->init( $text, k => 'v', f => $b );
438 #
439 # An object can be init()-ed more than once; all new values overwrite the old.
440 # This non-standard init() allows an unnamed initial arg.
441 #
442 # See: crash()
443 #
444 301 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod documented
445 301 my $self = shift;
446 301 ▶ if ( scalar @_ % 2 ) { # an odd number modulo 2 is one: true
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ % 2) 58 243 301
447 58 $self->{-mesg} = shift; # and now it's even
448 };
449
450 # Merge all values. Newer values always overwrite.
451 301 %{$self} = ( %{$self}, @_ );
452
453 # Set some default values, mostly to avoid 'uninitialized' warnings.
454 301 $self->put_base( $self->{-base} );
455 301 $self->put_type( $self->{-type} );
456 301 $self->put_mesg( $self->{-mesg} );
457 301 $self->put_quiet( $self->{-quiet} );
458 301 $self->put_nest( $self->{-nest} );
459 301 $self->_fix_pre_ind();
460
461 301 return $self;
462 }; ## init
463  
464 #----------------------------------------------------------------------------#
465 # ACCSESSORS
466  
467 24 my $Default = {
468 -base => q{},
469 -type => q{},
470 -mesg => q{},
471 -quiet => 0,
472 -nest => 0,
473 -prepend => undef,
474 -indent => undef,
475 };
476  
477  
478  
479 # put
480 303 ▶sub put_base {
Subroutine put_base: called CC=2 SCAR=6.9
Pod documented
481 303 my $self = shift;
482 303 $self->{-base} = shift;
483 303 ▶ if ( not defined $self->{-base} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"-base"}) 170 133 303
484 170 $self->{-base} = $Default->{-base};
485 };
486 303 return $self;
487 };
488 301 ▶sub put_type {
Subroutine put_type: called CC=2 SCAR=6.9
Pod documented
489 301 my $self = shift;
490 301 $self->{-type} = shift;
491 301 ▶ if ( not defined $self->{-type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"-type"}) 229 72 301
492 229 $self->{-type} = $Default->{-type};
493 };
494 301 return $self;
495 };
496 305 ▶sub put_mesg {
Subroutine put_mesg: called CC=2 SCAR=6.9
Pod documented
497 305 my $self = shift;
498 305 $self->{-mesg} = shift;
499 305 ▶ if ( not defined $self->{-mesg} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"-mesg"}) 177 128 305
500 177 $self->{-mesg} = $Default->{-mesg};
501 };
502 305 return $self;
503 };
504 304 ▶sub put_quiet {
Subroutine put_quiet: called CC=2 SCAR=6.9
Pod documented
505 304 my $self = shift;
506 304 $self->{-quiet} = shift;
507 304 ▶ if ( not defined $self->{-quiet} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"-quiet"}) 220 84 304
508 220 $self->{-quiet} = $Default->{-quiet};
509 };
510 304 return $self;
511 };
512 304 ▶sub put_nest {
Subroutine put_nest: called CC=2 SCAR=6.9
Pod documented
513 304 my $self = shift;
514 304 $self->{-nest} = shift;
515 304 ▶ if ( not defined $self->{-nest} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"-nest"}) 227 77 304
516 227 $self->{-nest} = $Default->{-nest};
517 };
518 # -top is now deprecated from the API
519 304 $self->{-top} = $self->{-nest} + $BASETOP;
520 304 return $self;
521 };
522 5 ▶sub put_prepend {
Subroutine put_prepend: called CC=1 SCAR=0.0
Pod documented
523 5 my $self = shift;
524 5 $self->{-prepend} = shift;
525 5 $self->_fix_pre_ind();
526 5 return $self;
527 };
528 5 ▶sub put_indent {
Subroutine put_indent: called CC=1 SCAR=0.0
Pod documented
529 5 my $self = shift;
530 5 $self->{-indent} = shift;
531 5 $self->_fix_pre_ind();
532 5 return $self;
533 };
534 # For internal use only
535 311 ▶sub _fix_pre_ind {
Subroutine _fix_pre_ind: called CC=6 SCAR=17.9
536 311 my $self = shift;
537 311 my $indent ;
538 my $case ;
539
540 311 ▶ $case = $case . ( defined $self->{-prepend} ? 'P' : '-' );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'-prepend'} ? : 14 297 311
541 311 ▶ $case = $case . ( defined $self->{-indent} ? 'I' : '-' );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'-indent'} ? : 9 302 311
542
543 # four cases cover all needs
544 311 ▶ if ( $case eq '--' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($case eq '--') { } 293 18 311
elsif ($case eq '-I') { } 4 14 18
elsif ($case eq 'P-') { } 9 5 14
545 293 $self->{-prepend} = $Default->{-prepend};
546 293 $self->{-indent} = $Default->{-indent};
547 }
548 elsif ( $case eq '-I' ) {
549 4 $self->{-prepend} = $self->{-indent};
550 }
551 elsif ( $case eq 'P-' ) {
552 9 my $prepend = $self->{-prepend};
553 9 $self->{-indent} = ( substr $prepend, 0, 1 )
554 . ( q{ } x ((length $prepend) - 1) )
555 ;
556 }
557 else {
558 # ( $case eq 'PI' ) # do nothing
559 };
560
561 311 return $self;
562 };
563  
564 # get
565 2 ▶sub get_base {
Subroutine get_base: called CC=1 SCAR=0.0
Pod documented
566 2 my $self = shift;
567 2 return $self->{-base};
568 };
569 2 ▶sub get_type {
Subroutine get_type: called CC=1 SCAR=0.0
Pod documented
570 2 my $self = shift;
571 2 return $self->{-type};
572 };
573 3 ▶sub get_mesg {
Subroutine get_mesg: called CC=1 SCAR=0.0
Pod documented
574 3 my $self = shift;
575 3 return $self->{-mesg};
576 };
577 2 ▶sub get_quiet {
Subroutine get_quiet: called CC=1 SCAR=0.0
Pod documented
578 2 my $self = shift;
579 2 return $self->{-quiet};
580 };
581 2 ▶sub get_nest {
Subroutine get_nest: called CC=1 SCAR=0.0
Pod documented
582 2 my $self = shift;
583 2 return $self->{-nest};
584 };
585 2 ▶sub get_prepend {
Subroutine get_prepend: called CC=1 SCAR=0.0
Pod documented
586 2 my $self = shift;
587 2 return $self->{-prepend};
588 };
589 2 ▶sub get_indent {
Subroutine get_indent: called CC=1 SCAR=0.0
Pod documented
590 2 my $self = shift;
591 2 return $self->{-indent};
592 };
593 2 ▶sub get_all {
Subroutine get_all: called CC=1 SCAR=0.0
Pod documented
594 2 my $self = shift;
595 2 return $self->{-all};
596 };
597 2 ▶sub get_lines {
Subroutine get_lines: called CC=1 SCAR=0.0
Pod documented
598 2 my $self = shift;
599 2 return $self->{-lines};
600 };
601 2 ▶sub get_frames {
Subroutine get_frames: called CC=1 SCAR=0.0
Pod documented
602 2 my $self = shift;
603 2 return $self->{-frames};
604 };
605  
606 ## accessors
607 #----------------------------------------------------------------------------#
608  
609 #=========# INTERNAL OBJECT METHOD
610 #
611 # $out = $self->_late( $in ); # late interpolate
612 #
613 # Wrapper method; see Error::Base::Late::_late().
614 140 ▶sub _late { return Error::Base::Late::_late(@_) };
Subroutine _late: called CC=1 SCAR=0.0
615 ##
616  
617 } #=====# ... Entire package inside bare block, not indented.
618 #=========# END PACKAGE BLOCK
619  
620 24 package Error::Base::Late; # switch package to avoid pseudo-global lexicals
621 {
622  
623 #=========# INTERNAL FUNCTION IN FOREIGN PACKAGE
624 #
625 # $out = _late( $self, $in ); # late interpolate
626 #
627 # Purpose : ____
628 # Parms : $in : scalar string
629 # Reads : every key in $self starting with a $, @, or % sigil
630 # : $self : available as '$self'
631 # Returns : $out : scalar string
632 # Writes : ____
633 # Throws : ____
634 # See also : ____
635 #
636 # I hope this is the worst possible implementation of late().
637 # Late interpolation is accomplished by multiple immediate interpolations,
638 # inside and outside of a string eval.
639 # Non-core PadWalker is not used to derive interpolation context;
640 # caller is required to pass context inside the $self object.
641 # To avoid collision and unintended interpolation, I make housekeeping
642 # variables internal to this routine, package variables.
643 # These are fully qualified to a "foreign" package; caller cannot
644 # accidentally access them (although I cannot stop you from doing stupid).
645 # Some work is done in a bare "setup" block with lexical variables.
646 # But package variables are used to pass values within the routine,
647 # from block to block, inside to outside, within and without the eval.
648 # Quoting is a major concern. Heredocs are used in three places for
649 # double-quoted interpolation; they may not conflict with each other
650 # or with any string that may exist within any of:
651 # - the string to be interpolated, $in
652 # - values passed in $self against @keys (keys with leading sigils)
653 # Rather than attempt to exclude all of these from a generic q//,
654 # I chose heredocs and three long, arbitrary strings.
655 #
656 141 ▶sub _late {
Subroutine _late: called CC=11 SCAR=24.0
657 24 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
658 24 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
659 24 ▶ no warnings 'uninitialized'; # too many to count
Subroutine BEGIN: called CC=1 SCAR=0.0
660 #~ ##### CASE:
661 #~ ##### @_
662 # No lexical variables loose in the outer block of the subroutine.
663 141 $Error::Base::Late::self = shift;
664 141 ▶ if ( not ref $Error::Base::Late::self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $Error::Base::Late::self) 1 140 141
665 1 die 'Error::Base internal error: no $self';
666 };
667 140 ▶ $Error::Base::Late::in = shift || undef;
Condition100%2/2
shift() || undef
l!l
9347
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
668 140 ▶ return $Error::Base::Late::in
Branch: 1 branch
BranchTrueFalseTotal
unless $Error::Base::Late::in =~ /[\$\@%]/ 120 20 140
669 unless $Error::Base::Late::in =~ /[\$\@%]/; # no sigil, don't bother
670
671 # Y0uMaYFiReWHeNReaDYGRiDLeY # quite unlikely to collide
672
673 20 @Error::Base::Late::code = undef; # to be eval-ed
674 20 $Error::Base::Late::out = undef; # interpolated
675
676 #--------------------------------------------------------------------#
677 { # setup block
678
679 # Some preamble.
680 20 push @Error::Base::Late::code,
681 q**,
682 q*#--------------------------------------------------------#*,
683 q*# START EVAL *,
684 q**,
685 q*my $self = $Error::Base::Late::self;*,
686 q**,
687 ;
688
689 # Unpack all appropriate k/v pairs into their own lexical variables...
690
691 # Each key includes leading sigil.
692 20 my @keys = grep { /^[\$\@%]/ } keys %$Error::Base::Late::self;
693 20 ▶ return $Error::Base::Late::in # abort if not interpolating today
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 2 18 20
694 #~ unless ( @keys or $Error::Base::Late::in =~ /\$self/ );
695 unless ( @keys );
696 18 my $key ; # placeholder includes sigil!
697 my $val ; # value to be interpolated
698 18 my $rt ; # builtin 'ref' returns (unwanted) class of blessed ref
699
700 # my $key = $sigil?$Error::Base::Late::self->{'$key'}?;
701 18 my $ch1 = q*my * ;
702 18 my $ch2 = q* = * ;
703 18 my $ch3 = q*Error::Base::Late::self->{'* ;
704 18 my $ch4 = q*'}* ;
705 18 my $ch5 = q*;* ;
706
707 18 for my $key (@keys) {
708 24 $val = $Error::Base::Late::self->{$key};
709 24 $rt = Scalar::Util::reftype $val; # returns no class
710
711 24 my $sigil ; # sigil (if any) to deref
712 24 my $lbc = q*{$*; # left brace if sigil . '$' for $self
713 24 my $rbc = q*}*; # right brace if sigil
714
715 24 ▶ if ( not $rt ) { # simple scalar...
Branch: 4 branches
BranchTrueFalseTotal
if (not $rt) { } 12 12 24
elsif ($rt eq 'SCALAR') { } 3 9 12
elsif ($rt eq 'ARRAY') { } 5 4 9
elsif ($rt eq 'HASH') { } 3 1 4
716 # ... don't deref
717 12 $lbc = q{$}; # only '$' for $self
718 12 $rbc = q{};
719 }
720 elsif ( $rt eq 'SCALAR' ) { # scalar ref
721 3 $sigil = q{$};
722 }
723 elsif ( $rt eq 'ARRAY' ) { # array ref
724 5 $sigil = q{@};
725 }
726 elsif ( $rt eq 'HASH' ) { # hash ref
727 3 $sigil = q{%};
728 }
729 else {
730 1 die 'Error::Base internal error: bad reftype in _late';
731 };
732
733 # my $key = $sigil?$Error::Base::Late::self->{'$key'}?;
734 23 push @Error::Base::Late::code,
735 ( join q{},
736 $ch1, $key, $ch2,
737 $sigil, $lbc, $ch3, $key, $ch4, $rbc, $ch5,
738 );
739
740 }; ## for keys
741 # ... done unpacking.
742
743 # Do the late interpolation phase.
744 17 push @Error::Base::Late::code,
745 q**,
746 q*<<Heredoc01_Y0uMaYFiReWHeNReaDYGRiDLeY;*,
747 <<Heredoc02_Y0uMaYFiReWHeNReaDYGRiDLeY,
748 $Error::Base::Late::in
749 Heredoc02_Y0uMaYFiReWHeNReaDYGRiDLeY
750 q*Heredoc01_Y0uMaYFiReWHeNReaDYGRiDLeY*,
751 q*#--------------------------------------------------------#*,
752 q**,
753 ;
754
755 # Code is now fully assembled.
756 17 $Error::Base::Late::eval_code =
757 join qq{\n}, @Error::Base::Late::code;
758
759 } ## setup
760 #--------------------------------------------------------------------#
761 { # eval string
762
763 20 $Error::Base::Late::out = eval
764 <<Heredoc03_Y0uMaYFiReWHeNReaDYGRiDLeY;
765 $Error::Base::Late::eval_code
766 Heredoc03_Y0uMaYFiReWHeNReaDYGRiDLeY
767
768 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 16 17
769 1 warn "Error::Base internal warning: in _late eval: $@";
770 1 return $Error::Base::Late::in; # best we can do
771 };
772
773 #~ ##### CASE
774 #~ ##### $Error::Base::Late::self
775 #~ ##### $Error::Base::Late::in
776 #~ ##### @Error::Base::Late::code
777 #~ ##### $Error::Base::Late::eval_code
778 #~ ##### $@
779
780 } ## eval string
781 #--------------------------------------------------------------------#
782
783 # Heredocs add spurious newlines.
784 16 chomp $Error::Base::Late::out;
785 16 chomp $Error::Base::Late::out;
786 #~ my $out = $Error::Base::Late::out;
787 #~ ##### $out;
788 16 return $Error::Base::Late::out;
789 }; ## _late
790  
791 } ## package Error::Base::Late
792  
793 ## END MODULE
794 24 1;
795 #============================================================================#
796 __END__
↑ Summary Base/Cookbook.pm »