blib/lib/No/Worries/Warn.pm

filter: Statementstmt 42.1% 24 / 57 Branchbran 5.0% 1 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 100% 2 / 2 totaltotal 36.6% 33 / 90 CC 10 SCAR 37.3
CC
10
Cov
32%
handler
90.0
warnf
6.0
import
2.0
CRAP 41.7
« blib/lib/No/Worries/Die.pm ↑ Summary blib/lib/No/Worries/File.pm »
1 #+##############################################################################
2 # #
3 # File: No/Worries/Warn.pm #
4 # #
5 # Description: warning handling without worries #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package No::Worries::Warn;
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 our $VERSION = "1.7";
17 1 our $REVISION = sprintf("%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 1 ▶use Carp qw(shortmess longmess);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use No::Worries qw($ProgramName);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use No::Worries::String qw(string_trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #
29 # global variables
30 #
31  
32 1 our($Prefix, $Syslog);
33  
34 #
35 # kind of warn() with sprintf()-like API
36 #
37  
38 0 ▶sub warnf ($@) {
Subroutine warnf: not called CC=2 SCAR=17.9
Pod documented
39 0 my($message, @arguments) = @_;
40  
41 0 ▶ $message = sprintf($message, @arguments) if @arguments;
Branch: 1 branch
BranchTrueFalseTotal
if @arguments 0 0 0
42 0 warn(string_trim($message) . "\n");
43 }
44  
45 #
46 # reasonable warn() handler
47 #
48  
49 0 ▶sub handler ($) {
Subroutine handler: not called CC=9 SCAR=45.0
Pod documented
50 0 my($message) = @_;
51  
52 0 $message = string_trim($message);
53 0 ▶ if ($ENV{NO_WORRIES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"NO_WORRIES"}) 0 0 0
54 0 ▶ if ($ENV{NO_WORRIES} =~ /\b(cluck)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"NO_WORRIES"} =~ /\b(cluck)\b/) 0 0 0
55 0 $message = longmess($message);
56 0 goto done;
57 }
58 0 ▶ if ($ENV{NO_WORRIES} =~ /\b(carp)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"NO_WORRIES"} =~ /\b(carp)\b/) 0 0 0
59 0 $message = shortmess($message);
60 0 goto done;
61 }
62 }
63 0 $message .= "\n";
64 done:
65 0 ▶ if ($Syslog) {
Branch: 1 branch
BranchTrueFalseTotal
if ($No::Worries::Warn::Syslog) 0 0 0
66 0 ▶ unless (defined(&No::Worries::Syslog::syslog_warning)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &No::Worries::Syslog::syslog_warning) 0 0 0
67 0 eval { require No::Worries::Syslog };
68 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
69 0 warn($@);
70 0 $Syslog = 0;
71 }
72 }
73 0 ▶ if ($Syslog) {
Branch: 1 branch
BranchTrueFalseTotal
if ($No::Worries::Warn::Syslog) 0 0 0
74 0 eval { No::Worries::Syslog::syslog_warning($message) };
75 0 ▶ warn($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
76 }
77 }
78 0 warn($Prefix . " " . $message);
79 }
80  
81 #
82 # module initialization
83 #
84  
85 # we tell Carp to treat our package as being internal
86 1 $Carp::Internal{ (__PACKAGE__) }++;
87  
88 # we set a default prefix
89 1 ▶$Prefix = length($ProgramName) ? "$ProgramName\!" : "*";
Branch: 1 branch
BranchTrueFalseTotal
length $No::Worries::Warn::ProgramName ? : 1 0 1
90  
91 #
92 # export control
93 #
94  
95 0 ▶sub import : method {
Subroutine import: not called CC=1 SCAR=6.9
96 0 my($pkg, %exported);
97  
98 0 $pkg = shift(@_);
99 0 grep($exported{$_}++, qw(warnf));
100 0 ▶ $exported{"handler"} = sub { $SIG{__WARN__} = \&handler };
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 ▶ $exported{"syslog"} = sub { $Syslog = 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 export_control(scalar(caller()), $pkg, \%exported, @_);
103 }
104  
105 1 1;
106  
107 __DATA__
« blib/lib/No/Worries/Die.pm ↑ Summary blib/lib/No/Worries/File.pm »