Exception.pm

filter: Statementstmt 14.7% 10 / 68 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 37.5% 3 / 8 Podpod 100% 5 / 5 totaltotal 13.5% 18 / 133 CC 21 SCAR 39.2
CC
21
Cov
9%
throw
110.0
message
30.0
traceback
20.0
CRAP 50.2
↑ Summary Base.pm »
1 package Object::Exception;
2 =head1 NAME
3  
4 Object::Exception - Multi-threaded exception class
5  
6 =head1 VERSION
7  
8 version 1.14
9  
10 =head1 ABSTRACT
11  
12 Multi-threaded exception class
13  
14 package SampleException;
15 use Object::Base qw(Object::Exception);
16 #
17 package main;
18 use Object::Exception;
19 #
20 # Enable DEBUG for traceback
21 our $DEBUG = 1;
22 #
23 # throws Object::Exception type and its msg: Exception1
24 eval
25 {
26 throw("Exception1");
27 };
28 if ($@)
29 {
30 warn $@ if ref($@) eq "Object::Exception";
31 }
32 #
33 # throws SampleException type and its msg: This is sample exception
34 sub sub_exception
35 {
36 SampleException->throw("This is sample exception");
37 }
38 eval
39 {
40 sub_exception();
41 };
42 if ($@) {
43 # $@ and $@->message returns same result
44 warn $@->message if ref($@) eq "SampleException";
45 }
46 #
47 # throws Object::Exception type and its message: SampleException. Because msg is not defined!
48 eval
49 {
50 SampleException->throw();
51 };
52 if ($@)
53 {
54 if (ref($@) eq "SampleException")
55 {
56 warn $@;
57 } else
58 {
59 # warns 'This is type of Object::Exception and its message: SampleException'
60 warn "This is type of ".ref($@)." and its message: $@";
61 }
62 }
63  
64 =head1 DESCRIPTION
65  
66 =head2 Functions
67  
68 =head3 traceback($level)
69  
70 returns array specified level of traceback by calling point of traceback function.
71  
72 =head3 dump_trace(@trace)
73  
74 returns string dump of trace array. Always ends with "\n".
75  
76 =head3 throw($msg)
77  
78 dies with new Object::Exception instance with specified message.
79  
80 =head2 Methods
81  
82 =head3 $class->new($msg)
83  
84 returns new Object::Exception instance with specified message. If $main::DEBUG is setted TRUE, $object->debug attribute is setted 1.
85  
86 =head3 $object->message()
87  
88 returns message of Object::Exception instance. If $msg is defined with new() or throw(), always ends with "\n".
89 If $object->debug attribute is TRUE, dump generated with dump_trace is added to end of message.
90  
91 =head3 $class->throw($msg)
92  
93 dies with new Object::Exception derived-class instance with specified message. If B<$class> is not derived from Object::Exception,
94 does nothing. $msg value must be specified explicitly and it can be B<undef>. Otherwise, method runs as B<throw($class)> function.
95  
96 =cut
97 2 ▶use Object::Base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use overload '""' => \&message;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100  
101 BEGIN
102 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 require 5.008;
104 1 $Object::Exception::VERSION = '1.14';
105 1 @Object::Exception::EXPORT = qw(throw);
106 1 @Object::Exception::EXPORT_OK = qw(traceback dump_trace);
107 }
108  
109  
110 attributes qw(:shared msg debug trace);
111  
112  
113 sub traceback
114 0 ▶{
Subroutine traceback: not called CC=4 SCAR=30.0
Pod documented
115 0 my ($level) = @_;
116 0 ▶ $level = 0 unless defined($level) and $level >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and $level >= 0 0 0 0
Condition0%0/3
defined $level and $level >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $level and $level >= 0
defined $level $level >= 0
Truth table0%0/3
A and B
A= defined $levelB= $level >= 0
ABresult
0X0
100
111
117 0 my @result;
118 0 ▶ while (scalar(my @caller = caller($level++)))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(my(@caller) = caller $level++)) 0 0 0
119 {
120 0 my @caller_next = caller($level);
121 0 push @result, {
122 package => $caller[0],
123 filename => $caller[1],
124 line => $caller[2],
125 subroutine => $caller_next[3],
126 };
127 }
128 0 return @result;
129 }
130  
131 sub dump_trace
132 0 ▶{
Subroutine dump_trace: not called CC=3 SCAR=24.8
Pod documented
133 0 my @trace = @_;
134 0 my $result = "";
135 0 my $i = 1;
136 0 for my $trace (reverse @trace)
137 {
138 0 $result .= sprintf("%-".($i*1)."s", "");
139 0 $result .= "in $trace->{package} ";
140 0 $result .= "at ";
141 0 ▶ $result .= "$trace->{subroutine} " if defined($trace->{subroutine});
Branch: 1 branch
BranchTrueFalseTotal
if defined $trace->{"subroutine"} 0 0 0
142 0 $result .= "$trace->{filename} ";
143 0 $result .= "line $trace->{line}\n";
144 } continue
145 {
146 0 $i++;
147 }
148 0 return $result;
149 }
150  
151 sub throw
152 0 ▶{
Subroutine throw: not called CC=10 SCAR=47.0
Pod documented
153 0 my $class;
154 0 ▶ if (@_ > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
155 {
156 0 $class = shift;
157 } else
158 {
159 0 $class = __PACKAGE__;
160 }
161 0 my ($msg, $tracelevel) = @_;
162 0 ▶ return unless defined($class) and not ref($class) and UNIVERSAL::isa($class, __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and not ref $class and UNIVERSAL::isa($class, __PACKAGE__) 0 0 0
Condition0%0/6
defined $class and not ref $class and UNIVERSAL::isa($class, __PACKAGE__)
!ll&&!rl&&r
000
defined $class and not ref $class
!ll&&!rl&&r
000
MC/DC0%0/3
defined $class and not ref $class and UNIVERSAL::isa($class, __PACKAGE__)
defined $class not ref $class UNIVERSAL::isa($class, __PACKAGE__)
Truth table0%0/4
A and not(B) and C
A= defined $classB= not ref $classC= UNIVERSAL::isa($class, __PACKAGE__)
ABCresult
0XX0
10X0
1100
1111
163 0 my @trace;
164 0 ▶ if (ref($msg))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg) 0 0 0
165 {
166 0 ▶ return unless UNIVERSAL::isa($msg, __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($msg, __PACKAGE__) 0 0 0
167 0 ▶ $class = ref($msg) if $class eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ 0 0 0
168 0 unshift @trace, @{$msg->trace};
169 0 $msg = $msg->msg;
170 }
171 0 my $self = $class->new($msg);
172 0 ▶ $tracelevel = 0 unless defined($tracelevel);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tracelevel 0 0 0
173 0 $tracelevel++;
174 0 my @traceback = traceback($tracelevel);
175 0 ▶ unless (@trace)
Branch: 1 branch
BranchTrueFalseTotal
if (@trace) { } 0 0 0
176 {
177 0 unshift @trace, @traceback;
178 } else
179 {
180 0 unshift @trace, $traceback[0];
181 }
182 0 $self->trace(\@trace);
183 0 die $self;
184 }
185 ################################################################################
186  
187 sub new
188 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
189 0 my $class = shift;
190 0 my ($msg) = @_;
191 0 my $self = $class->SUPER();
192 0 $self->msg($msg);
193 0 ▶ $self->debug((defined($main::DEBUG) and $main::DEBUG)? 1: 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $main::DEBUG && $main::DEBUG ? : 0 0 0
Condition0%0/3
defined $main::DEBUG and $main::DEBUG
!ll&&!rl&&r
000
MC/DC0%0/2
defined $main::DEBUG and $main::DEBUG
defined $main::DEBUG $main::DEBUG
Truth table0%0/3
A and B
A= defined $main::DEBUGB= $main::DEBUG
ABresult
0X0
100
111
194 0 $self->trace([]);
195 0 return $self;
196 }
197  
198 sub message
199 0 ▶{
Subroutine message: not called CC=5 SCAR=34.0
Pod documented
200 0 my $self = shift;
201 0 my ($debug) = @_;
202 0 ▶ $debug = $self->debug unless defined($debug);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $debug 0 0 0
203 0 my $msg = $self->msg;
204 0 my $result = "";
205 0 ▶ $result .= "$msg\n" if defined($msg) and not ref($msg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg and not ref $msg 0 0 0
Condition0%0/3
defined $msg and not ref $msg
!ll&&!rl&&r
000
MC/DC0%0/2
defined $msg and not ref $msg
defined $msg not ref $msg
Truth table0%0/3
A and not(B)
A= defined $msgB= not ref $msg
ABresult
0X0
100
111
206 0 ▶ return $result unless $debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug 0 0 0
207 0 $result .= dump_trace(@{$self->trace});
208 0 return $result;
209 }
210  
211  
212 1;
213 __END__
↑ Summary Base.pm »