File Coverage

blib/lib/Carp/Proxy.pm
Criterion Covered Total %
statement 491 494 99.3
branch 131 136 96.3
condition 26 30 86.6
subroutine 166 166 100.0
pod 20 20 100.0
total 834 846 98.5


line stmt bran cond sub pod time code
1             # -*- cperl -*-
2             package Carp::Proxy;
3 43     44   7811406 use warnings;
  43         478  
  43         1466  
4 43     43   246 use strict;
  43         85  
  43         822  
5 43     43   872 use 5.010;
  43         156  
6              
7             our $VERSION = '0.16';
8              
9 43     43   19490 use Moose;
  43         16744226  
  43         360  
10              
11             has( 'arg',
12             documentation => q(Perl's $ARG (AKA $_) at proxy invocation),
13             is => 'ro',
14             isa => 'Any',
15             required => 1,
16             );
17              
18             has( 'as_yaml',
19             documentation => q(Render message as YAML dump of Carp::Proxy object),
20             is => 'rw',
21             isa => 'Bool',
22             lazy => 1,
23             builder => '_build_as_yaml',
24             );
25              
26             has( 'banner_title',
27             documentation => q(The first word(s) in the banner (at top of message)),
28             is => 'rw',
29             isa => 'Str',
30             lazy => 1,
31             builder => '_build_banner_title',
32             );
33              
34             has( 'begin_hook',
35             documentation => q(Callback before handler is launched),
36             is => 'rw',
37             isa => 'Maybe[CodeRef]',
38             lazy => 1,
39             builder => '_build_begin_hook',
40             );
41              
42             has( 'body_indent',
43             documentation => q(paragraph indent (beyond <header_indent>)),
44             is => 'rw',
45             isa => 'Int',
46             builder => '_build_body_indent',
47             trigger => \&_validate_body_indent,
48             );
49              
50             has( 'child_error',
51             documentation => q(Perl's $CHILD_ERROR (AKA $?) at proxy invocation),
52             is => 'ro',
53             isa => 'Any',
54             required => 1,
55             );
56              
57             has( 'columns',
58             documentation => q( Controls width of banner and filled() paragraphs ),
59             is => 'rw',
60             isa => 'Int',
61             lazy => 1,
62             builder => '_build_columns',
63             trigger => \&_validate_columns,
64             );
65              
66             has( 'context',
67             documentation => q( Stacktrace verbosity/inclusion in message ),
68             is => 'rw',
69             isa => 'Defined',
70             lazy => 1,
71             builder => '_build_context',
72             trigger => \&_validate_context,
73             );
74              
75             has( 'disposition',
76             documentation => q( Throwing semantics ),
77             is => 'rw',
78             isa => 'Defined',
79             lazy => 1,
80             builder => '_build_disposition',
81             trigger => \&_validate_disposition,
82             );
83              
84             has( 'end_hook',
85             documentation => q( Callback just before disposition ),
86             is => 'rw',
87             isa => 'Maybe[CodeRef]',
88             lazy => 1,
89             builder => '_build_end_hook',
90             );
91              
92             has( 'eval_error',
93             documentation => q(Perl's $EVAL_ERROR (AKA $@) at proxy invocation),
94             is => 'ro',
95             isa => 'Any',
96             required => 1,
97             );
98              
99             has( 'exit_code',
100             documentation => q(exit-code harvested by OS when this process dies),
101             is => 'rw',
102             isa => 'Int',
103             lazy => 1,
104             builder => '_build_exit_code',
105             );
106              
107             has( 'fq_proxy_name',
108             documentation => q( Fully-Qualified Proxy-Name (with pkg:: prefix)),
109             is => 'rw',
110             isa => 'Str',
111             required => 1,
112             );
113              
114             has( 'handler_name',
115             documentation => q(The name of the handler requested by the user),
116             is => 'ro',
117             isa => 'Str',
118             required => 1,
119             );
120              
121             has( 'handler_pkgs',
122             documentation => q(Search for handler subroutines in these pkgs),
123             is => 'rw',
124             isa => 'ArrayRef',
125             lazy => 1,
126             builder => '_build_handler_pkgs',
127             traits => ['Array'],
128             handles =>
129             {
130             append_handler_package => 'push',
131             prepend_handler_package => 'unshift',
132             list_handler_pkgs => 'elements',
133             },
134             );
135              
136             has( 'handler_prefix',
137             documentation => q( Prefix applied to <handler_name> before lookup ),
138             is => 'rw',
139             isa => 'Maybe[Str]',
140             lazy => 1,
141             builder => '_build_handler_prefix',
142             );
143              
144             has( 'header_indent',
145             documentation => q( Indent from left margin for paragraph headers ),
146             is => 'rw',
147             isa => 'Int',
148             lazy => 1,
149             builder => '_build_header_indent',
150             trigger => \&_validate_header_indent,
151             );
152              
153             has( 'maintainer',
154             documentation => q( Responsible party's email, phone ),
155             is => 'rw',
156             isa => 'Str',
157             lazy => 1,
158             builder => '_build_maintainer',
159             );
160              
161             has( 'numeric_errno',
162             documentation => q(Perl's $ERRNO (AKA $!) at proxy invocation (0+$!)),
163             is => 'ro',
164             isa => 'Maybe[Num]',
165             required => 1,
166             );
167              
168             has( 'pod_filename',
169             documentation => q(The search for synopsis() POD is in this file),
170             is => 'rw',
171             isa => 'Str',
172             lazy => 1,
173             builder => '_build_pod_filename',
174             );
175              
176             has( 'proxy_filename',
177             documentation => q(The filename of the function requesting the proxy ),
178             is => 'ro',
179             isa => 'Str',
180             required => 1,
181             );
182              
183             has( 'proxy_name',
184             documentation => q(The subname of the generated proxy function),
185             is => 'ro',
186             isa => 'Str',
187             required => 1,
188             );
189              
190             has( 'proxy_package',
191             documentation => q(The userland package that requested the proxy),
192             is => 'ro',
193             isa => 'Str',
194             required => 1,
195             );
196              
197             has( 'section_title',
198             documentation => q( Default title for section header ),
199             is => 'rw',
200             isa => 'Str',
201             lazy => 1,
202             builder => '_build_section_title',
203             );
204              
205             has( 'sections',
206             documentation => q( List of filled/fixed/raw section requests ),
207             is => 'rw',
208             isa => 'ArrayRef[ArrayRef]',
209             traits => ['Array'],
210             handles =>
211             {
212             append_section => 'push',
213             prepend_section => 'unshift',
214             list_sections => 'elements',
215             },
216             builder => '_build_sections',
217             );
218              
219             has( 'string_errno',
220             documentation => q(Perl's $ERRNO (AKA $!) at proxy invocation (''.$!)),
221             is => 'ro',
222             isa => 'Maybe[Str]',
223             required => 1,
224             );
225              
226             has( 'tags',
227             documentation => q( Tag-Value store for exception-related user-data ),
228             is => 'rw',
229             isa => 'HashRef',
230             lazy => 1,
231             builder => '_build_tags',
232             );
233              
234 43     43   344125 no Moose;
  43         107  
  43         258  
235             __PACKAGE__->meta->make_immutable;
236              
237 43     43   11609 use Config;
  43         95  
  43         2171  
238 43     43   261 use Cwd qw( abs_path );
  43         92  
  43         2845  
239 43     43   293 use English qw( -no_match_vars );
  43         83  
  43         390  
240 43     43   18548 use Sub::Name qw( subname );
  43         104  
  43         2639  
241 43     43   272 use overload '""' => \&_overload_stringification;
  43         95  
  43         503  
242 43     43   35752 use Pod::Usage qw( pod2usage );
  43         2190405  
  43         4632  
243 43     43   26680 use Readonly;
  43         171516  
  43         2496  
244 43     43   19039 use YAML::XS qw( Dump );
  43         123932  
  43         2564  
245              
246             #-----
247             # We use an internal proxy to throw our own errors. It cannot be defined
248             # until later, but this lets us use bareword invocation.
249             #-----
250 43     43   23989 use subs qw( error );
  43         1093  
  43         239  
251              
252             Readonly::Scalar my $NEWLINE => ($OSNAME =~ / win /xi) ? "\r\n" : "\n";
253              
254             my @VALID_CONTEXT_STRINGS = qw( none die croak confess internals );
255             my $VALID_CONTEXT_REX = _fixed_string_rex( @VALID_CONTEXT_STRINGS );
256              
257             my @VALID_DISPOSITION_STRINGS = qw( return warn die );
258             my $VALID_DISPOSITION_REX = _fixed_string_rex( @VALID_DISPOSITION_STRINGS );
259              
260             #----- Some symbolic constants for indexing the list returned by caller()
261             my $CALLER_PACKAGE;
262             my $CALLER_FILENAME;
263             my $CALLER_LINE;
264             my $CALLER_SUBROUTINE;
265             BEGIN {
266 43     43   7794 Readonly::Scalar $CALLER_PACKAGE => 0;
267 43         1413 Readonly::Scalar $CALLER_FILENAME => 1;
268 43         924 Readonly::Scalar $CALLER_LINE => 2;
269 43         876 Readonly::Scalar $CALLER_SUBROUTINE => 3;
270             }
271              
272 424     424   12490 sub _overload_stringification { return $_[0]->render_message; }
273              
274 419     419   9604 sub _build_as_yaml { return 0; }
275 375     375   8564 sub _build_banner_title { return 'Fatal'; }
276 519     519   11882 sub _build_begin_hook { return undef; }
277 570     570   14818 sub _build_body_indent { return 2; }
278 442     442   9862 sub _build_columns { return 78; }
279 378     378   8577 sub _build_context { return 'confess'; }
280 422     422   9753 sub _build_disposition { return 'die'; }
281 449     449   10150 sub _build_end_hook { return undef; }
282 422     422   9571 sub _build_exit_code { return 1; }
283 522     522   17092 sub _build_handler_pkgs { return []; }
284 504     504   11714 sub _build_handler_prefix { return undef; }
285 447     447   10423 sub _build_header_indent { return 2; }
286 29     29   670 sub _build_maintainer { return ''; }
287 28     28   734 sub _build_pod_filename { return $_[0]->proxy_filename; }
288 18     18   82 sub _build_proxy_name { return 'fatal'; }
289 129     129   3214 sub _build_section_title { return 'Description'; }
290 541     541   15891 sub _build_sections { return []; }
291 1     1   56 sub _build_tags { return {}; }
292              
293             sub _validate_body_indent {
294 7     7   20 my( $self, $indent ) = @_;
295              
296 7 100       35 error 'negative_body_indentation', $indent
297             if $indent < 0;
298              
299 5         114 return;
300             }
301              
302             sub _cp_negative_body_indentation {
303 2     2   8 my( $cp, $indent ) = @_;
304              
305 2         10 $cp->_disallowed_setting( 'body_indent', $indent );
306              
307 2         6 return;
308             }
309              
310             sub _validate_columns {
311 17     17   44 my( $self, $columns ) = @_;
312              
313 17 100       53 error 'insufficient_columns', $columns
314             if $columns <= 0;
315              
316 13         365 return;
317             }
318              
319             sub _cp_insufficient_columns {
320 4     4   14 my( $cp, $columns ) = @_;
321              
322 4         17 $cp->_disallowed_setting( 'columns', $columns );
323              
324 4         11 return;
325             }
326              
327             sub _validate_context {
328 85     85   218 my( $self, $context ) = @_;
329              
330 85 100 100     911 error 'invalid_context_setting', $context
331             if 'CODE' ne ref $context
332             and $context !~ $VALID_CONTEXT_REX;
333              
334 77         1909 return;
335             }
336              
337             sub _cp_invalid_context_setting {
338 8     8   24 my( $cp, $context ) = @_;
339              
340 8         33 $cp->_disallowed_setting( 'context', $context );
341              
342 8         28 return;
343             }
344              
345             sub _validate_disposition {
346 44     44   119 my( $self, $disposition ) = @_;
347              
348 44 100 100     350 error 'invalid_disposition_setting', $disposition
349             if 'CODE' ne ref $disposition
350             and $disposition !~ $VALID_DISPOSITION_REX;
351              
352             return
353 38         972 }
354              
355             sub _cp_invalid_disposition_setting {
356 6     6   19 my( $cp, $disposition ) = @_;
357              
358 6         25 $cp->_disallowed_setting( 'disposition', $disposition );
359              
360 6         20 return;
361             }
362              
363             sub _validate_header_indent {
364 7     7   18 my( $self, $indent ) = @_;
365              
366 7 100       24 error 'negative_header_indentation', $indent
367             if $indent < 0;
368              
369 5         115 return;
370             }
371              
372             sub _cp_negative_header_indentation {
373 2     2   7 my( $cp, $indent ) = @_;
374              
375 2         11 $cp->_disallowed_setting( 'header_indent', $indent );
376              
377 2         6 return;
378             }
379              
380 0         0 BEGIN {
381 43     43   39470 Readonly::Scalar my $POD_USAGE_SPECIFIC_SECTION => 99;
382              
383             sub _disallowed_setting {
384 22     22   73 my( $cp, $attr, $value ) = @_;
385              
386 22         81 my $req = _display_code_or_string( $value );
387              
388 22         125 $cp->filled(<<"EOF");
389             The requested setting of '$req' for the '$attr' attribute is not allowed.
390             EOF
391              
392 22         154 $cp->synopsis( -verbose => $POD_USAGE_SPECIFIC_SECTION,
393             -sections => ["ATTRIBUTES/$attr"],
394             );
395 22         116 return;
396             }
397             }
398              
399             sub _display_code_or_string {
400 31     31   172 my( $value ) = @_;
401              
402 31 100       181 my $req
    100          
403             = not( defined $value ) ? '(undef)'
404             : ref( $value ) ? 'REF: ' . ref( $value )
405             : $value;
406              
407 31         89 return $req;
408             }
409              
410             sub _fixed_string_rex {
411 86     86   251 my( @strings ) = @_;
412              
413 86         300 my $alternations = join ' | ', @strings;
414              
415 86         3180 return qr{
416             \A
417             (?: $alternations )
418             \z
419             }x;
420             }
421              
422             sub import {
423 155     155   565830 my( $class, @proxy_attrlist_pairs ) = @_;
424              
425 155         320 my %by_proxyname;
426              
427             #-----
428             # If there are no args then the user is implicitly requesting a
429             # proxy named 'fatal'.
430             #-----
431 155 100       993 if (not @proxy_attrlist_pairs) {
    100          
    100          
432              
433 19         697 $by_proxyname{ $class->_build_proxy_name() } = {};
434             }
435             #----- Just one argument names a proxy with default attributes
436             elsif ( 1 == @proxy_attrlist_pairs ) {
437              
438 1         3 $by_proxyname{ $proxy_attrlist_pairs[0] } = {};
439             }
440             #----- Otherwise there had better be pairs...
441             elsif ( @proxy_attrlist_pairs % 2 ) {
442              
443 1         4 error 'unmatched_proxy_arglist', @proxy_attrlist_pairs;
444             }
445             else {
446              
447 134         481 %by_proxyname = @proxy_attrlist_pairs;
448             }
449              
450 154         1479 while(my($proxy_name, $attributes) = each %by_proxyname ) {
451              
452 193         608 $class->_create_proxy( $proxy_name, $attributes );
453             }
454              
455 154         3418067 return;
456             }
457              
458             sub _cp_unmatched_proxy_arglist {
459 1     1   3 my( $cp, @mismatched_list ) = @_;
460              
461 1         2 my $count = @mismatched_list;
462              
463 1         3 my $pairings = '';
464 1         5 while( my( $proxy_name, $attr_val_hashref ) =
465             splice @mismatched_list, 0, 2
466             ) {
467              
468 2 100       5 $attr_val_hashref = ''
469             if not defined $attr_val_hashref;
470              
471 2         11 $pairings .= "$proxy_name => $attr_val_hashref" . $NEWLINE;
472             }
473              
474 1         6 $cp->filled( <<"EOF" );
475             Proxy creation arguments must come in (proxy, arglist) pairs. Each pair
476             should take the form:
477              
478             proxy name => hashref
479              
480             An odd number of arguments were provided:
481              
482             $pairings
483             EOF
484 1         2 return;
485             }
486              
487             sub _create_proxy {
488 193     193   444 my( $class, $proxy_name, $requested_attributes, ) = @_;
489              
490             #----- caller(1) should be import() called from userland
491 193         609 my ( $user_pkg, $user_fname ) = (caller 1)
492             [ $CALLER_PACKAGE, $CALLER_FILENAME ];
493              
494 193         5237 my $fq_proxy_name = $user_pkg . '::' . $proxy_name;
495              
496             #-----
497             # The *configuration* builtin handler returns a reference to this
498             # closure hash.
499             #-----
500             my %attrs = ( proxy_filename => $user_fname,
501             proxy_name => $proxy_name,
502             proxy_package => $user_pkg,
503             fq_proxy_name => $fq_proxy_name,
504 193         915 %{ $requested_attributes },
  193         1105  
505             );
506              
507 193         620 my $proxy_coderef = _define_proxy( $class, \%attrs );
508              
509             #-----
510             # Name the coderef so that caller() reports the name instead of ANON.
511             #-----
512 193         1480 subname( $fq_proxy_name, $proxy_coderef );
513              
514             #----- Install the proxy in userland
515             {
516 43     43   22183 no strict 'refs';
  43         121  
  43         104999  
  193         392  
517 193         314 *{ $fq_proxy_name } = $proxy_coderef;
  193         978  
518             }
519              
520 193         961 return;
521             }
522              
523             sub _define_proxy {
524 193     193   428 my( $class, $attrs ) = @_;
525              
526             return sub {
527 594     594   3914581 my( $handler_name, @optional_arguments ) = @_;
        594      
        593      
        584      
        233      
        193      
        187      
        187      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
        139      
528              
529 594 100       1799 return $attrs
530             if $handler_name eq '*configuration*';
531              
532             my $cp = $class->new({
533             arg => $ARG, # $_
534             child_error => $CHILD_ERROR, # $?
535             eval_error => $EVAL_ERROR, # $@
536             numeric_errno => 0 + $ERRNO, # $!
537             string_errno => '' . $ERRNO, # $!
538             handler_name => $handler_name,
539 584         2952 %{ $attrs },
  584         21775  
540             });
541              
542 526         16293 $cp->append_handler_package( $cp->proxy_package );
543              
544 526         13148 my $begin_hook = $cp->begin_hook;
545 526 100       1208 $begin_hook->( $cp )
546             if defined $begin_hook;
547              
548 526         1836 $cp->call( $handler_name, @optional_arguments );
549              
550 460         1612 $cp->add_context;
551              
552 455         11246 my $end_hook = $cp->end_hook;
553 455 100       1035 $end_hook->( $cp )
554             if defined $end_hook;
555              
556 455         1172 return $cp->perform_disposition;
557 193         1158 };
558             }
559              
560             sub perform_disposition {
561 455     455 1 844 my( $self ) = @_;
562              
563 455         10772 my $disposition = $self->disposition;
564              
565 455 100       1942 if ( not defined $disposition ) {
    100          
    100          
    100          
    100          
566              
567 1         6 error 'unknown_disposition', $self;
568             }
569             elsif ( 'CODE' eq ref $disposition ) {
570              
571 8         35 return $disposition->( $self );
572             }
573             elsif ( 'warn' eq $disposition ) {
574              
575             ## no critic( ErrorHandling::RequireCarping )
576 2         19 warn $self;
577 2         18 return ();
578             }
579             elsif ( 'die' eq $disposition ) {
580              
581 423         10043 local $ERRNO = $self->exit_code;
582              
583             ## no critic( ErrorHandling::RequireCarping )
584 423         4842 die $self;
585             }
586             elsif ( 'return' ne $disposition ) {
587              
588 1         5 error 'unknown_disposition', $self;
589             }
590              
591 20         306 return $self;
592             }
593              
594             sub _cp_unknown_disposition {
595 2     2   5 my( $cp, $original_cp ) = @_;
596              
597 2         50 my $disp = _display_code_or_string( $original_cp->disposition );
598              
599 2         11 my $possibilities = join ' ', @VALID_DISPOSITION_STRINGS;
600              
601 2         7 _unsupported_attribute_value( $cp,
602             $original_cp,
603             'disposition',
604             $disp,
605             $possibilities );
606              
607 2         4 return;
608             }
609              
610             sub _unsupported_attribute_value {
611 6     6   22 my( $cp, $original_cp, $attr, $val, $possibilities ) = @_;
612              
613 6         33 $cp->filled(<<"EOF");
614             The program has encountered an error. The developers attempted to
615             diagnose the error but they made a mistake during the diagnosis.
616             There are now two errors. You should complain!
617              
618             The secondary error is an attempt to use an unsupported value for
619             an attribute.
620              
621             $attr: '$val'
622              
623             The supported values for $attr, beyond a CodeRef, are:
624              
625             $possibilities
626             EOF
627              
628 6         23 $cp->_describe_primary_error( $original_cp );
629 6         12 return;
630             }
631              
632             sub _describe_primary_error {
633 14     14   30 my( $self, $original_cp ) = @_;
634              
635 14         51 $self->contact_maintainer();
636              
637 14         45 $self->filled(<<'EOF', 'Primary Error' );
638             The remaining sections attempt to describe the original error.
639             EOF
640              
641 14         347 my $st = $original_cp->section_title;
642 14         504 foreach my $section ( $original_cp->list_sections ) {
643              
644 8 100       26 my $primary_title = defined( $section->[2] )
645             ? $section->[2]
646             : $st;
647              
648             #----- Turn 'Description' into 'Primary Description' etc.
649 8         31 $primary_title =~ s/ ^ /Primary /x;
650              
651 8         254 $self->append_section([ $section->[0],
652             $section->[1],
653             $primary_title ]);
654             }
655              
656 14         30 return;
657             }
658              
659             sub add_context {
660 460     460 1 982 my( $self ) = @_;
661              
662 460         12341 my $context = $self->context;
663              
664 460 100       2474 if ( not defined $context ) { error 'unknown_context', $self; }
  2 100       8  
    100          
    100          
    100          
    100          
    100          
665 4         12 elsif ( 'CODE' eq ref $context ) { $context->($self); }
666 4         14 elsif ( 'die' eq $context ) { $self->_die_context(); }
667 6         25 elsif ( 'croak' eq $context ) { $self->_croak_context(); }
668 387         952 elsif ( 'confess' eq $context ) { $self->_confess_context(); }
669 46         159 elsif ( 'internals' eq $context ) { $self->_internals_context(); }
670 2         10 elsif ( 'none' ne $context ) { error 'unknown_context', $self; }
671              
672 455         740 return;
673             }
674              
675             sub _die_context {
676 4     4   10 my( $self ) = @_;
677              
678 4         10 my $caller_index = $self->_find_proxy_frame();
679              
680 4         18 my( $file, $line, $subr ) = $self->_file_line_subr( $caller_index );
681              
682 4         14 $self->_single_frame_context( $file, $line, $subr );
683              
684 4         8 return;
685             }
686              
687             sub _croak_context {
688 6     6   20 my( $self ) = @_;
689              
690             #-----
691             # 'croak' semantics asks us to go back one additional frame from
692             # where the proxy was called.
693             #-----
694 6         37 my $caller_index = 1 + $self->_find_proxy_frame();
695              
696 6         36 my( $file, $line, $subr ) = $self->_file_line_subr( $caller_index );
697              
698             #-----
699             # If the proxy was invoked from top-level code then there is no
700             # caller to report. In this case we fallback to 'die' semantics.
701             #-----
702 6 100       22 if (not defined $file) {
703              
704 1         37 --$caller_index;
705              
706 1         30 ( $file, $line, $subr ) = $self->_file_line_subr( $caller_index );
707             }
708              
709 6         41 $self->_single_frame_context( $file, $line, $subr );
710 6         13 return;
711             }
712              
713             sub _confess_context {
714 387     387   660 my( $self ) = @_;
715              
716 387         832 my $caller_index = $self->_find_proxy_frame();
717              
718 386         1196 $self->_multi_frame_context( $caller_index );
719 386         643 return;
720             }
721              
722             sub _internals_context {
723 46     46   120 my( $self ) = @_;
724              
725 46         202 $self->_multi_frame_context( undef );
726 46         95 return;
727             }
728              
729             sub _cp_unknown_context {
730 4     4   8 my( $cp, $original_cp ) = @_;
731              
732 4         96 my $context = _display_code_or_string( $original_cp->context );
733              
734 4         20 my $possibilities = join ' ', @VALID_CONTEXT_STRINGS;
735              
736 4         20 _unsupported_attribute_value( $cp,
737             $original_cp,
738             'context',
739             $context,
740             $possibilities );
741              
742 4         6 return;
743             }
744              
745             sub _find_proxy_frame {
746 402     402   649 my( $self ) = @_;
747              
748 402         10037 my $fqp = $self->fq_proxy_name;
749              
750 402         724 my $frame = 1;
751 402         557 while (1) {
752              
753 1214         2863 my( $sub ) = (caller $frame)[ $CALLER_SUBROUTINE ];
754              
755 1214 100       47505 error 'no_proxy_frame', $self
756             if not defined $sub;
757              
758             last
759 1213 100       2526 if $sub eq $fqp;
760              
761 812         1220 ++$frame;
762             }
763              
764 401         901 return $frame
765             }
766              
767             sub _cp_no_proxy_frame {
768 1     1   8 my( $cp, $original_cp ) = @_;
769              
770 1         4 my $frames = '';
771 1         2 for( my $i=0; 1; ++$i ) {
772              
773 10         17 my( $subr ) = (caller $i)[ $CALLER_SUBROUTINE ];
774              
775             last
776 10 100       446 if not defined $subr;
777              
778 9         34 $frames .= " $i => $subr" . $NEWLINE;
779             }
780              
781 1         30 my $proxy = $original_cp->fq_proxy_name;
782              
783 1         7 $cp->filled(<<"EOF");
784             The callstack does not appear to contain a frame for the proxy. This is
785             an internal error. The proxy name that was the target of the search is:
786              
787             $proxy
788              
789             The following list contains the stackframe index and the associated subrotine
790             name:
791             EOF
792              
793 1         5 $cp->fixed( $frames, '' );
794              
795 1         4 $cp->_describe_primary_error( $original_cp );
796 1         11 return;
797             }
798              
799             sub _file_line_subr {
800 2133     2133   3898 my( $self, $caller_index ) = @_;
801              
802 2133         2934 my( $file, $line, $subr );
803              
804 2133         3247 eval{
805 2133         5292 ( $file, $line, $subr ) =
806             (caller 1 + $caller_index)[
807             $CALLER_FILENAME,
808             $CALLER_LINE,
809             $CALLER_SUBROUTINE,
810             ];
811             };
812              
813 2133 50       124295 return $EVAL_ERROR ? () : ($file, $line, $subr);
814             }
815              
816             sub _single_frame_context {
817 10     10   30 my( $self, $file, $line, $subr ) = @_;
818              
819 10         35 my $whence = $self->_make_frame_report( $file, $line, $subr );
820              
821 10         51 $self->fixed( $whence, 'Exception' );
822              
823 10         13 return;
824             }
825              
826             sub _section_indent {
827 2573     2573   4532 my( $self ) = @_;
828              
829 2573         58031 return $self->body_indent + $self->header_indent;
830             }
831              
832             sub _make_frame_report {
833 1700     1700   3547 my( $self, $file, $line, $subr ) = @_;
834              
835             #----- We want to report just the basename of the subroutine (no pkg)
836 1700         7347 $subr =~ s/\A .+ :: //x;
837              
838             #-----
839             # If the filename is short enough then it will appear on the same line
840             # as the subr and the line number. If the filename is too long then
841             # we put the filename on the next line and indent by an extra amount.
842             # The extra amount is an additional body_indent, or 2 spaces if there
843             # is no body indent.
844             #-----
845 1700   100     43962 my $file_indent = $self->body_indent || 2;
846              
847 1700         3456 my $section_indent = $self->_section_indent;
848              
849 1700         4471 my $whence = "$subr called from line $line of";
850              
851             #-----
852             # If we were to add the filename to whence then it would need to be
853             # separated by a space (+ 1). Also we need to account for the fact
854             # that the report will form the body of a fixed() section, so there
855             # will be a section indent in front of whence.
856             #-----
857 1700         2964 my $length_with_file =
858             $section_indent + length( $whence ) + 1 + length( $file );
859              
860 1700 100       38464 $whence .= ($self->columns >= $length_with_file )
861             ? ' '
862             : $NEWLINE . (' ' x $file_indent);
863              
864 1700         3444 $whence .= $file . $NEWLINE;
865              
866 1700         4764 return $whence;
867             }
868              
869             sub _multi_frame_context {
870 432     432   936 my( $self, $starting_frame ) = @_;
871              
872             #-----
873             # We have to add an extra frame to account for ourselves, unless
874             # they want 'internals', in which case they want everything.
875             #-----
876 432 100       1086 $starting_frame = defined( $starting_frame )
877             ? 1 + $starting_frame
878             : 0;
879              
880 432         759 my $stacktrace = '';
881 432         793 for( my $frame = $starting_frame; 1; ++$frame ) {
882              
883 2122         4529 my( $file, $line, $subr ) = $self->_file_line_subr( $frame );
884              
885             last
886 2122 100       5145 if not defined $file;
887              
888 1690         3727 $stacktrace .= $self->_make_frame_report( $file, $line, $subr );
889             }
890              
891 432         1237 $self->fixed( $stacktrace, 'Stacktrace' );
892              
893 432         688 return;
894             }
895              
896              
897             sub filled {
898 416     416 1 1303 my( $self, $content, $title ) = @_;
899              
900 416         15299 $self->append_section([ 'filled_section', $content, $title ]);
901 416         1183 return;
902             }
903              
904             sub fixed {
905 501     501 1 1273 my( $self, $content, $title ) = @_;
906              
907 501         18148 $self->append_section([ 'fixed_section', $content, $title ]);
908 501         930 return;
909             }
910              
911             sub raw {
912 1     1 1 9 my( $self, $content ) = @_;
913              
914 1         38 $self->append_section([ 'raw_section', $content ]);
915 1         2 return;
916             }
917              
918             sub filled_section {
919 406     406 1 854 my( $self, $content, $title ) = @_;
920              
921 406 100       1388 return ''
922             if $content =~ /\A \s* \z/x;
923              
924 405         1045 my $buffer = $self->header( $title );
925 405         10065 my $columns = $self->columns;
926              
927 405         1690 my @paragraphs = split / (?: \r? \n){2,} /x, $content;
928              
929 405         984 my $section_indent = ' ' x $self->_section_indent;
930              
931 405         1016 foreach my $p (@paragraphs) {
932              
933             #----- You need words to make a paragraph
934             next
935 443 100       1368 if $p =~ /\A \s* \z/x;
936              
937 442         1037 $buffer .= _fill_paragraph( $section_indent, $columns, $p );
938             }
939              
940 405         1450 return $buffer;
941             }
942              
943             sub _fill_paragraph {
944 442     442   1010 my( $indent, $columns, $text ) = @_;
945              
946             #----- Whitespace, where carriage-returns and newlines don't count.
947 442         1638 my( $leading_ws ) = $text =~ /\A ( [^\r\n\S]* ) /x;
948              
949             #----- Any non-tab whitespace (vertical tabs, formfeeds etc) become spaces
950 442         999 $leading_ws =~ tr/\t/ /c;
951 442         967 $leading_ws = $indent . _expand_tabs( $leading_ws );
952              
953 442         2175 my @words = split ' ', $text;
954 442         958 my $line = $leading_ws . shift @words;
955 442         706 my $buffer = '';
956              
957 442         765 foreach my $w (@words) {
958              
959 4000 100       6289 if (( length( $line ) + 1 + length( $w )) <= $columns) {
960              
961 3855         5686 $line .= ' ' . $w;
962             }
963             else {
964              
965 145         339 $buffer .= $line . $NEWLINE;
966              
967             #----- Always eat one word, even if it exceeds columns
968 145         252 $line = $leading_ws . $w;
969             }
970             }
971              
972 442         1034 $buffer .= $line . $NEWLINE . $NEWLINE;
973 442         1528 return $buffer;
974             }
975              
976 0         0 BEGIN{
977 43     43   378 Readonly::Scalar my $TAB => 8;
978              
979             sub _expand_tabs {
980 2735     2735   4259 my( $text ) = @_;
981              
982             #----- Keep replacing the first tab as long as there are tabs
983 2735         5552 1 while $text =~ s{ \A ( [^\t]* ) \t }
  10         61  
984             { $1 . (' ' x ($TAB - (length($1) % $TAB))) }xe;
985 2735         6603  
986             return $text;
987             }
988             }
989              
990 468     468 1 909 sub fixed_section {
991             my( $self, $content, $title ) = @_;
992 468         948  
993 468         1068 my $buffer = $self->header( $title );
994             my $indent = ' ' x $self->_section_indent;
995 468         3619  
996 468         1118 my @lines = split / \r? \n /x, $content;
997             foreach my $l (@lines) {
998 2293         3823  
999             $buffer
1000             .= $indent
1001             . _expand_tabs( $l )
1002             . $NEWLINE;
1003             }
1004 468         4435  
1005 468         1026 $buffer =~ s/ \s+ \z//x;
1006             $buffer .= $NEWLINE . $NEWLINE;
1007 468         2193  
1008             return $buffer;
1009             }
1010              
1011 1     1 1 3 sub raw_section {
1012             my( $self, $content ) = @_;
1013 1         3  
1014             return $content;
1015             }
1016              
1017 2     2   4 sub _cp_missing_identifier {
1018             my( $cp ) = @_;
1019 2         7  
1020             $cp->filled(<<'EOF');
1021             The 'name' argument for identifier_presentation() is empty or undef.
1022 2         3 EOF
1023             return;
1024             }
1025              
1026 420     420 1 1354 sub identifier_presentation {
1027             my( $class, $name ) = @_;
1028 420 100 100     1730  
1029             error 'missing_identifier'
1030             if not( defined $name )
1031             or not( length $name );
1032 418         1015  
1033 418         1383 $name =~ s/ _ / /xg;
  3         16  
1034 418         1182 $name =~ s{ ([[:lower:]]) ([[:upper:]]) }{ "$1 $2" }xge;
1035             $name = lc $name;
1036 418         1257  
1037             return $name;
1038             }
1039              
1040 871     871 1 1557 sub header {
1041             my( $self, $title ) = @_;
1042 871 100       1708  
1043             if ( defined $title ) {
1044 757 100       1621  
1045             return ''
1046             if not length $title;
1047             }
1048             else {
1049 114         3165  
1050             $title = $self->section_title;
1051             }
1052 861         22437  
1053             my $header
1054             = (' ' x $self->header_indent)
1055             . "*** ${title} ***$NEWLINE";
1056 861         1940  
1057             return $header;
1058             }
1059              
1060 419     419 1 870 sub banner {
1061             my( $self ) = @_;
1062 419         9745  
1063             my $standout = ('~' x $self->columns) . $NEWLINE;
1064 419         10040  
1065             my $banner
1066             = $standout
1067             . $self->banner_title
1068             . ' << '
1069             . $self->identifier_presentation( $self->handler_name )
1070             . ' >>'
1071             . $NEWLINE
1072             . $standout;
1073 419         985  
1074             return $banner;
1075             }
1076              
1077 31     31 1 178 sub synopsis {
1078             my( $self, @tag_value_pairs ) = @_;
1079 31 100       121  
1080             error 'odd_synopsis_augmentation', @tag_value_pairs
1081             if @tag_value_pairs % 2;
1082 30         70  
1083 30         101 my $buffer = '';
1084             my $fd = _open_string_as_file( \$buffer );
1085 30         72  
1086 30         963 eval {
1087             pod2usage( -input => $self->pod_filename,
1088             -output => $fd,
1089             -exitval => 'NOEXIT',
1090             -verbose => 0,
1091             @tag_value_pairs );
1092             };
1093 30 100       8465855  
1094             if ($EVAL_ERROR) {
1095 1         2  
  1         4  
1096 1         25 my $ignore = print {$fd} <<"EOF";
1097             Unable to create synopsis section from file '@{[ $self->pod_filename ]}':
1098              
1099             $EVAL_ERROR
1100             EOF
1101             }
1102 30         157  
1103             _close_string_file( $fd );
1104 30         186  
1105 30         214 $self->fixed( $buffer, 'Synopsis' );
1106             return;
1107             }
1108              
1109 30     30   70 sub _open_string_as_file {
1110             my( $string_ref ) = @_;
1111 30 50   187   805  
  4         34  
  4         8  
  4         28  
1112             open my( $fd ), '>', $string_ref
1113             or error 'cannot_open_string';
1114 30         3598  
1115             return $fd;
1116             }
1117              
1118 1     1   3 sub _cp_cannot_open_string {
1119             my( $cp ) = @_;
1120 1         3  
1121             $cp->filled( <<"EOF" );
1122             Unable to create a file descriptor using a string as the storage medium.
1123 1         7 EOF
1124 1         2 $cp->errno_section;
1125             return;
1126             }
1127              
1128 30     30   96 sub _close_string_file {
1129             my( $fd ) = @_;
1130 30 50       159  
1131             close $fd
1132             or error 'cannot_close_string';
1133 30         75  
1134             return;
1135             }
1136              
1137 1     1   3 sub _cp_cannot_close_string {
1138             my( $cp ) = @_;
1139 1         4  
1140             $cp->filled( <<"EOF" );
1141             Unable to close a file-descriptor that points to a string buffer as the
1142             storage medium.
1143 1         3 EOF
1144 1         2 $cp->errno_section;
1145             return;
1146             }
1147              
1148 1     1   3 sub _cp_odd_synopsis_augmentation {
1149             my( $cp, @tag_value_pairs ) = @_;
1150 1         3  
1151 1         3 my $tv_report = '';
1152             while( @tag_value_pairs >= 2 ) {
1153 2         7  
1154             my( $t, $v ) = splice @tag_value_pairs, 0, 2;
1155 2         21  
1156             $tv_report .= "$t => $v" . $NEWLINE;
1157             }
1158 1         4  
1159             $tv_report .= "$tag_value_pairs[0] =>" . $NEWLINE;
1160 1         7  
1161             $cp->filled( <<"EOF" );
1162             The synopsis() method allows users to supplement pod2usage() arguments
1163             with tag-value pairs. The supplied arguments did not come in pairs; there
1164             are an odd number.
1165              
1166             $tv_report
1167 1         2 EOF
1168             return;
1169             }
1170              
1171 5     5 1 24 sub usage {
1172             my( $self ) = @_;
1173 5         13  
1174             for(my $index = $self->_find_proxy_frame(); 1; ++$index ) {
1175 13         30  
1176             my( $subr ) = (caller $index)[ $CALLER_SUBROUTINE ];
1177 13 100       838  
1178             error 'no_usage_documentation', $self
1179             if not defined $subr;
1180              
1181 12         59 #----- Discard any package qualifiers
1182             $subr =~ s/ \A .* :: //x;
1183 12         27  
1184             my $handler = 'usage_' . $subr;
1185 12         28  
1186             my $where = $self->_locate_handler( $handler );
1187              
1188 12 100       28 next
1189             if not $where;
1190 4         16  
1191 4         12 $where->( $self );
1192             last;
1193             }
1194 4         15  
1195             return;
1196             }
1197              
1198 1     1   4 sub _cp_no_usage_documentation {
1199             my( $cp, $original_cp ) = @_;
1200 1         4  
1201             $cp->filled(<<"EOF");
1202             There was an error. The developers caught the error and attempted to
1203             describe it. Part of the description was supposed to be provided by a
1204             "usage" subroutine. Unfortunately they forgot to define the usage
1205             subroutine. Now there are two errors. You shoud complain!
1206             EOF
1207 1         26  
1208             my $prefix = $original_cp->handler_prefix;
1209 1   50     8  
1210             $prefix //= '(undef)';
1211 1         3  
1212 1         33 $cp->fixed(<<"EOF", 'Missing Handler - Secondary Error');
1213             handler_pkgs: @{[ join ' : ', $original_cp->list_handler_pkgs ]}
1214             handler_prefix: $prefix
1215             EOF
1216 1         5  
1217 1         2 $cp->_describe_primary_error( $original_cp );
1218             return;
1219             }
1220 0         0  
1221 43     43   70519 BEGIN{
1222 43         1267 Readonly::Scalar my $ONE_BYTE => 8;
1223 43         924 Readonly::Scalar my $SIGNAL_MASK => 0x7F;
1224             Readonly::Scalar my $CORE_DUMP => 0x80;
1225              
1226 256     256 1 1211 sub decipher_child_error {
1227             my( $cp ) = shift;
1228 256 50 33     6572  
1229             my $child_error = ( @_ and defined( $_[0] ) and $_[0] =~ /\A \d+ \z/x )
1230             ? shift
1231             : $cp->child_error;
1232 256 100       567  
1233             if ( 0 == $child_error ) {
1234 1         4  
1235             $cp->filled( 'The child process completed normally (exit code 0).',
1236 1         2 'Process Succeeded' );
1237             return;
1238             }
1239 255         476  
1240 255 100       509 my $signal = $child_error & $SIGNAL_MASK;
1241             if ($signal) {
1242 254         4184  
1243 254         3300 my @names = split ' ', $Config{sig_name};
1244             my @nums = split ' ', $Config{sig_num};
1245 254         541  
1246 254         6869 my %by_num;
1247             @by_num{ @nums } = @names;
1248 254         627  
1249             my $sig_name = $by_num{ $signal };
1250 254 100       1013  
    100          
1251             my $msg
1252             = 'The child process was terminated by '
1253             . ((defined $sig_name)
1254             ? "SIG$sig_name (signal $signal)."
1255             : "signal $signal.")
1256             . (($child_error & $CORE_DUMP)
1257             ? ' There was a core dump.'
1258             : '');
1259 254         691  
1260 254         4201 $cp->filled( $msg, 'Process terminated by signal' );
1261             return;
1262             }
1263 1         4  
1264             my $exit_code = $child_error >> $ONE_BYTE;
1265 1         6  
1266             $cp->filled( "The child process terminated with exit code $exit_code.",
1267 1         3 'Process returns failing status' );
1268             return;
1269             }
1270             }
1271              
1272 2     2 1 13 sub filename {
1273             my( $cp, $file, $title ) = @_;
1274 2 100       6  
1275             $title = 'Filename'
1276             if not defined $title;
1277 2         16  
1278 2         3 $cp->_abs_path_section( $file, $title );
1279             return;
1280             }
1281              
1282 2     2 1 14 sub directory {
1283             my( $cp, $dir, $title ) = @_;
1284 2 100       6  
1285             $title = 'Directory'
1286             if not defined $title;
1287 2         6  
1288 2         4 $cp->_abs_path_section( $dir, $title );
1289             return;
1290             }
1291              
1292 4     4   10 sub _abs_path_section {
1293             my( $cp, $entry, $title ) = @_;
1294 4         5  
1295             my $path;
1296              
1297             #-----
1298             # On *nix abs_path() appears to return undef if it has trouble. On
1299             # Windows it appears to throw. Docs are ambiguous.
1300 4         8 #-----
  4         157  
1301             eval{ $path = abs_path( $entry ); };
1302 4 100 66     25  
1303             $path = $entry
1304             if not( defined $path )
1305             or $EVAL_ERROR;
1306 4         15  
1307             $cp->fixed( $path, $title );
1308 4         8  
1309             return;
1310             }
1311              
1312 6     6 1 36 sub errno_section {
1313             my( $cp, $title ) = @_;
1314 6         211  
1315             my $string_errno = $cp->string_errno;
1316              
1317 6 100 100     51 return
1318             if not( defined $string_errno )
1319             or not( length $string_errno );
1320 2 100       7  
1321             $title = 'System Diagnostic'
1322             if not defined $title;
1323 2         7  
1324 2         3 $cp->filled( $string_errno, $title );
1325             return;
1326             }
1327              
1328 424     424 1 849 sub render_message {
1329             my( $self ) = @_;
1330 424 100       11032  
1331             return Dump( $self )
1332             if $self->as_yaml;
1333 420         1194  
1334             my $buffer = $self->banner;
1335 420         14554  
1336 420         1052 my @sections = $self->list_sections;
1337             foreach my $s (@sections) {
1338 875         1291  
  875         2231  
1339             my( $meth, @args ) = @{ $s };
1340 875         2778  
1341             $buffer .= $self->$meth( @args );
1342 420         4082 }
1343             return $buffer;
1344             }
1345              
1346 528     528 1 1366 sub call {
1347             my( $self, $handler, @args ) = @_;
1348 528         1271  
1349             my $where = $self->_locate_handler( $handler );
1350 528 100       1206  
1351             error 'embarrassed_developers', $self, $handler
1352             if not $where;
1353 522         1721  
1354             $where->( $self, @args );
1355 462         13232  
1356             return;
1357             }
1358              
1359 540     540   1026 sub _locate_handler {
1360             my( $self, $handler_name ) = @_;
1361 540         758  
1362             my $coderef;
1363 540 100       1423  
1364             if ($handler_name =~ /\A [*] ( .+ ) [*] \z/x) {
1365 13         36  
1366 13         72 my $builtin = $1;
1367             $coderef = $self->can( '_builtin_' . $builtin );
1368             }
1369             else {
1370 527         13201  
1371             my $handler_prefix = $self->handler_prefix;
1372 527 100       1754  
1373             my @prefixes = (defined $handler_prefix)
1374             ? ( $handler_prefix )
1375             : ( '_cp_', '_', '' );
1376 527         17304  
1377             PKG: foreach my $pkg ($self->list_handler_pkgs) {
1378 529         989  
1379             foreach my $pre (@prefixes) {
1380 1474         6011  
1381             $coderef = $pkg->can( $pre . $handler_name );
1382              
1383 1474 100       4171 last PKG
1384             if $coderef;
1385             }
1386             }
1387             }
1388 540         1176  
1389             return $coderef;
1390             }
1391              
1392 6     6   13 sub _cp_embarrassed_developers {
1393             my( $cp, $original_cp, $handler ) = @_;
1394 6         15  
1395             $cp->filled(<<"EOF");
1396             There was an error. The developers caught the error and attempted to
1397             pass diagnosis off to a handler. Unfortunately they forgot to define
1398             the handler. Now there are two errors. You shoud complain!
1399             EOF
1400 6         140  
1401             my $prefix = $original_cp->handler_prefix;
1402 6   100     20  
1403             $prefix //= '(undef)';
1404 6 100       15  
1405             $prefix = q('')
1406             if not length $prefix;
1407 6         16  
1408             $cp->fixed(<<"EOF", 'Missing Handler - Secondary Error');
1409 6         187 handler_name: $handler
1410             handler_pkgs: @{[ join ' : ', $original_cp->list_handler_pkgs ]}
1411             handler_prefix: $prefix
1412             EOF
1413 6         25  
1414 6         9 $cp->_describe_primary_error( $original_cp );
1415             return;
1416             }
1417              
1418 8     8   17 sub _builtin_internal_error {
1419             my( $cp, @args ) = @_;
1420 8 100       29  
1421             $cp->filled( "@args" )
1422             if @args;
1423 8         21  
1424             $cp->contact_maintainer;
1425 8         11  
1426             return;
1427             }
1428              
1429 5     5   11 sub _builtin_assertion_failure {
1430             my( $cp, $description, $hashref ) = @_;
1431 5         9  
1432             my $boilerplate = <<"EOF";
1433             An assertion has failed. This indicates that the internal state of
1434             the program is corrupt.
1435             EOF
1436 5 100 100     37  
1437             $boilerplate .= $NEWLINE . $description
1438             if defined( $description )
1439             and length( $description );
1440 5         16  
1441             $cp->filled( $boilerplate );
1442 5         17  
1443             $cp->contact_maintainer;
1444 5 100 100     16  
  2         9  
1445             if (defined( $hashref ) and keys %{ $hashref }) {
1446 1         73  
1447             my $yaml = Dump( $hashref );
1448 1         8  
1449             $cp->fixed( $yaml, 'Salient State (YAML)' );
1450             }
1451 5         122  
1452             $cp->context( 'confess' );
1453 5         11  
1454             return;
1455             }
1456              
1457 31     31 1 90 sub contact_maintainer {
1458             my( $cp ) = @_;
1459 31         807  
1460             my $maintainer = $cp->maintainer;
1461 31 100       98  
1462             $cp->fixed( $maintainer, 'Please contact the maintainer' )
1463             if length $maintainer;
1464 31         58  
1465             return;
1466             }
1467              
1468             BEGIN {
1469              
1470             #-----
1471             # The real intent of this eval is to add another frame to the
1472             # callstack so that create_proxy() installs error() into the
1473             # Carp::Proxy package.
1474 43     43   44747 #-----
1475 43         364 eval {
1476             __PACKAGE__->import( 'error',
1477             {
1478             banner_title => 'Oops',
1479             context => 'internals',
1480             });
1481             };
1482              
1483 43 50       3655 ## no critic( ErrorHandling::RequireCarping )
1484             die $EVAL_ERROR if $EVAL_ERROR;
1485             }
1486              
1487              
1488             1;
1489              
1490             __END__
1491              
1492             =pod
1493              
1494             =begin stopwords
1495              
1496             accessor
1497             accessors
1498             AnnoCPAN
1499             arg
1500             arg-checking
1501             ArrayRef
1502             ArrayRefs
1503             boolean
1504             boundarys
1505             builtin
1506             BUILTIN
1507             callstack
1508             camelCasedIdentifiers
1509             CodeRef
1510             CPAN
1511             CPAN's
1512             customizable
1513             dereferences
1514             filename
1515             HashRef
1516             hashref
1517             initializations
1518             invoker
1519             invoker-upward
1520             Liebert
1521             logfile
1522             multi-line
1523             parameterized
1524             perldoc
1525             perlvar
1526             prepended
1527             Proxys
1528             regex
1529             repeatability
1530             runtime
1531             rw
1532             stackframe
1533             stackframes
1534             Stackframes
1535             stacktrace
1536             stacktraces
1537             STDERR
1538             stringification
1539             tradeoff
1540             undef
1541             whitespace
1542             YAML
1543              
1544             =end stopwords
1545              
1546             =head1 NAME
1547              
1548             Carp::Proxy - Diagnostic delegation
1549              
1550             =head1 SYNOPSIS
1551              
1552             use Carp::Proxy;
1553            
1554             fatal 'handler_subroutine', @optional_arguments
1555             if not $assertion;
1556            
1557             sub handler_subroutine {
1558             my( $proxy_object, @optional_arguments ) = @_;
1559            
1560             $proxy_object->filled( 'explanation' );
1561             return;
1562             }
1563              
1564             =head1 DESCRIPTION
1565              
1566             B<Carp::Proxy> is a framework for throwing exceptions. The goal is to
1567             couple the small lexical footprint of the B<die()> statement with
1568             support for comprehensive error messages. Good diagnostics pay for
1569             themselves; let's make them easier to produce.
1570              
1571             Error messages in Perl are commonly coded with idioms like:
1572              
1573             die 'explanation'
1574             if not $assertion;
1575              
1576             The idiom is attractive when the explanation is simple. If an
1577             explanation grows to more than a few words, or if it requires
1578             calculation, then the surrounding flow becomes disrupted. The
1579             solution, of course, is to offload failing assertions to a subroutine.
1580              
1581             Subroutines that perform diagnosis, compose error messages and throw
1582             exceptions tend to have repeated code at the beginning and end, with
1583             unique content somewhere in the middle. B<Carp::Proxy> proposes a
1584             wrapper subroutine, called a Proxy, to factor out the repeated sections.
1585              
1586             fatal 'user_subroutine'
1587             if not $assertion;
1588              
1589             Proxys, like B<fatal()>, serve as elaborate, customizable replacements
1590             for B<warn()>, B<die()> and members of the B<Carp::> family like
1591             B<confess()>. If we look at B<warn()>, B<die()>, B<confess()> and the
1592             others, we notice that they are all just different variations on two themes:
1593              
1594             - Add locational context to a user-supplied message.
1595             - Throw some kind of exception.
1596              
1597             B<Carp::Proxy> parameterizes the two themes into attributes of an
1598             exception object that is created whenever a Proxy is called. The
1599             Proxy passes the object to a user-defined "Handler" subroutine which
1600             is responsible for constructing the diagnostic message. When the
1601             Handler returns, the Proxy optionally adds "Context" (a stacktrace) to
1602             the message and performs "Disposition", typically by calling B<die()>.
1603              
1604             When the object is constructed it captures the state of Perl's error
1605             variables, for later examination by the Handler. The object provides
1606             methods that aid in message composition. Attributes control message
1607             formatting, stacktrace generation and how Disposition will be handled.
1608              
1609             The object overloads Perl's stringification operator with a message
1610             rendering method, causing uncaught exceptions to be nicely formatted.
1611             Exceptions that are caught can be modified and re-thrown.
1612              
1613             =head1 WE ARE THE 99%
1614              
1615             B<Carp::Proxy> has a long list of features, but getting started is easy.
1616             All you need for most day-to-day work is the Proxy and two methods:
1617              
1618             fatal() The (default) Proxy.
1619             filled() Auto-formatting message builder method.
1620             fixed() Pre-formatted message builder method.
1621              
1622             Use B<fatal()> wherever you currently use B<die()>. Your Handler should
1623             compose the diagnostic text using the L<filled()|/filled> and/or
1624             L<fixed()|/fixed> methods.
1625              
1626             =head1 SAMPLE OUTPUT
1627              
1628             The formatted messages produced by the Proxy start off with a
1629             "Banner". The Banner includes a title and the name of the Handler.
1630             As the Banner is the first thing seen by users, it is helpful if the
1631             Handler name conveys a terse description of the situation.
1632              
1633             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1634             Fatal: << cannot overwrite >>
1635             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1636              
1637             Following the Banner are a series of "Sections" containing paragraphs of
1638             descriptive text. Each Section is introduced by a "Header" sub-title
1639             that is wrapped in *** stars ***.
1640              
1641             *** Description ***
1642             The destination file already exists. An attempt was made
1643             to overwrite it, but the attempt failed. This might have
1644             happened because of permission problems, or possibly if
1645             the destination is not a file (i.e. a directory or link
1646             or something else).
1647            
1648             Either remove the destination manually, or choose a
1649             different destination.
1650            
1651             *** Destination Filename ***
1652             /home/isaac/muse/content
1653            
1654             *** ls(1) output ***
1655             -r--r--r-- 1 isaac users 21626 Aug 5 17:22 content
1656            
1657             *** System Diagnostic ***
1658             Permission denied
1659            
1660             *** Stacktrace ***
1661             fatal called from line 273 of /usr/bin/cantu
1662             set_output called from line 244 of /usr/bin/cantu
1663             main called from line 24 of /usr/bin/cantu
1664              
1665             Here is the corresponding Handler code. Note that most of the Handler's
1666             body is dedicated to producing message content; there is very little
1667             overhead. Handlers are easy to write.
1668              
1669             sub cannot_overwrite {
1670             my( $cp, $filename ) = @_;
1671              
1672             $cp->filled(<<"EOF");
1673             The destination file already exists. An attempt was made to
1674             overwrite it, but the attempt failed. This might have happened
1675             because of permission problems, or possibly if the destination
1676             is not a file (i.e. a directory or link or something else).
1677              
1678             Either remove the destination manually, or choose a different
1679             destination.
1680             EOF
1681             $cp->filename( $filename, 'Destination Filename' );
1682             $cp->fixed( qx{ /bin/ls -ld $filename }, 'ls(1) output' );
1683             $cp->errno_section;
1684             return;
1685             }
1686              
1687             =head1 EXPORTS
1688              
1689             B<Carp::Proxy> defines an exception class. Depending on the arguments
1690             supplied to B<use()>, B<Carp::Proxy> also generates Proxy subroutines
1691             for export. The generation part is important: B<Carp::Proxy>
1692             implements the B<import()> method as a function factory.
1693              
1694             Proxy customization is performed by supplying a HashRef of
1695             B<attribute =E<gt> parameter> pairs for each desired Proxy.
1696             The arguments to B<use()> or B<import()> look like this:
1697              
1698             proxy_name1 =>
1699             {
1700             attribute => parameter,
1701             ...
1702             },
1703              
1704             proxy_name2 =>
1705             {
1706             attribute => parameter,
1707             ...
1708             },
1709             ...
1710              
1711             If there is only one argument, i.e. a proxy-name key with no corresponding
1712             value, then an empty HashRef, C<{}>, where all attributes assume defaults,
1713             is implied.
1714              
1715             Here are some examples:
1716              
1717             #-----
1718             # All three of these 'use' statements generate and export a
1719             # Proxy named fatal() with defaults for all attributes.
1720             #-----
1721             use Carp::Proxy;
1722             use Carp::Proxy 'fatal';
1723             use Carp::Proxy fatal => {};
1724            
1725             #-----
1726             # This statement is the same as all of the above, except
1727             # that now the Proxy is named error() instead of fatal().
1728             #-----
1729             use Carp::Proxy 'error';
1730            
1731             #-----
1732             # Here we export two proxys, oops() and warning(), each with
1733             # different sets of attributes.
1734             #-----
1735             use Carp::Proxy oops => { context => 'internals' },
1736             warning => { banner_title => 'Warning',
1737             disposition => 'warn' };
1738            
1739             #----- No exports. Class definition only.
1740             use Carp::Proxy ();
1741              
1742             The no-export form is desirable if you want the class definition for your
1743             own purposes, or if Proxy attributes need to be established at runtime.
1744             You can invoke L<import()|/import>, at any time, to build Proxy subroutines.
1745              
1746             use Carp::Proxy ();
1747             ...
1748             Carp::Proxy->import( expire => { end_hook => $log_me });
1749              
1750             =head1 PROXY INVOCATION
1751              
1752             Usage:
1753             <proxy> $handler, @optional_arguments;
1754              
1755             The default L<proxy_name|/proxy_name> is C<fatal> so the typical
1756             usage looks more like
1757              
1758             fatal $handler, @optional_arguments;
1759              
1760             I<$handler> is expected to be a string (NOT a CodeRef!) that names a
1761             user-defined subroutine.
1762              
1763             The Proxy performs the following actions:
1764              
1765             =over 4
1766              
1767             =item B<1 - Capture the Environment>
1768              
1769             Perl's error/status variables $ARG, $ERRNO, $CHILD_ERROR and $EVAL_ERROR
1770             (also known as B<$_>, B<$!>, B<$?> and B<$@>, respectively) are all
1771             captured as soon as possible to preserve their values for examination by
1772             I<$handler>.
1773              
1774             =item B<2 - Create a Carp::Proxy object>
1775              
1776             The Proxy supplies settings for all the object's required attributes,
1777             see L<ATTRIBUTES|/ATTRIBUTES>. The Proxy also
1778             forwards initializations for any attributes supplied by the user (arguments
1779             to B<use()> or L<import()|/import>).
1780              
1781             =item B<3 - Call begin_hook>
1782              
1783             The L<begin_hook|/begin_hook> attribute, if it exists, is
1784             called, passing the object as the only argument.
1785              
1786             =item B<4 - Locate the Handler>
1787              
1788             Locating the Handler is a complex process; see
1789             L<HANDLER SEARCH|/HANDLER-SEARCH:>. Briefly, the default behavior is to use
1790             the first subroutine it finds from the following list of templates. The
1791             list is evaluated once for each package in the
1792             L<handler_pkgs|/handler_pkgs> attribute.
1793              
1794             <package>::_cp_<handler_name>
1795             <package>::_<handler_name>
1796             <package>::<handler_name>
1797              
1798             =item B<5 - Call Handler>
1799              
1800             The Handler is called with the object as the first argument. Any
1801             arguments passed to the Proxy, beyond the handler-name, are propagated as
1802             additional arguments to the Handler.
1803              
1804             =item B<6 - Add Calling Context (Stacktrace)>
1805              
1806             The method L<add_context()|/add_context> is invoked to generate a
1807             Section with stacktrace content, as dictated by the
1808             L<context|/context> attribute.
1809              
1810             =item B<7 - Call end_hook>
1811              
1812             The L<end_hook|/end_hook> attribute, if it exists, is called,
1813             passing the object as the only argument.
1814              
1815             =item B<8 - Perform Disposition>
1816              
1817             The method L<perform_disposition()|/perform_disposition> is
1818             invoked. Disposition is controlled by the
1819             L<disposition|/disposition> attribute; typically this means
1820             passing the B<Carp::Proxy> object to B<die()>.
1821              
1822             =back
1823              
1824             If L<perform_disposition()|/perform_disposition> returns, rather
1825             than throwing, then the returned value is propagated as the return value
1826             of the Proxy.
1827              
1828             =head1 ATTRIBUTES
1829              
1830             All B<Carp::Proxy> object attributes have correspondingly named accessors.
1831             When the accessors are invoked without arguments, they return the
1832             attribute's value. Mutable (Read-Write) attributes have accessors that can
1833             be supplied with an argument to set the attribute's value.
1834              
1835             Users generally do not create B<Carp::Proxy> objects directly; the Proxy
1836             does that for them. The object constructor, L<new()|/new>, requires
1837             specification for several of the attributes like
1838             L<eval_error|/eval_error>. The Proxy supplies these required
1839             attributes, but arguments to B<use()> or L<import()|/import> can override them.
1840              
1841             All other attributes invoke a "builder" method to initialize the
1842             attribute value if one is not provided. Builder
1843             methods are named with a prefix of B<'_build_'>. You can change default
1844             values for these attributes with arguments to B<use()> / L<import()|/import>,
1845             or by providing custom builder functions in a sub-class.
1846              
1847             =head2 arg
1848              
1849             I<arg> holds the value of Perl's B<$ARG ($_)>, as harvested from the
1850             invoking environment. This can be handy if you are using
1851             B<Try::Tiny>.
1852              
1853             =over 4
1854              
1855             =item Builder: None; L<new()|/new> requires I<arg> specification.
1856              
1857             =item Default: N/A
1858              
1859             =item Domain: Any
1860              
1861             =item Affects: For user convenience; not used by B<Carp::Proxy>
1862              
1863             =item Mutability: Read-Only
1864              
1865             =back
1866              
1867             =head2 as_yaml
1868              
1869             The I<as_yaml> attribute is a flag that controls message rendering.
1870             When False, message text is derived from the
1871             L<sections|/sections> attribute; this is the normal mode of
1872             operation.
1873              
1874             When I<as_yaml> is True message text is a B<YAML::Dump()> of the
1875             B<Carp::Proxy> object. Serialization via YAML makes it possible to
1876             propagate exceptions up from child processes. See the section on
1877             L<PROPAGATION|/PROPAGATION>.
1878              
1879             =over 4
1880              
1881             =item Builder: _build_as_yaml()
1882              
1883             =item Default: 0 (False)
1884              
1885             =item Domain: Boolean
1886              
1887             =item Affects: L<render_message()|/render_message>
1888              
1889             =item Mutability: Read-Write
1890              
1891             =back
1892              
1893             =head2 banner_title
1894              
1895             The Banner is the first part of the message; I<banner_title> contains the
1896             first word(s) in the Banner.
1897              
1898             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1899             Fatal << handler name >>
1900             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1901             -----
1902             \
1903             +------ banner_title
1904              
1905             =over 4
1906              
1907             =item Builder: _build_banner_title()
1908              
1909             =item Default: 'Fatal'
1910              
1911             =item Domain: String
1912              
1913             =item Affects: L<banner()|/banner>
1914              
1915             =item Mutability: Read-Write
1916              
1917             =back
1918              
1919             =head2 begin_hook
1920              
1921             If I<begin_hook> contains a CodeRef then the Proxy will call the CodeRef
1922             immediately after constructing the B<Carp::Proxy> object. The object
1923             is passed as the only argument.
1924              
1925             A I<begin_hook> is a great way to always do some activity at the start of
1926             every exception - before any Handler gets control.
1927              
1928             =over 4
1929              
1930             =item Builder: _build_begin_hook()
1931              
1932             =item Default: undef
1933              
1934             =item Domain: undef or a CodeRef
1935              
1936             =item Affects: L<PROXY-INVOCATION|/PROXY-INVOCATION:>
1937              
1938             =item Mutability: Read-Write
1939              
1940             =back
1941              
1942             =head2 body_indent
1943              
1944             I<body_indent> influences the presentation of paragraphs created
1945             by the Section creating methods L<filled()|/filled> and
1946             L<fixed()|/fixed>. Use I<body_indent> to determine the amount of
1947             additional indentation, beyond L<header_indent|/header_indent>,
1948             that is applied to Section paragraphs.
1949              
1950             =over 4
1951              
1952             =item Builder: _build_body_indent()
1953              
1954             =item Default: 2
1955              
1956             =item Domain: Non-negative integers
1957              
1958             =item Affects: L<filled_section()|/filled_section> and L<fixed_section()|/fixed_section>
1959              
1960             =item Mutability: Read-Write
1961              
1962             =back
1963              
1964             =head2 child_error
1965              
1966             I<child_error> holds the value of Perl's B<$CHILD_ERROR ($?)>, as harvested
1967             from the invoking environment.
1968              
1969             =over 4
1970              
1971             =item Builder: None; L<new()|/new> requires I<child_error> specification.
1972              
1973             =item Default: N/A
1974              
1975             =item Domain: Any
1976              
1977             =item Affects: L<decipher_child_error()|/decipher_child_error>
1978              
1979             =item Mutability: Read-Only
1980              
1981             =back
1982              
1983             =head2 columns
1984              
1985             The I<columns> attribute sets the line width target for the Banner and
1986             for any filled Sections. Values below about 30 are not practical.
1987              
1988             =over 4
1989              
1990             =item Builder: _build_columns()
1991              
1992             =item Default: 78
1993              
1994             =item Domain: Positive Integers
1995              
1996             =item Affects: L<banner()|/banner> and L<filled_section()|/filled_section>
1997              
1998             =item Mutability: Read-Write
1999              
2000             =back
2001              
2002             =head2 context
2003              
2004             The I<context> attribute controls the generation of a stacktrace Section.
2005              
2006             =over 4
2007              
2008             =item Builder: _build_context()
2009              
2010             =item Default: 'confess'
2011              
2012             =item Domain:
2013              
2014             =over 4
2015              
2016             =item 'none' - No Section generated.
2017              
2018             =item 'die' - Describe where Proxy was called.
2019              
2020             =item 'croak' - Describe where Proxy's caller was called.
2021              
2022             =item 'confess' - Stacktrace, starting with Proxy call.
2023              
2024             =item 'internals' - Complete stacktrace with Carp::Proxy guts.
2025              
2026             =item CodeRef - Do it yourself.
2027              
2028             =back
2029              
2030             =item Affects: L<add_context()|/add_context>
2031              
2032             =item Mutability: Read-Write
2033              
2034             =back
2035              
2036             =head2 disposition
2037              
2038             The I<disposition> attribute controls how the exception is thrown.
2039              
2040             =over 4
2041              
2042             =item Builder: _build_disposition()
2043              
2044             =item Default: 'die'
2045              
2046             =item Domain:
2047              
2048             =over 4
2049              
2050             =item 'return' - No exception thrown; Proxy returns.
2051              
2052             =item 'warn' - Carp::Proxy object passed to Perl's B<warn()>.
2053              
2054             =item 'die' - Carp::Proxy object passed to Perl's B<die()>.
2055              
2056             =item CodeRef - Do it yourself.
2057              
2058             =back
2059              
2060             =item Affects: L<perform_disposition()|/perform_disposition>
2061              
2062             =item Mutability: Read-Write
2063              
2064             =back
2065              
2066             =head2 end_hook
2067              
2068             If I<end_hook> contains a CodeRef then the Proxy will call the CodeRef
2069             just before performing disposition. The B<Carp::Proxy> object is
2070             passed as the only argument.
2071              
2072             The I<end_hook> is handy for things that you want all Handlers to do as
2073             their last action. An example might be writing to a logfile.
2074              
2075             =over 4
2076              
2077             =item Builder: _build_end_hook()
2078              
2079             =item Default: undef
2080              
2081             =item Domain: undef or a CodeRef
2082              
2083             =item Affects: L<PROXY-INVOCATION|/PROXY-INVOCATION:>
2084              
2085             =item Mutability: Read-Write
2086              
2087             =back
2088              
2089             =head2 eval_error
2090              
2091             The I<eval_error> attribute holds the value of Perl's B<$EVAL_ERROR ($@)>,
2092             as harvested from the invoking environment.
2093              
2094             =over 4
2095              
2096             =item Builder: None; L<new()|/new> requires an I<eval_error> specification
2097              
2098             =item Default: N/A
2099              
2100             =item Domain: Any
2101              
2102             =item Affects: For user convenience; not used by B<Carp::Proxy>
2103              
2104             =item Mutability: Read-Only
2105              
2106             =back
2107              
2108             =head2 exit_code
2109              
2110             I<exit_code> is used to set the value harvested by the operating system
2111             when a process dies.
2112              
2113             =over 4
2114              
2115             =item Builder: _build_exit_code()
2116              
2117             =item Default: 1
2118              
2119             =item Domain: Integers greater than Zero
2120              
2121             =item Affects: L<perform_disposition()|/perform_disposition>
2122              
2123             =item Mutability: Read-Write
2124              
2125             =back
2126              
2127             =head2 fq_proxy_name
2128              
2129             I<fq_proxy_name> is the fully-qualified proxy-name. This is the Proxy's
2130             name, complete with exported package qualifier. This might be useful if
2131             a Handler wants to know the parental Proxy.
2132              
2133             =over 4
2134              
2135             =item Builder: None; L<new()|/new> requires I<fq_proxy_name> specification
2136              
2137             =item Default: N/A
2138              
2139             =item Domain: String
2140              
2141             =item Affects: L<add_context()|/add_context>
2142              
2143             =item Mutability: Read-Write
2144              
2145             =back
2146              
2147             =head2 handler_name
2148              
2149             The Proxy saves its first argument, the Handler, in I<handler_name>.
2150              
2151             =over 4
2152              
2153             =item Builder: None; L<new()|/new> requires I<handler_name> specification
2154              
2155             =item Default: N/A
2156              
2157             =item Domain: String
2158              
2159             =item Affects: L<banner()|/banner>, L<HANDLER SEARCH|/HANDLER-SEARCH:>
2160              
2161             =item Mutability: Read-Write
2162              
2163             =back
2164              
2165             =head2 handler_pkgs
2166              
2167             The search for a Handler subroutine is performed in each of the packages
2168             in the ArrayRef I<handler_pkgs>. A copy of
2169             L<proxy_package|/proxy_package> is automatically appended, by
2170             the Proxy after object construction.
2171              
2172             =over 4
2173              
2174             =item Builder: _build_handler_pkgs()
2175              
2176             =item Default: []
2177              
2178             =item Domain: ArrayRef
2179              
2180             =item Affects: L<HANDLER SEARCH|/HANDLER-SEARCH:>
2181              
2182             =item Mutability: Read-Write
2183              
2184             =back
2185              
2186             =head2 handler_prefix
2187              
2188             I<handler_prefix> affects how the search for a Handler is performed.
2189             The list of templates that are tried during L<HANDLER SEARCH|/HANDLER-SEARCH:>
2190             is based on I<handler_prefix>.
2191              
2192             =over 4
2193              
2194             =item Builder: _build_handler_prefix()
2195              
2196             =item Default: undef
2197              
2198             =item Domain: undef or String
2199              
2200             =item Affects: L<HANDLER SEARCH|/HANDLER-SEARCH:>
2201              
2202             =item Mutability: Read-Write
2203              
2204             =back
2205              
2206             =head2 header_indent
2207              
2208             Section Headers are indented from the left margin by I<header_indent>
2209             spaces.
2210              
2211             =over 4
2212              
2213             =item Builder: _build_header_indent()
2214              
2215             =item Default: 2
2216              
2217             =item Domain: Non-negative Integers
2218              
2219             =item Affects: L<header()|/header>, L<filled_section()|/filled_section> L<fixed_section()|/fixed_section>
2220              
2221             =item Mutability: Read-Write
2222              
2223             =back
2224              
2225             =head2 maintainer
2226              
2227             The L<contact_maintainer()|/contact_maintainer> method produces a
2228             Section that urges the message recipient to contact the maintainer. The
2229             Section is created only if the I<maintainer> attribute is non-empty. A
2230             string containing an email address and a telephone number works well.
2231              
2232             =over 4
2233              
2234             =item Builder: _build_maintainer()
2235              
2236             =item Default: ''
2237              
2238             =item Domain: String
2239              
2240             =item Affects: L<contact_maintainer()|/contact_maintainer>
2241              
2242             =item Mutability: Read-Write
2243              
2244             =back
2245              
2246             =head2 numeric_errno
2247              
2248             The I<numeric_errno> attribute contains the value of
2249             Perl's B<$ERRNO ($!)>, as harvested from the invoking environment. The
2250             value is obtained by evaluating B<$ERRNO> in a numeric context.
2251              
2252             =over 4
2253              
2254             =item Builder: None; L<new()|/new> requires I<numeric_errno> specification
2255              
2256             =item Default: N/A
2257              
2258             =item Domain: Any
2259              
2260             =item Affects: For user convenience; not used by B<Carp::Proxy>
2261              
2262             =item Mutability: Read-Only
2263              
2264             =back
2265              
2266             =head2 pod_filename
2267              
2268             The L<synopsis()|/synopsis> method searches for POD in
2269             I<pod_filename>.
2270              
2271             =over 4
2272              
2273             =item Builder: _build_pod_filename()
2274              
2275             =item Default: L<proxy_filename|/proxy_filename>.
2276              
2277             =item Domain: String
2278              
2279             =item Affects: L<synopsis()|/synopsis>
2280              
2281             =item Mutability: Read-Write
2282              
2283             =back
2284              
2285             =head2 proxy_filename
2286              
2287             The filename containing the code that requested construction of the Proxy,
2288             either by B<use()> or L<import()|/import>.
2289              
2290             =over 4
2291              
2292             =item Builder: None; L<new()|/new> requires I<proxy_filename> specification
2293              
2294             =item Default: N/A
2295              
2296             =item Domain: String
2297              
2298             =item Affects: L<pod_filename|/pod_filename>.
2299              
2300             =item Mutability: Read-Only
2301              
2302             =back
2303              
2304             =head2 proxy_name
2305              
2306             I<proxy_name> contains the name of the Proxy subroutine.
2307              
2308             The default I<proxy_name> is B<'fatal'>.
2309              
2310             The only time this attribute is used is when B<use()> or L<import()|/import>
2311             are called without arguments. Defining a B<_build_proxy_name()> in
2312             a sub class allows you to change the default name.
2313              
2314             =over 4
2315              
2316             =item Builder: _build_proxy_name(); L<new()|/new> requires I<proxy_name>
2317              
2318             =item Default: 'fatal'
2319              
2320             =item Domain: String
2321              
2322             =item Affects: B<use()>, L<import()|/import>
2323              
2324             =item Mutability: Read-Only
2325              
2326             =back
2327              
2328             =head2 proxy_package
2329              
2330             The I<proxy_package> attribute is derived from the package that requested
2331             construction of the Proxy, either by calling B<use()> or L<import()|/import>.
2332              
2333             =over 4
2334              
2335             =item Builder: None; L<new()|/new> requires I<proxy_package> specification
2336              
2337             =item Default: Package of whatever subroutine called B<use()> or L<import()|/import>
2338              
2339             =item Domain: String
2340              
2341             =item Affects: L<handler_pkgs|/handler_pkgs>
2342              
2343             =item Mutability: Read-Only
2344              
2345             =back
2346              
2347             =head2 section_title
2348              
2349             The Section-creating methods L<filled()|/filled> and
2350             L<fixed()|/fixed>, accept an optional, second argument to be used
2351             as the title for the Section. When this optional argument is not
2352             supplied, I<section_title> is used instead.
2353              
2354             =over 4
2355              
2356             =item Builder: _build_section_title()
2357              
2358             =item Default: 'Description'
2359              
2360             =item Domain: Non-empty String
2361              
2362             =item Affects: L<header()|/header>, L<filled()|/filled>, L<fixed()|/fixed>
2363              
2364             =item Mutability: Read-Write
2365              
2366             =back
2367              
2368             =head2 sections
2369              
2370             The Section-creating methods L<filled()|/filled>,
2371             L<fixed()|/fixed> and L<raw()|/raw> create Section
2372             specifications. Section specifications accumulate in the ArrayRef
2373             I<sections>.
2374              
2375             =over 4
2376              
2377             =item Builder: _build_sections()
2378              
2379             =item Default: []
2380              
2381             =item Domain: ArrayRef of section-specifications
2382              
2383             =item Affects: L<render_message()|/render_message>
2384              
2385             =item Mutability: Read-Write
2386              
2387             =back
2388              
2389             =head2 string_errno
2390              
2391             I<string_errno> is a read-only attribute that contains the value of Perl's
2392             B<$ERRNO ($!)>, harvested from the invoking environment. The value is
2393             obtained by evaluating B<$ERRNO> in a string context.
2394              
2395             =over 4
2396              
2397             =item Builder: None; L<new()|/new> requires I<string_errno> specification
2398              
2399             =item Default: N/A
2400              
2401             =item Domain: String
2402              
2403             =item Affects: L<errno_section()|/errno_section>
2404              
2405             =item Mutability: Read-Only
2406              
2407             =back
2408              
2409             =head2 tags
2410              
2411             Passing arbitrary data to the catching environment can sometimes be
2412             useful. The I<tags> attribute is a HashRef for tag-value pairs of user
2413             data. The attribute is completely ignored by the Proxy and by
2414             B<Carp::Proxy> methods.
2415              
2416             =over 4
2417              
2418             =item Builder: _build_tags()
2419              
2420             =item Default: {}
2421              
2422             =item Domain: HashRef
2423              
2424             =item Affects: For user convenience; not used by B<Carp::Proxy>
2425              
2426             =item Mutability: Read-Write
2427              
2428             =back
2429              
2430             =head1 METHODS
2431              
2432             The documentation for each method starts off with a 'Usage' description.
2433             A description will look something like this:
2434              
2435             Usage:
2436             <void> $cp->append_handler_package( $pkg <, $pkg2 ...>);
2437              
2438             The word enclosed in angle-brackets, at the beginning, (Like
2439             B<E<lt>voidE<gt>>) attempts to convey the return value. Arguments in
2440             angle-brackets are optional, with the ellipsis (B<...>) implying
2441             repeatability. B<$cp> is a B<Carp::Proxy> object. B<$class>, if used as
2442             the invoker, indicates a class method.
2443              
2444             =head2 add_context
2445              
2446             Usage:
2447             <void> $cp->add_context();
2448              
2449             B<add_context()> creates a Section that contains a stacktrace of where the
2450             Proxy was invoked. The L<context|/context> attribute controls
2451             whether or not the Section is generated, as well as what kind of
2452             stacktrace is produced.
2453              
2454             B<add_context()> is called by the Proxy when the Handler returns.
2455              
2456             Perl's B<caller()> is used to probe the callstack and report stackframes.
2457             Stackframes are rendered on one line if the length would not exceed the
2458             value of the L<columns|/columns> attribute. Long lines are
2459             folded at the filename portion of the stackframe and given
2460             L<body_indent|/body_indent> extra spaces of indentation.
2461              
2462             The L<context|/context> attribute may take on any of these
2463             values:
2464              
2465             =over 4
2466              
2467             =item B<'none'>
2468              
2469             The I<context> of C<'none'> is a request to forego stacktrace generation.
2470             No Section is produced.
2471              
2472             =item B<'die'>
2473              
2474             The I<context> of C<'die'> adds a Section containing a single entry. The
2475             entry details the source location where the Proxy was invoked. The effect
2476             is intended to mimic Perl's behavior when B<die()> is passed a string
2477             WITHOUT a trailing newline.
2478              
2479             The title for the Section is C<'Exception'>.
2480              
2481             *** Exception ***
2482             fatal called from line 27 of /home/duane/bin/assim
2483              
2484             =item B<'croak'>
2485              
2486             The I<context> of C<'croak'> adds a Section that identifies the subroutine
2487             that invoked the Proxy. The effect is intended to mimic the behavior of
2488             B<Carp::croak()>, which assigns blame to the caller.
2489              
2490             The title for the Section is C<'Exception'>.
2491              
2492             *** Exception ***
2493             perform_query called from line 1172 of
2494             /opt/barkta/linux/v3.7/bin/ReadRecords
2495              
2496             =item B<'confess'>
2497              
2498             The I<context> setting of C<'confess'> creates a multi-line Section.
2499             Lines in the Section correspond to stackframes from nearest to outermost,
2500             much like the behavior of B<Carp::confess>.
2501              
2502             C<'confess'> is the default I<context> for B<Carp::Proxy> objects.
2503              
2504             The Section title is 'Stacktrace'.
2505              
2506             =item B<'internals'>
2507              
2508             The I<context> setting C<'internals'> is very similar to the setting
2509             C<'confess'>. Both produce full stacktraces, but C<'confess'> omits
2510             stackframes that originate on behalf of the Proxy. You normally do
2511             not want to see B<Carp::Proxy> stackframes, although they might be helpful
2512             in debugging a sub-class. C<'internals'> gives you everything.
2513              
2514             The Section title is 'Stacktrace'.
2515              
2516             =item B<CodeRef>
2517              
2518             By providing a CodeRef users can completely control context reporting.
2519              
2520             The Proxy will make a callback to I<CodeRef> immediately after the Handler
2521             returns. The B<Carp::Proxy> object will be passed as the only argument.
2522             The CodeRef should create a Section using the L<filled()|/filled>,
2523             L<fixed()|/fixed> or L<raw()|/raw> methods.
2524              
2525             The B<Carp> module from the Perl standard library provides some complex
2526             functionality for ignoring stackframes that you may find useful.
2527              
2528             =back
2529              
2530             =head2 append_handler_package
2531              
2532             Usage:
2533             <void> $cp->append_handler_package( $pkg <, $pkg2 ...>);
2534              
2535             The attribute L<handler_pkgs|/handler_pkgs> is an ArrayRef.
2536             B<append_handler_package()> is sugar to make adding packages to the end of
2537             L<handler_pkgs|/handler_pkgs> easier.
2538              
2539             =head2 append_section
2540              
2541             Usage:
2542             <void> $cp->append_section( $array_ref <, $array_ref2...>);
2543              
2544             The L<sections|/sections> attribute is an ArrayRef containing
2545             child ArrayRefs, one for each Section (like filled(), fixed() etc.).
2546             B<append_section()> is sugar to make adding a Section request to the
2547             L<sections|/sections> attribute, easier. Section requests are
2548             added to the end of L<sections|/sections> (appended).
2549              
2550             =head2 banner
2551              
2552             Usage:
2553             <String> $cp->banner();
2554              
2555             B<banner()> produces the multi-line introduction to a diagnostic message.
2556             The Banner is intended to stand out visually so it fills up the horizontal
2557             space from left to right margins. The value of
2558             L<columns|/columns> dictates the amount of fill needed. The
2559             Banner looks something like this:
2560              
2561             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2562             <banner_title> << <cleansed_handler> >>
2563             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2564              
2565             In the above template, I<banner_title> is taken directly from the
2566             L<banner_title|/banner_title> attribute. I<cleansed_handler> is
2567             generated by invoking
2568             L<identifier_presentation()|/identifier_presentation> on the
2569             L<handler_name|/handler_name> attribute.
2570              
2571             =head2 call
2572              
2573             Usage:
2574             <void> $cp->call( $handler, @optional_arguments );
2575              
2576             The task of Handlers is to create Sections. Handlers can call other
2577             Handlers to compose common Sections.
2578              
2579             Most Handlers know how to locate their peers because they reside in the
2580             same package and have the same prefix conventions. B<call()> can
2581             certainly be used to invoke peers, although it might seem like overkill.
2582              
2583             B<call()> is useful when Handlers reside in a hierarchy of packages and
2584             you need a full search. B<call()> is also the only way to invoke
2585             L<BUILTIN HANDLERS|/BUILTIN-HANDLERS:>.
2586              
2587             $cp->call( '*assertion_failure*', $description, \%status_vars );
2588              
2589             B<call()> runs the algorithm described in L<HANDLER SEARCH|/HANDLER-SEARCH:>.
2590              
2591             =head2 contact_maintainer
2592              
2593             Usage:
2594             <void> $cp->contact_maintainer();
2595              
2596             If the L<maintainer|/maintainer> attribute is non-empty then a
2597             Section containing the L<maintainer|/maintainer> string is
2598             created. No Section is created if L<maintainer|/maintainer> is
2599             empty.
2600              
2601             This works well if L<maintainer|/maintainer> contains contact
2602             info.
2603              
2604             *** Please contact the maintainer ***
2605             Your Name your.name@help.org (123)456-7890
2606              
2607             =head2 decipher_child_error
2608              
2609             Usage:
2610             <void> $cp->decipher_child_error();
2611             -or-
2612             <void> $cp->decipher_child_error( $child_error );
2613              
2614             Perl's B<$CHILD_ERROR> (B<$?>) encodes several bits of information about
2615             how a child process terminates, see the B<perlvar> documentation on
2616             B<$CHILD_ERROR> for details. B<decipher_child_error()> unpacks the
2617             various bits of information in B<$CHILD_ERROR> and converts them into a
2618             L<filled()|/filled> Section. Examples:
2619              
2620             *** Process Succeeded ***
2621             The child process completed normally (exit code 0).
2622              
2623             *** Process returns failing status ***
2624             The child process terminated with an exit code of 14.
2625              
2626             *** Process terminated by signal ***
2627             The child process was terminated by SIGSEGV (signal 11).
2628              
2629             If a I<$child_error> argument is provided then the argument value is
2630             deciphered, otherwise the value held in the L<child_error|/child_error>
2631             attribute is used.
2632              
2633             =head2 directory
2634              
2635             Usage:
2636             <void> $cp->directory( $dir <, $title >);
2637              
2638             The B<directory()> method creates a L<fixed()|/fixed> Section. The
2639             optional I<$title>, if supplied, forms the title for the Section,
2640             otherwise C<'Directory'> is used as the title.
2641              
2642             Output from B<Cwd::abs_path()> is used to form the body of the Section.
2643              
2644             =head2 errno_section
2645              
2646             Usage:
2647             <void> $cp->errno_section( <$title> );
2648              
2649             A filled Section is created using the contents of the
2650             L<string_errno|/string_errno> attribute. If I<$title> is
2651             not provided then 'System Diagnostic' is used as the Header title.
2652              
2653             No Section is created if the L<string_errno|/string_errno>
2654             attribute is empty.
2655              
2656             =head2 filename
2657              
2658             Usage:
2659             <void> $cp->filename( $file <, $title >);
2660              
2661             The B<filename()> method creates a L<fixed()|/fixed> Section. The
2662             optional I<$title>, if supplied, forms the title for the Section,
2663             otherwise C<'Filename'> is used as the title.
2664              
2665             Output from B<Cwd::abs_path()> is used to form the body of the Section.
2666              
2667             =head2 filled
2668              
2669             Usage:
2670             <void> $cp->filled( $content <, $title >);
2671              
2672             B<filled()> creates a Section. The Section is introduced with a
2673             L<header()|/header> containing I<$title>. The body of the Section is
2674             produced by reformatting I<$content> into paragraphs of length-limited
2675             lines.
2676              
2677             If I<$title> is not supplied, or if it is undef, then the
2678             L<section_title|/section_title> attribute is used in its place. If
2679             I<$title> is an empty string then no Header is produced. This makes it
2680             easy to chain together Fixed and Filled Sections under the same Header.
2681              
2682             Any spaces at the beginning of each paragraph in I<$content> sets the
2683             relative indentation for the whole paragraph. Each paragraph may have
2684             different indentations.
2685              
2686             Paragraphs are reformatted by splitting them into words, on whitespace, then
2687             building up new lines. Each line starts with spaces corresponding to the
2688             sum of L<header_indent|/header_indent>, L<body_indent|/body_indent> and any
2689             paragraph-specific indentation. Lines are then filled with words to achieve
2690             a target line width. The target width is given by the L<columns|/columns>
2691             attribute.
2692              
2693             In actuality, all the B<filled()> method does is to add a request for a
2694             "filled_section" onto the end of the B<sections> list. The actual
2695             processing is performed by L<filled_section()|/filled_section> when
2696             L<render_message()|/render_message> traverses the B<sections> list.
2697             What this means is that the settings for attributes like
2698             L<section_title|/section_title>, L<columns|/columns>,
2699             L<header_indent|/header_indent> and
2700             L<body_indent|/body_indent> only come into play when
2701             L<render_message()|/render_message> is run.
2702              
2703             See L<filled_section()|/filled_section> for details.
2704              
2705             =head2 filled_section
2706              
2707             Usage:
2708             <String> $cp->filled_section( $content, $title );
2709              
2710             B<filled_section()> is not usually invoked directly by users.
2711             L<render_message()|/render_message> invokes B<filled_section()> as it
2712             traverses the list held in the L<sections|/sections> attribute.
2713              
2714             I<$content> is expected to be a string. If I<$content> is an empty string
2715             then no Section is produced - an empty string is returned.
2716              
2717             I<$title> is converted into a section-title using L<header()|/header>.
2718              
2719             I<$content> is split into paragraphs wherever there are two or more
2720             consecutive newlines, more specifically using this regex:
2721              
2722             /(?: \r? \n ){2,}/x
2723              
2724             Each paragraph is examined for leading whitespace. This leading whitespace
2725             is processed by converting tabs into spaces on eight-column boundarys. The
2726             converted whitespace forms the supplemental indentation for the paragraph.
2727              
2728             New paragraphs are formed a line at a time by starting with an indentation
2729             amount corresponding to the sum of L<header_indent|/header_indent>,
2730             L<body_indent|/body_indent> and any supplemental indentation. Words from
2731             the old paragraph are added to the line so long as the line length does not
2732             exceed L<columns|/columns>. At least one word is always added, even if
2733             L<columns|/columns> is exceeded.
2734              
2735             Any trailing whitespace is removed. Output paragraphs are joined with a
2736             blank line. The returned string is the concatenation of the section title,
2737             the paragraphs and a trailing blank line.
2738              
2739             Override B<filled_section()> in a sub-class, rather than
2740             L<filled()|/filled>, if you want different filling behavior.
2741              
2742             =head2 fixed
2743              
2744             Usage:
2745             <void> $cp->fixed( $content <, $title >);
2746              
2747             B<fixed()> creates a Section. The Section is introduced with a
2748             L<header()|/header> containing I<$title>. The body of the Section is formed
2749             by retaining the formatting already present in I<$content>.
2750              
2751             If I<$title> is not supplied, or if it is undef, then the
2752             L<section_title|/section_title> attribute is used in its place. If
2753             I<$title> is an empty string then no Header is included. This makes it easy
2754             to chain together Fixed and Filled Sections under the same Header.
2755              
2756             Each line in I<$content> is indented by a constant amount corresponding to
2757             the L<header_indent|/header_indent> plus the L<body_indent|/body_indent>.
2758             Tabs in I<$content> are folded into spaces to preserve column alignment
2759             before the indentation is prepended. Trailing whitespace on each line is
2760             replaced with an appropriate line terminator for the platform. I<$content>
2761             is otherwise unmolested. Almost WYSIWYG.
2762              
2763             In actuality, all the B<fixed()> method does is to add a request for a
2764             "fixed_section" onto the end of the B<sections> list. The actual processing
2765             is performed by the L<fixed_section()|/fixed_section> method when the
2766             L<render_message()|/render_message> method traverses the B<sections> list.
2767             What this means is that the settings for attributes like
2768             L<section_title|/section_title>, L<header_indent|/header_indent> and
2769             L<body_indent|/body_indent> only matter at the time
2770             L<render_message()|/render_message> is run.
2771              
2772             See L<fixed_section()|/fixed_section> for details.
2773              
2774             =head2 fixed_section
2775              
2776             Usage:
2777             <String> $cp->fixed_section( $content, $title );
2778              
2779             B<fixed_section()> is not usually invoked directly by users.
2780             L<render_message()|/render_message> invokes B<fixed_section()> as
2781             it traverses the list in the L<sections|/sections> attribute.
2782              
2783             I<$content> is expected to be a string. If I<$content> is the empty
2784             string then no Section is generated and an empty string is returned.
2785              
2786             I<$title> is converted into a Section title string using
2787             L<header()|/header>.
2788              
2789             I<$content> is split into lines on newline ("\n") characters for
2790             processing. Trailing whitespace is removed. Embedded tabs are converted
2791             to the equivalent number of spaces assuming eight character boundarys.
2792             Indentation corresponding to the sum of
2793             L<header_indent|/header_indent> and
2794             L<body_indent|/body_indent> is added to the beginning of each
2795             line. Lines are joined with platform-appropriate line termination.
2796              
2797             Trailing whitespace is removed, the section-title is prepended and a
2798             single blank line is added to the end.
2799              
2800             =head2 header
2801              
2802             Usage:
2803             <String> $cp->header( $title );
2804              
2805             B<header()> produces an introductory line for a Section of paragraphs.
2806             The line is indented from the left margin by
2807             L<header_indent|/header_indent> spaces. The line is formed
2808             using the following template:
2809              
2810             <indent>*** <$title> ***
2811              
2812             The intent is to provide an introductory heading for Section paragraphs.
2813              
2814             *** Description ***
2815             The database server is refusing connections.
2816              
2817             If I<$title> is undef then the L<section_title|/section_title>
2818             attribute is used in its place. Passing an empty string (C<''>) for
2819             I<title> causes B<header()> to omit Header generation. In this case an
2820             empty string is returned.
2821              
2822             B<header()> is called by the Section creating methods
2823             L<filled_section()|/filled_section> and
2824             L<fixed_section()|/fixed_section>.
2825              
2826             Subclass B<Carp::Proxy> and override B<header()> for a different look.
2827              
2828             =head2 identifier_presentation
2829              
2830             Usage:
2831             <String> $class->identifier_presentation( $name );
2832              
2833             The Banner reads better when words in the
2834             L<handler_name|/handler_name> are separated by spaces rather
2835             than underscores (C<_>). Likewise with camelCasedIdentifiers.
2836              
2837             Underscores are replaced by single spaces everywhere they occur. Spaces
2838             are inserted everywhere character-case changes from lower to upper, and
2839             upper-case characters are folded to lower-case. The following are example
2840             conversions:
2841              
2842             'no_user_credentials' => 'no user credentials'
2843             'nonexistentRecord' => 'nonexistent record'
2844              
2845             Sub-class B<Carp::Proxy> and override B<identifier_presentation()> if
2846             you want a different convention.
2847              
2848             =head2 import
2849              
2850             Usage:
2851             <void> $class->import( <%attrs_by_proxy>);
2852              
2853             B<import()> accepts specifications for Proxy construction. Specifications
2854             take the form of a proxyname and a hashref of attribute initializations.
2855              
2856             proxyname1 => {
2857             attributeA => initial_valueA,
2858             attributeB => initial_valueB,
2859             ...
2860             }
2861              
2862             Any number of proxyname, hashref pairs may be specified; a proxy subroutine
2863             will be constructed for each pair.
2864              
2865             If there is only one argument it is taken to be a proxyname introducing an
2866             empty hashref. If there are no arguments then it is assumed that the
2867             builder-specified default for the L<proxy_name|/proxy_name> attribute
2868             (C<'fatal'>), should be used for the proxyname and an empty hashref used for
2869             the attribute initializations.
2870              
2871             B<import()> probes the callstack to determine the package and filename of
2872             the user code that called B<import()>. B<import()> uses these values to create
2873             a hash containing the attributes L<proxy_filename|/proxy_filename>,
2874             L<proxy_name|/proxy_name> L<proxy_package|/proxy_package> and
2875             L<fq_proxy_name|/fq_proxy_name>. Any supplied attributes are added to the
2876             hash. The builtin handler L<*configuration*|/configuration> returns a
2877             reference to this hash.
2878              
2879             =head2 list_handler_packages
2880              
2881             Usage:
2882             <list> $cp->list_handler_packages();
2883              
2884             B<list_handler_packages()> is sugar that dereferences the
2885             L<handler_pkgs|/handler_pkgs> attribute (an ArrayRef) and
2886             returns the contents.
2887              
2888             =head2 list_sections
2889              
2890             Usage:
2891             <list> $cp->list_sections();
2892              
2893             The L<sections|/sections> attribute is an ArrayRef.
2894             B<list_sections()> is sugar to return all the elements of
2895             L<sections|/sections>.
2896              
2897             =head2 new
2898              
2899             Usage:
2900             <Carp::Proxy object> $class->new
2901             ( arg => harvested $_,
2902             eval_error => harvested $@,
2903             fq_proxy_name => 'package::subname',
2904             handler_name => 'name of handler',
2905             numeric_errno => harvested 0 + $!,
2906             proxy_filename => 'filename',
2907             proxy_name => 'subname',
2908             proxy_package => 'package',
2909             string_errno => harvested '' . $!,
2910             < attribute => value ...>
2911             );
2912              
2913             I<new()> is normally called by the Proxy, so this documentation is only
2914             useful if you are using the object for your own purposes. There are a large
2915             number of required attribute-value pairs. Specification for any additional
2916             attributes is supported. Builder methods are invoked for all unspecified
2917             attributes.
2918              
2919             There is some inconsistency around the L<proxy_name|/proxy_name> attribute.
2920             The L<proxy_name|/proxy_name> is required by I<new()> even though it has a
2921             builder method. The builder is for use by L<import()|/import>, which
2922             invokes it if needed, and passes the result to new().
2923              
2924             =head2 perform_disposition
2925              
2926             Usage:
2927             <Scalar> $cp->perform_disposition();
2928              
2929             The L<disposition|/disposition> attribute determines the final
2930             actions of the Proxy, which are carried out by B<perform_disposition()>.
2931             Valid settings for L<disposition|/disposition> are:
2932              
2933             =over 4
2934              
2935             =item B<'warn'>
2936              
2937             A I<disposition> of C<'warn'> causes B<perform_disposition()> to do this:
2938              
2939             warn $cp;
2940             return ();
2941              
2942             =item B<'die'>
2943              
2944             A I<disposition> of C<'die'> causes B<perform_disposition()> to do this:
2945              
2946             $ERRNO = $cp->exit_code;
2947             die $cp;
2948              
2949             See Perl's B<die()> for an explanation of propagating $ERRNO into the exit
2950             code for the process.
2951              
2952             C<'die'> is the default I<disposition>.
2953              
2954             =item B<'return'>
2955              
2956             The I<disposition> of C<'return'> is unusual; it signifies a desire to
2957             abort the whole death-by-proxy process. B<perform_disposition> does this:
2958              
2959             return $cp;
2960              
2961             =item B<CodeRef>
2962              
2963             The user can take control of disposition by supplying a CodeRef for
2964             I<disposition>. In this case, the behavior of B<perform_disposition()>
2965             is:
2966              
2967             return $cp->disposition->( $cp );
2968              
2969             =back
2970              
2971             =head2 prepend_handler_package
2972              
2973             Usage:
2974             <void> $cp->prepend_handler_package( $pkg <, $pkg2...>);
2975              
2976             The attribute L<handler_pkgs|/handler_pkgs> is an ArrayRef.
2977             B<prepend_handler_package()> is sugar to make adding packages to the front
2978             of L<handler_pkgs|/handler_pkgs> easier.
2979              
2980             =head2 prepend_section
2981              
2982             Usage:
2983             <void> $cp->prepend_section( $array_ref <, $array_ref2...>);
2984              
2985             The L<sections|/sections> attribute is an ArrayRef containing
2986             child ArrayRefs, one for each Section (like filled(), fixed() etc.).
2987             B<prepend_section()> is sugar to make adding a Section request to the
2988             L<sections|/sections> attribute, easier. Section requests are
2989             added to the front of L<sections|/sections> (prepended).
2990              
2991             =head2 raw
2992              
2993             Usage:
2994             <void> $cp->raw( $content );
2995              
2996             B<raw()> provides an alternative to L<fixed()|/fixed> and
2997             L<filled()|/filled> for composing diagnostic Sections.
2998              
2999             In effect, B<raw()> creates a Section containing only B<$content>.
3000             You are completely responsible for the final appearance of the Section;
3001             there is no Header, no trailing blank line, no indentation and no
3002             platform appropriate line termination.
3003              
3004             In actuality, all the B<raw()> method does is to add a request for a raw
3005             Section onto the B<sections> list; the actual processing is performed by
3006             the L<raw_section()|/raw_section> method when the
3007             L<render_message()|/render_message> traverses B<sections>.
3008              
3009             See L<raw_section()|/raw_section> for details.
3010              
3011             =head2 raw_section
3012              
3013             Usage:
3014             <String> $cp->raw_section( $content );
3015              
3016             B<raw_section()> is not usually invoked directly by users.
3017             L<render_message()|/render_message> invokes B<raw_section()> as it
3018             traverses the list in the L<sections|/sections> attribute.
3019              
3020             B<raw_section()> does nothing; the returned string is simply a copy of
3021             I<$content>.
3022              
3023             =head2 render_message
3024              
3025             Usage:
3026             <String> $cp->render_message();
3027              
3028             The behavior of B<render_message()> is dependent on the setting of the
3029             attribute L<as_yaml|/as_yaml>. If L<as_yaml|/as_yaml> is False, which is
3030             the default, then B<render_message()> walks the list of
3031             section-specifications stored in the L<sections|/sections> attribute,
3032             executing each one in turn. The return value is formed by concatenating
3033             each of the results.
3034              
3035             The L<sections|/sections> attribute, an ArrayRef, is expected to contain any
3036             number of ArrayRef elements. Each child ArrayRef must have at least one
3037             element: the name of a method to be invoked. Any remaining elements are
3038             passed to the invoked method as arguments. For example, a
3039             L<sections|/sections> specification that looks like this:
3040              
3041             [
3042             [ 'filled_section', 'content1', 'title1' ],
3043             [ 'filled_section', 'content2', 'title2' ],
3044             ]
3045              
3046             Results in the execution of something like this:
3047              
3048             my $buffer = $cp->banner();
3049              
3050             $buffer .= $cp->filled_section( 'content1', 'title1' );
3051             $buffer .= $cp->filled_section( 'content2', 'title2' );
3052              
3053             return $buffer;
3054              
3055             The L<sections|/sections> list is unchanged by the traversal, so
3056             B<render_message()> may be invoked repeatedly. Settings for attributes like
3057             L<banner_title|/banner_title>, L<columns|/columns>,
3058             L<section_title|/section_title>, L<header_indent|/header_indent> and
3059             L<body_indent|/body_indent> can be changed between invocations to vary the
3060             message format.
3061              
3062             Changing attributes like L<context|/context>, which are
3063             referenced during the generation of Section specifications, have no effect.
3064              
3065             If L<as_yaml|/as_yaml> is True then we return a string that
3066             is a YAML dump of the B<Carp::Proxy> object, something like this:
3067              
3068             return YAML::XS::Dump( $cp );
3069              
3070             The intent here is to use YAML to serialize all aspects of the
3071             B<Carp::Proxy> object. Assuming that we have a
3072             L<disposition|/disposition> setting of B<die>, our
3073             serialized object will be written out to STDERR, where it can be captured
3074             by a parent process and reconstituted. The reconstituted object can
3075             be examined, or augmented with parental context and rethrown.
3076              
3077             =head2 synopsis
3078              
3079             Usage:
3080             <void> $cp->synopsis( %optional_supplements );
3081              
3082             The B<synopsis()> method employs B<Pod::Usage::pod2usage()> to create a
3083             Section from the user's POD. User POD is located by searching in the
3084             L<pod_filename|/pod_filename> attribute.
3085              
3086             The call to B<pod2usage()> is passed a HashRef with the following options:
3087              
3088             -input => $cp->pod_filename,
3089             -output => <filehandle>,
3090             -exitval => 'NOEXIT',
3091             -verbose => 0,
3092              
3093             This set of options causes B<pod2usage()> to format the B<SYNOPSIS>
3094             portion of the user's POD. Any key-value pairs in
3095             B<%optional_supplements> are appended to the contents of the HashRef,
3096             allowing you to override or supplement these defaults.
3097              
3098             =head3 Example
3099              
3100             Internally, B<Carp::Proxy> uses B<synopsis()> to extract sections from this
3101             POD document when composing diagnostics. If, for instance, you supply a
3102             negative value as the setting for L<body_indent|/body_indent> you get an
3103             exception. The text of the exception is generated using something like
3104             this:
3105              
3106             $cp->synopsis( -verbose => 99,
3107             -sections => ["ATTRIBUTES/body_indent"],
3108             );
3109              
3110             The resulting diagnostic looks something like this:
3111              
3112             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3113             Oops << negative body indentation >>
3114             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3115             *** Description ***
3116             The requested setting of '-1' for the 'body_indent'
3117             attribute is not allowed.
3118            
3119             *** Synopsis ***
3120             body_indent:
3121             *body_indent* influences the presentation of paragraphs
3122             created by the Section creating methods filled() and
3123             fixed(). Use *body_indent* to determine the amount of
3124             additional indentation, beyond header_indent, that is
3125             applied to Section paragraphs.
3126            
3127             Builder: _build_body_indent()
3128             Default: 2
3129             Domain: Non-negative integers
3130             Affects: filled_section() and fixed_section()
3131             Mutability: Read-Write
3132            
3133             *** Stacktrace ***
3134             ...
3135              
3136             See L<'perldoc Pod::Usage'|Pod::Usage> and
3137             L<'perldoc Pod::Select'|Pod::Select> for details about using B<-verbose>
3138             and B<-sections>.
3139              
3140             =head2 usage
3141              
3142             Usage:
3143             <void> $cp->usage();
3144              
3145             B<usage()> examines the callstack, to find the invoker - the subroutine
3146             that invoked the Proxy. A pass through the
3147             L<HANDLER SEARCH|/HANDLER-SEARCH:> algorithm is made to see if it can find
3148             a subroutine with this name:
3149              
3150             usage_<invoker>
3151              
3152             In the default configuration this means that these three subroutine
3153             names are tested for existence:
3154              
3155             <package>::_cp_usage_<invoker>
3156             <package>::_usage_<invoker>
3157             <package>::usage_<invoker>
3158              
3159             Just like the search for a Handler, the settings for
3160             L<handler_prefix|/handler_prefix> and
3161             L<handler_pkgs|/handler_pkgs> influence the where and what of
3162             the search for a usage subroutine.
3163              
3164             If none of the attempts finds an existing subroutine then the next entry in
3165             the callstack (i.e. the invoker of the invoker) is tried. The progression
3166             up the callstack continues until there are no more stackframes. At this
3167             point the algorithm gives up and throws a "no usage documentation" exception.
3168              
3169             The search sounds complex, but the intent is simple: public subroutines
3170             and methods can call utilities, say to validate incoming arguments, and
3171             these utilities can call Proxys to throw exceptions. When the Handler
3172             invokes B<usage()> we eventually find a usage message associated with the
3173             public subroutine.
3174              
3175             #----- We want this to be called for help with 'my_func()'
3176             sub _usage_my_func {
3177             my( $cp ) = @_;
3178              
3179             $cp->fixed( 'Usage: <num> my_func( val );', 'Usage' );
3180             $cp->filled( 'my_func() returns blah blah blah.', '' );
3181             }
3182              
3183             sub my_func {
3184             my( $val ) = @_;
3185              
3186             fatal 'missing_argument', 'val'
3187             if not defined $val;
3188             ...
3189             }
3190              
3191             #----- Reusable by anyone that defines their own _usage_
3192             sub _cp_missing_argument {
3193             my( $cp, $name ) = @_;
3194              
3195             $cp->filled("The argument '$name' is missing, or undef.");
3196             $cp->usage;
3197             }
3198              
3199             Other subroutines, besides my_func(), can throw fatal exceptions with the
3200             'missing_argument' Handler. The diagnostic will be customized
3201             appropriately for each one.
3202              
3203             The invoker-upward aspect of the search means that B<my_func()>, instead
3204             of calling B<fatal()> directly, could have called an arg-checking utility,
3205             which called another utility etc., which finally called B<fatal()>. The
3206             search would have eventually located B<_usage_my_func()>.
3207              
3208             =head1 HANDLER SEARCH
3209              
3210             A Proxy invocation contains, as its first argument, a string that will
3211             become the L<handler_name|/handler_name> attribute. The string
3212             C<'no_such_author'> is used to establish
3213             L<handler_name|/handler_name> in this example:
3214              
3215             fatal 'no_such_author', $who;
3216              
3217             The Proxy calls the Handler to build up the diagnostic message, but first
3218             it must locate the requested subroutine.
3219              
3220             The search for a Handler subroutine is made in the packages found in
3221             L<handler_pkgs|/handler_pkgs>. Users can specify a list of
3222             packages to search by supplying the tagged list to B<use()> or
3223             L<import()|/import>.
3224              
3225             package main;
3226             use Carp::Proxy fatal => { handler_pkgs => [qw( Support Common )]};
3227              
3228             You can also sub-class B<Carp::Proxy> and override
3229             B<_build_handler_pkgs()> to return an ArrayRef of the desired packages.
3230              
3231             The Proxy always appends a copy of
3232             L<proxy_package|/proxy_package> to
3233             L<handler_pkgs|/handler_pkgs> after object construction.
3234             L<proxy_package|/proxy_package> is the package that issued the
3235             B<use()>, or made the call to L<import()|/import>. In the above example
3236             L<handler_pkgs|/handler_pkgs> becomes:
3237              
3238             [qw( Support Common main )]
3239              
3240             The subroutine that is the target of the search is influenced by the
3241             setting of L<handler_prefix|/handler_prefix>. When the
3242             L<handler_prefix|/handler_prefix> attribute is undef, the Proxy
3243             builds three templates from L<handler_name|/handler_name>. The
3244             first subroutine that exists is used as the Handler.
3245              
3246             <package>::_cp_<handler_name>
3247             <package>::_<handler_name>
3248             <package>::<handler_name>
3249              
3250             If L<handler_prefix|/handler_prefix> is not undef then only one
3251             template is tried:
3252              
3253             <package>::<handler_prefix><handler_name>
3254              
3255             If a Handler subroutine is not found by the template search then a check
3256             is made to see if L<handler_name|/handler_name> matches one of
3257             the B<Carp::Proxy> builtin Handlers. The builtin Handlers are surrounded
3258             by C<'*'> characters since those are guaranteed not to collide with user
3259             Handlers.
3260              
3261             *assertion_failure*
3262             *internal_error*
3263             *configuration*
3264              
3265             See L<BUILTIN HANDLERS|/BUILTIN-HANDLERS:> for a description of their
3266             functionality.
3267              
3268             Finally, if a suitable Handler is not found by any of the above searches
3269             the Proxy concludes that you forgot to define a Handler. In response, the
3270             Proxy attempts to shame you into compliance by throwing an exception of
3271             its own:
3272              
3273             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3274             Oops << embarrassed developers >>
3275             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3276             *** Description ***
3277             There was an error. The developers caught the error and
3278             attempted to pass diagnosis off to a handler. Unfortunately
3279             they forgot to define the handler. Now there are two
3280             errors. You should complain!
3281              
3282             *** Please contact the maintainer ***
3283             your.name@support.org 555-1212
3284              
3285             *** Missing Handler ***
3286             handler_name: no_credentials
3287             handler_pkgs: main
3288             handler_prefix: (undef)
3289              
3290             *** Stacktrace ***
3291             fatal called from line 443 of /usr/local/bin/hibs
3292             validate_user called from line 510 of /usr/local/bin/hibs
3293             cmdline called from line 216 of /usr/local/bin/hibs
3294             main called from line 17 of /usr/local/bin/hibs
3295              
3296             =head1 BUILTIN HANDLERS
3297              
3298             These are handler subroutines that come with B<Carp::Proxy>.
3299              
3300             =head2 internal_error
3301              
3302             Usage:
3303             <void> fatal '*internal_error*', @strings;
3304              
3305             The C<'*internal_error*'> Handler can be used to promote warnings to
3306             errors or to turn miscellaneous B<die()> exceptions to full B<Carp::Proxy>
3307             exceptions. The typical use is to trap B<$SIG{__DIE__}> or
3308             B<$SIG{__WARN__}>.
3309              
3310             use English;
3311            
3312             $SIG{__DIE__} = sub{
3313            
3314             fatal '*internal_error*', @_
3315             if not $EXCEPTIONS_BEING_CAUGHT;
3316             };
3317              
3318             A Filled Section is generated from the string interpolation of
3319             I<@strings>. In the above example, the argument is the message that was
3320             passed to B<die()>, like "Illegal division by zero". A
3321             L<contact_maintainer()|/contact_maintainer> Section is also added.
3322              
3323             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3324             Fatal: << internal error >>
3325             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3326             *** Description ***
3327             Illegal division by zero at ./combine_decks line 27.
3328              
3329             *** Please contact the maintainer ***
3330             your.name@support.org 555-1212
3331              
3332             *** Stacktrace ***
3333             ...
3334              
3335             =head2 assertion_failure
3336              
3337             Usage:
3338             <void> fatal '*assertion_failure*', $description <, $hashref>;
3339              
3340             If a failing assertion is indicative of a programmer fault then the
3341             primary audience for a diagnostic message will be a maintainer rather than
3342             an end user. Maintainers are most often helped by knowledge of the
3343             surrounding state. The builtin Handler B<*assertion_failure*> attempts to
3344             be a generic Handler, useful for transmitting state to maintainers.
3345              
3346             Using B<*assertion_failure*> frees the programmer from having to write a
3347             Handler. The tradeoff is that some ability to customize the diagnostic is
3348             lost and the invocation syntax is more cluttered. The tradeoff can be
3349             reasonable for events that are rarely triggered, especially if it
3350             encourages programmers to add more assertions.
3351              
3352             B<'*assertion_failure*'> produces a Filled Section with some boilerplate
3353             containing the supplied I<$description>.
3354              
3355             Also included is a Fixed Section which contains a YAML dump of
3356             I<$hashref>. This works best when the HashRef keys act as informational
3357             names (tag=>value pairs) to convey state. YAML is nice here because it
3358             does a great job of serializing complex data structures.
3359              
3360             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3361             Fatal: << assertion failure >>
3362             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3363             *** Description ***
3364             An assertion has failed. This indicates that the internal
3365             state of the program is corrupt.
3366              
3367             <$description>
3368              
3369             *** Please contact the maintainer ***
3370             your.name@support.org 555-1212
3371              
3372             *** Salient State (YAML) ***
3373             ---
3374             failure: 'unmatched case'
3375             index: 27
3376             selection: 'brunch'
3377              
3378             *** Stacktrace ***
3379             ...
3380              
3381             =head2 configuration
3382              
3383             Usage:
3384             <HashRef> fatal '*configuration*';
3385              
3386             The C<'*configuration*'> Handler is unconventional in that no exception is
3387             thrown. Instead, a reference to an internal hash is returned to the
3388             calling environment. Any changes to the referenced hash affect all future
3389             Proxy invocations.
3390              
3391             Proxy configuration is established when a Proxy is created - either during
3392             B<use()> or L<import()|/import>. Configuration consists of attribute
3393             =E<gt> parameter pairs that are supplied by the user.
3394              
3395             use Carp::Proxy ( warning => { banner_title => 'Warning',
3396             disposition => 'warn' });
3397              
3398             In the above snippet, L<banner_title|/banner_title> and
3399             L<disposition|/disposition>, are internally held in a
3400             closure-based hash that persists across all invocations of the Proxy. The
3401             B<*configuration*> Handler causes the Proxy to return a reference to this
3402             internal hash.
3403              
3404             Here is an example of wanting to change Proxy behavior after Proxy
3405             creation:
3406              
3407             #----- fatal() does NOT throw an exception here...
3408             my $config = fatal '*configuration*';
3409            
3410             $config->{ disposition } = \&GUI::as_dialog;
3411              
3412             As alluded to above, we want our GUI program to use conventional STDERR
3413             based messages during initialization, but once the GUI is up we want
3414             future messages to go to a dialog widget.
3415              
3416             =head1 PROPAGATION
3417              
3418             The I<as_yaml> attribute controls stringification of the Proxy object.
3419             In its normal state of false (0), I<as_yaml> produces the formatted
3420             error message. When true (1), I<as_yaml> instead produces a YAML Dump()
3421             of the proxy object.
3422              
3423             Newer versions of YAML do not bless reconstituted objects as a security
3424             precaution, so if you want to propagate errors up from child processes
3425             you will need to specifically allow it.
3426              
3427             # 'cmd' here throws a fatal() with as_yaml set to 1
3428             $output = qx{ cmd 2>&1 1>/dev/null };
3429            
3430             if ( $CHILD_ERROR ) {
3431            
3432             my $kids_proxy;
3433             {
3434             local $YAML::XS::LoadBlessed = 1;
3435             $kids_proxy = YAML::XS::Load( $output );
3436             }
3437             do_something( $kids_proxy )
3438             }
3439              
3440              
3441             =head1 BUGS AND LIMITATIONS
3442              
3443             Please report any bugs or feature requests to C<bug-carp-proxy at
3444             rt.cpan.org>, or through the web interface at
3445             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Carp-Proxy>. I will be
3446             notified, and then you'll automatically be notified of progress on your
3447             bug as I make changes.
3448              
3449             =head1 DEPENDENCIES
3450              
3451             Core dependencies (come with Perl)
3452              
3453             Config
3454             Cwd
3455             English
3456             overload
3457             Pod::Usage
3458              
3459             External dependencies (install from CPAN)
3460              
3461             Moose
3462             Readonly
3463             Sub::Name
3464             YAML::XS
3465              
3466             =head1 SUPPORT
3467              
3468             You can find documentation for this module with the perldoc command.
3469              
3470             perldoc Carp::Proxy
3471              
3472              
3473             You can also look for information at:
3474              
3475             =over 4
3476              
3477             =item * RT: CPAN's request tracker (report bugs here)
3478              
3479             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Carp-Proxy>
3480              
3481             =item * AnnoCPAN: Annotated CPAN documentation
3482              
3483             L<http://annocpan.org/dist/Carp-Proxy>
3484              
3485             =item * CPAN Ratings
3486              
3487             L<http://cpanratings.perl.org/d/Carp-Proxy>
3488              
3489             =item * Search CPAN
3490              
3491             L<http://search.cpan.org/dist/Carp-Proxy/>
3492              
3493             =back
3494              
3495              
3496             =head1 SEE ALSO
3497              
3498             =over 4
3499              
3500             =item perldoc L<perlvar>
3501              
3502             The section on $CHILD_ERROR describes information packing when a child
3503             process terminates. This is used by
3504             L<decipher_child_error()|/decipher_child_error>.
3505              
3506             =item perldoc -f die
3507              
3508             The documentation on Perl's B<die()> details how the exit code for a process
3509             depends on B<$ERRNO> and B<$CHILD_ERROR>.
3510              
3511             =item perldoc L<Pod::Usage>
3512              
3513             The L<synopsis()|/synopsis> method calls B<pod2usage()> to format
3514             the B<SYNOPSIS> section from user POD.
3515              
3516             =item perldoc L<YAML::XS>
3517              
3518             The L<as_yaml|/as_yaml> attribute produces a YAML Dump of the
3519             B<Carp::Proxy> object so that it can be reconstituted later.
3520              
3521             The L<*assertion_failure*|/assertion_failure> builtin
3522             Handler produces a Section containing YAML Dump of a user HashRef.
3523              
3524             =item perldoc L<Carp>
3525              
3526             The 'croak' and 'confess' concepts were originated by B<Carp>. If you are
3527             making a Do-it-yourself CodeRef for L<context|/context> then
3528             B<Carp>'s B<longmess()> or B<longmess_heavy()> may prove useful.
3529              
3530             =back
3531              
3532             =head1 LICENSE AND COPYRIGHT
3533              
3534             Copyright 2014-2020 Paul Liebert.
3535              
3536             This program is free software; you can redistribute it and/or modify it
3537             under the terms of either: the GNU General Public License as published
3538             by the Free Software Foundation; or the Artistic License.
3539              
3540             See http://dev.perl.org/licenses/ for more information.
3541              
3542             =cut