| 1 | #+############################################################################## | ||||||||||||||||||||||||||||
| 2 | # # | ||||||||||||||||||||||||||||
| 3 | # File: No/Worries/Die.pm # | ||||||||||||||||||||||||||||
| 4 | # # | ||||||||||||||||||||||||||||
| 5 | # Description: error handling without worries # | ||||||||||||||||||||||||||||
| 6 | # # | ||||||||||||||||||||||||||||
| 7 | #-############################################################################## | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # module definition | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package No::Worries::Die; | ||||||||||||||||||||||||||||
| 14 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 20 | our $VERSION = "1.7"; | |||||||||||||||||||||||||||
| 17 | 20 | our $REVISION = sprintf("%d.%02d", q$Revision: 1.20 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # used modules | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 20 | ▶ | use Carp qw(shortmess longmess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 20 | ▶ | use No::Worries qw($ProgramName); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 20 | ▶ | use No::Worries::Export qw(export_control); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 20 | ▶ | use No::Worries::String qw(string_trim); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | # global variables | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 20 | our($Prefix, $Syslog); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | # kind of die() with sprintf()-like API | ||||||||||||||||||||||||||||
| 36 | # | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 31 | ▶ | sub dief ($@) { | ||||||||||||||||||||||||||
|
Subroutine
dief: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 31 | my($message, @arguments) = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 31 | ▶ | $message = sprintf($message, @arguments) if @arguments; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 31 | die(string_trim($message) . "\n"); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||
| 46 | # reasonable die() handler | ||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub handler ($) { | ||||||||||||||||||||||||||
|
Subroutine
handler: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 0 | my($message) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # do nothing if called parsing a module/eval or executing an eval | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return if not defined($^S) or $^S; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $^S or $^S
MC/DC0%0/2
not defined $^S or $^S
not defined $^S
$^S
Truth table0%0/3
not(A) or B
A= not defined $^SB= $^S
| |||||||||||||||||||||||||||||
| 54 | # handle a "normal" error | ||||||||||||||||||||||||||||
| 55 | 0 | $message = string_trim($message); | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($ENV{NO_WORRIES}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ($ENV{NO_WORRIES} =~ /\b(confess)\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $message = longmess($message); | |||||||||||||||||||||||||||
| 59 | 0 | goto done; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($ENV{NO_WORRIES} =~ /\b(croak)\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $message = shortmess($message); | |||||||||||||||||||||||||||
| 63 | 0 | goto done; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | $message .= "\n"; | |||||||||||||||||||||||||||
| 67 | done: | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($Syslog) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | unless (defined(&No::Worries::Syslog::syslog_error)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | eval { require No::Worries::Syslog }; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | warn($@); | |||||||||||||||||||||||||||
| 73 | 0 | $Syslog = 0; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($Syslog) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | eval { No::Worries::Syslog::syslog_error($message) }; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | warn($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 0 | die($Prefix . " " . $message); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||
| 85 | # module initialization | ||||||||||||||||||||||||||||
| 86 | # | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # we tell Carp to treat our package as being internal | ||||||||||||||||||||||||||||
| 89 | 20 | $Carp::Internal{ (__PACKAGE__) }++; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # we set a default prefix | ||||||||||||||||||||||||||||
| 92 | 20 | ▶ | $Prefix = length($ProgramName) ? "$ProgramName\:" : "***"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||
| 95 | # export control | ||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 66 | ▶ | sub import : method { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 99 | 66 | my($pkg, %exported); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 66 | $pkg = shift(@_); | |||||||||||||||||||||||||||
| 102 | 66 | grep($exported{$_}++, qw(dief)); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | $exported{"handler"} = sub { $SIG{__DIE__} = \&handler }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $exported{"syslog"} = sub { $Syslog = 1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 105 | 66 | export_control(scalar(caller()), $pkg, \%exported, @_); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 20 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | __DATA__ | ||||||||||||||||||||||||||||