blib/lib/Carp/Always.pm

filter: Statementstmt 52.9% 27 / 51 Branchbran 10.0% 1 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 7 / 14 Podpod n/a totaltotal 46.6% 35 / 75 CC 6 SCAR 9.9
CC
6
Cov
46%
_die
6.0
unimport
4.0
unimport
4.0
CRAP 2.7
↑ Summary
1  
2 package Carp::Always;
3  
4 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.16';
9 1 $VERSION =~ tr/_//d;
10  
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 require Carp;
13 1 $Carp::CarpInternal{ +__PACKAGE__ }++;
14 }
15  
16 1 ▶use constant CHOMP_DOT => $Carp::VERSION < 1.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 0 ▶sub _warn { warn &_longmess }
Subroutine _warn: not called CC=1 SCAR=6.9
19  
20 0 ▶sub _die { die ref $_[0] ? @_ : &_longmess }
Subroutine _die: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
21  
22 0 ▶sub _longmess {
Subroutine _longmess: not called CC=1 SCAR=6.9
23 0 ▶ if (CHOMP_DOT && $_[-1] =~ /\.\n\z/) {
Subroutine _longmess: not called CC=1 SCAR=6.9
24 my $arg = pop @_;
25 $arg =~ s/\.\n\z/\n/;
26 push @_, $arg;
27 }
28 0 my $mess = &Carp::longmess;
29 0 $mess =~ s/( at .*?\n)\1/$1/s; # Suppress duplicate tracebacks
30 0 $mess;
31 }
32  
33 1 my @HOOKS = qw(__DIE__ __WARN__);
34 1 my %OLD_SIG;
35  
36 1 ▶sub import {
Subroutine import: called CC=2 SCAR=10.0
37 0 ▶ my $class = shift;
Subroutine import: not called CC=2 SCAR=10.0
38 0 ▶ return if $OLD_SIG{$class};
Branch: 2 branches
BranchTrueFalseTotal
if $OLD_SIG{$class} 0 1 1
if $OLD_SIG{$class} 0 0 0
39 0 @{ $OLD_SIG{$class} }{ @HOOKS, 'Verbose' } = (@SIG{@HOOKS}, $Carp::Verbose);
40  
41 0 @SIG{@HOOKS} = ($class->can('_die'), $class->can('_warn'));
42 0 $Carp::Verbose = 'verbose'; # makes carp() cluck and croak() confess
43 }
44  
45 0 ▶sub unimport {
Subroutine unimport: not called CC=2 SCAR=14.0
46 0 ▶ my $class = shift;
Subroutine unimport: not called CC=2 SCAR=14.0
47 0 ▶ return unless $OLD_SIG{$class};
Branch: 2 branches
BranchTrueFalseTotal
unless $OLD_SIG{$class} 0 0 0
unless $OLD_SIG{$class} 0 0 0
48 1 ▶ no if "$]" <= 5.008008, 'warnings' => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 (@SIG{@HOOKS}, $Carp::Verbose) = @{ delete $OLD_SIG{$class} }{ @HOOKS, 'Verbose' };
50 }
51  
52 1 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<Carp> module does
91 the heavy work, through C<longmess()>. 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<carp> and C<croak> are made verbose,
97 behaving like C<cluck> and C<confess>.
98  
99 =head1 METHODS
100  
101 L<Carp::Always> implements the following methods.
102  
103 =head2 import
104  
105 Carp::Always->import()
106  
107 Enables L<Carp::Always>. 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<Carp::Always>. 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<Acme::JavaTrace> by Sébastien Aperghis-Tramoni.
128 Sébastien also has a newer module called
129 L<Devel::SimpleTrace> 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<Carp::Indeed>. After thinking long
135 and getting nowhere, I went with nuffin's suggestion
136 and now it is called C<Carp::Always>.
137  
138 =head1 SEE ALSO
139  
140 L<Carp>
141  
142 L<Acme::JavaTrace> and L<Devel::SimpleTrace>
143  
144 L<Carp::Always::Color>
145  
146 L<Carp::Source::Always>
147  
148 L<Devel::Confess>
149  
150 L<Carp::Always::SyntaxHighlightSource> and L<Carp::Always::DieOnly>
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<warn>, C<die>, 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<https://github.com/aferreira/cpan-Carp-Always/issues>
169  
170 Backlog in CPAN RT: L<https://rt.cpan.org/Public/Dist/Display.html?Name=Carp-Always>
171  
172 =head1 AUTHOR
173  
174 Adriano Ferreira, E<lt>ferreira@cpan.orgE<gt>
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
↑ Summary