File Coverage

blib/lib/Carp/Always.pm
Criterion Covered Total %
statement 22 32 68.7
branch 1 6 16.6
condition n/a
subroutine 7 11 63.6
pod n/a
total 30 49 61.2


line stmt bran cond sub pod time code
1              
2             package Carp::Always;
3              
4 1     1   55859 use 5.006;
  1         2  
5 1     1   4 use strict;
  1         1  
  1         16  
6 1     1   4 use warnings;
  1         1  
  1         64  
7              
8             our $VERSION = '0.16';
9             $VERSION =~ tr/_//d;
10              
11             BEGIN {
12 1     1   12 require Carp;
13 1         30 $Carp::CarpInternal{ +__PACKAGE__ }++;
14             }
15              
16 1     1   5 use constant CHOMP_DOT => $Carp::VERSION < 1.25;
  1         1  
  1         348  
17              
18 0     0   0 sub _warn { warn &_longmess }
19              
20 0 0   0   0 sub _die { die ref $_[0] ? @_ : &_longmess }
21              
22             sub _longmess {
23 0     0   0 if (CHOMP_DOT && $_[-1] =~ /\.\n\z/) {
24             my $arg = pop @_;
25             $arg =~ s/\.\n\z/\n/;
26             push @_, $arg;
27             }
28 0         0 my $mess = &Carp::longmess;
29 0         0 $mess =~ s/( at .*?\n)\1/$1/s; # Suppress duplicate tracebacks
30 0         0 $mess;
31             }
32              
33             my @HOOKS = qw(__DIE__ __WARN__);
34             my %OLD_SIG;
35              
36             sub import {
37 1     1   8 my $class = shift;
38 1 50       4 return if $OLD_SIG{$class};
39 1         3 @{ $OLD_SIG{$class} }{ @HOOKS, 'Verbose' } = (@SIG{@HOOKS}, $Carp::Verbose);
  1         5  
40              
41 1         12 @SIG{@HOOKS} = ($class->can('_die'), $class->can('_warn'));
42 1         15 $Carp::Verbose = 'verbose'; # makes carp() cluck and croak() confess
43             }
44              
45             sub unimport {
46 0     0     my $class = shift;
47 0 0         return unless $OLD_SIG{$class};
48 1     1   573 no if "$]" <= 5.008008, 'warnings' => 'uninitialized';
  1         11  
  1         8  
49 0           (@SIG{@HOOKS}, $Carp::Verbose) = @{ delete $OLD_SIG{$class} }{ @HOOKS, 'Verbose' };
  0            
50             }
51              
52             1;
53              
54             =encoding utf8
55              
56             =head1 NAME
57              
58             Carp::Always - Warns and dies noisily with stack backtraces
59              
60             =head1 SYNOPSIS
61              
62             use Carp::Always;
63              
64             Often used on the command line:
65              
66             perl -MCarp::Always script.pl
67              
68             =head1 DESCRIPTION
69              
70             This module is meant as a debugging aid. It can be
71             used to make a script complain loudly with stack backtraces
72             when warn()ing or die()ing.
73              
74             Here are how stack backtraces produced by this module
75             looks:
76              
77             # it works for explicit die's and warn's
78             $ perl -MCarp::Always -e 'sub f { die "arghh" }; sub g { f }; g'
79             arghh at -e line 1
80             main::f() called at -e line 1
81             main::g() called at -e line 1
82              
83             # it works for interpreter-thrown failures
84             $ perl -MCarp::Always -w -e 'sub f { $a = shift; @a = @$a };' \
85             -e 'sub g { f(undef) }; g'
86             Use of uninitialized value in array dereference at -e line 1
87             main::f('undef') called at -e line 2
88             main::g() called at -e line 2
89              
90             In the implementation, the L module does
91             the heavy work, through C. The
92             actual implementation sets the signal hooks
93             L<$SIG{__WARN__}|perlvar/%SIG> and L<$SIG{__DIE__}|perlvar/%SIG> to
94             emit the stack backtraces.
95              
96             Also, all uses of C and C are made verbose,
97             behaving like C and C.
98              
99             =head1 METHODS
100              
101             L implements the following methods.
102              
103             =head2 import
104              
105             Carp::Always->import()
106              
107             Enables L. Also triggered by statements like
108              
109             use Carp::Always;
110             use Carp::Always 0.14;
111              
112             but not by
113              
114             use Carp::Always (); # does not invoke import()
115              
116             =head2 unimport
117              
118             Carp::Always->unimport();
119              
120             Disables L. Also triggered with
121              
122             no Carp::Always;
123              
124             =head1 ACKNOWLEDGMENTS
125              
126             This module was born as a reaction to a release
127             of L by Sébastien Aperghis-Tramoni.
128             Sébastien also has a newer module called
129             L with the same code and fewer flame
130             comments on docs. The pruning of the uselessly long
131             docs of this module was prodded by Michael Schwern.
132              
133             Schwern and others told me "the module name stinked" -
134             it was called C. After thinking long
135             and getting nowhere, I went with nuffin's suggestion
136             and now it is called C.
137              
138             =head1 SEE ALSO
139              
140             L
141              
142             L and L
143              
144             L
145              
146             L
147              
148             L
149              
150             L and L
151              
152             =head1 BUGS
153              
154             =over 4
155              
156             =item *
157              
158             This module does not play well with other modules which fusses
159             around with C, C, C<$SIG{__WARN__}>, C<$SIG{__DIE__}>.
160              
161             =item *
162              
163             Test scripts are good. I should write more of these.
164              
165             =back
166              
167             Please report bugs via GitHub
168             L
169              
170             Backlog in CPAN RT: L
171              
172             =head1 AUTHOR
173              
174             Adriano Ferreira, Eferreira@cpan.orgE
175              
176             =head1 COPYRIGHT AND LICENSE
177              
178             Copyright (C) 2005-2013, 2018 by Adriano Ferreira
179              
180             This library is free software; you can redistribute it and/or modify
181             it under the same terms as Perl itself.
182              
183             =cut