blib/lib/PException.pm

filter: Statementstmt 89.0% 89 / 100 Branchbran 73.3% 22 / 30 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 88.8% 16 / 18 Podpod 90.9% 10 / 11 totaltotal 84.8% 146 / 172 CC 21 SCAR 14.8
CC
21
Cov
85%
__onflyhandler
10.6
try
5.1
throw
5.0
CRAP 4.4
↑ Summary
1 # -*- cperl -*-
2 #
3 # Copyright (c) 1997-2003 Samuel MOUNIEE
4 #
5 # This file is part of PException.
6 #
7 # PException is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # PException is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with PException; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #
21 package PException;
22  
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK $_XCPHDL_ );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #sub DEBUG { 5 }
27 #sub debug { print STDERR join( " ! ", @_ ) if $_[0] >= DEBUG }
28 10 ▶sub debug { }
Subroutine debug: called CC=1 SCAR=0.0
Pod undocumented
29  
30 1 require Exporter;
31  
32 1 $VERSION= "2.4";
33  
34 =pod
35  
36 =head1 NAME
37  
38 PException - Exception manager
39  
40 =head1 SYNOPSIS
41  
42 use PException;
43  
44  
45 try {
46 throw( AnException->new() ) if $something;
47 throw( AnOtherException->new( [] ) )
48 unless $anotherthing;
49 }
50 catch AnException( sub { } ) ,
51 onfly AnOtherException( sub { } ) ;
52  
53  
54 =cut
55  
56 1 @ISA = qw( Exporter );
57 1 @EXPORT = qw( throw try );
58 1 @EXPORT_OK = qw( );
59  
60 #my $_XCPHDL_ = [ {
61 # EXCEPTIONS => 1,
62 # FLYS => 2,
63 # STACKFLY => 3,
64 # ONFLY => 4,
65 # FRESH => 5,
66 # CANDIE => 6
67 # }, [], [], [], 0, 0, 0 ];
68  
69 1 $_XCPHDL_ = {
70 EXCEPTIONS => [],
71 FLYS => [],
72 STACKFLY => [],
73 ONFLY => 0,
74 FRESH => 0,
75 CANDIE => 0
76 };
77  
78 BEGIN
79 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 $SIG{__WARN__} = \&__onflyhandler;
81  
82 6 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 # $PException::_XCPHDL_->{CANDIE} = 1;
84  
85 # if( $@ && hadWaittingExceptions() )
86 # { debug( 2, "NN-> $@ <" ) }
87 # elsif ( $@ ) { debug( 2, "NO-> $@ <" ); $@ = undef }
88 # elsif ( @_ ) { debug( 2, "ON->", @_, "<" ) }
89 # else { debug( 2, "OO->", $@, @_, "<" ) }
90 # $@ .= " - ";
91  
92 6 die ( $@, @_ );
93 # throw();
94 1 };
95 }
96  
97  
98 =pod
99  
100 =head1 DESCRIPTION
101  
102 =head2 Methods & Functions
103  
104 =over 4
105  
106 =item try
107  
108 execute code until its end or an exception happens.
109  
110 =cut
111  
112 sub try(&@)
113 7 ▶{
Subroutine try: called CC=5 SCAR=16.3
Pod documented
114 7 my $e = shift;
115  
116 7 my @catch = grep { $_->isa("PException::CATCH") } @_;
117 7 my @onfly = grep { $_->isa("PException::ONFLY") } @_;
118  
119 ## add an new stack of onfly exception
120  
121 7 push( @{$PException::_XCPHDL_->{STACKFLY}}, [ @onfly ] );
122  
123 # debug( 1, "try", $e, @_ );
124  
125 7 eval { &$e; };
126  
127 ## remove the stack of onfly exception
128  
129 7 pop( @{$PException::_XCPHDL_->{STACKFLY}} );
130  
131 7 ▶ if( $@ && hadWaittingExceptions() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ and hadWaittingExceptions()) { } 6 1 7
elsif ($@) { } 0 1 1
Condition66%2/3
$@ and hadWaittingExceptions()
!ll&&!rl&&r
106
MC/DC50%1/2
$@ and hadWaittingExceptions()
$@ hadWaittingExceptions()
Truth table66%2/3
A and B
A= $@B= hadWaittingExceptions()
ABresult
0X0
100
111
132 6 chomp( $@ );
133 # debug( 2, "---> $@ <" );
134 6 ▶ throw() if checkException( @catch );
Branch: 1 branch
BranchTrueFalseTotal
if &checkException(@catch) 1 5 6
135 }
136 elsif ( $@ ) {
137 0 chomp( $@ );
138 # debug( 2, "no-> $@ <" );
139 0 $@ = undef;
140 }
141  
142 # debug( 2, "STACKFLY", scalar( @{$PException::_XCPHDL_->{STACKFLY}} ) );
143 }
144  
145 =pod
146  
147 =item throw
148  
149 throw a list of exceptions.
150  
151 =cut
152  
153 sub throw(@)
154 14 ▶{
Subroutine throw: called CC=5 SCAR=16.1
Pod documented
155 # debug( 1, "throw", "begin" );
156  
157 14 $PException::_XCPHDL_->{FRESH} = 0;
158 14 ▶ if ( @_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 7 7 14
159 7 push( @{$PException::_XCPHDL_->{EXCEPTIONS}}, @_ );
160 7 $PException::_XCPHDL_->{FRESH} = 1;
161 }
162  
163 14 my @tmp = @{$PException::_XCPHDL_->{EXCEPTIONS}};
164  
165 # debug( 2, "throw", @tmp );
166  
167 14 map { $_ = ref($_) . "($$_)" } @tmp;
168  
169 14 ▶ if ( !$PException::_XCPHDL_->{ONFLY} && $PException::_XCPHDL_->{CANDIE} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $PException::_XCPHDL_->{'ONFLY'} and $PException::_XCPHDL_->{'CANDIE'}) { } 6 8 14
elsif (not $PException::_XCPHDL_->{'ONFLY'}) { } 8 0 8
Condition66%2/3
not $PException::_XCPHDL_->{"ONFLY"} and $PException::_XCPHDL_->{"CANDIE"}
!ll&&!rl&&r
086
MC/DC50%1/2
not $PException::_XCPHDL_->{"ONFLY"} and $PException::_XCPHDL_->{"CANDIE"}
not $PException::_XCPHDL_->{"ONFLY"} $PException::_XCPHDL_->{"CANDIE"}
Truth table66%2/3
not(A) and B
A= not $PException::_XCPHDL_->{"ONFLY"}B= $PException::_XCPHDL_->{"CANDIE"}
ABresult
0X0
100
111
170 # debug( 2, "throw", "Die" );
171 6 $_XCPHDL_->{CANDIE} = 0;
172 6 die( join( "\t- ", "Die", @tmp ) );
173 }
174 elsif( !$PException::_XCPHDL_->{ONFLY} )
175 {
176 # debug( 2, "throw", "Warn" );
177 8 warn( join( "\t- ", "Warn", @tmp ) );
178 }
179 }
180  
181  
182 =pod
183  
184 =item new
185  
186 create a new instance of an Exception.
187  
188 =cut
189  
190 8 ▶sub new($@) {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
191 8 my $classe = shift;
192 8 my $sc = shift;
193  
194 8 ▶ return bless $sc, $classe if ref( $sc );
Branch: 1 branch
BranchTrueFalseTotal
if ref $sc 0 8 8
195 8 return bless \$sc, $classe;
196 }
197  
198  
199 =pod
200  
201 =item catch
202  
203 execute an subroutine if this kind of exception is thrown
204  
205 =cut
206 6 ▶sub catch($&) { return newTypedCatching( $_[0], $_[1], "CATCH" ) }
Subroutine catch: called CC=1 SCAR=0.0
Pod documented
207  
208 =pod
209  
210 =item onfly
211  
212 execute an subroutine and continue the execution of the last try
213 if this kind of exception is thrown
214  
215 =cut
216 1 ▶sub onfly($&) { return newTypedCatching( $_[0], $_[1], "ONFLY" ) }
Subroutine onfly: called CC=1 SCAR=0.0
Pod documented
217  
218  
219 =pod
220  
221 =item addFlyingHandler
222  
223 add a flying exception handler. it allows to continue the try
224 block where the exception appear.
225  
226 =cut
227 sub addFlyingHandler($&)
228 1 ▶{ push( @{$PException::_XCPHDL_->{FLYS}}, newTypedCatching( $_[0], $_[1], "FLYS" ) ) }
Subroutine addFlyingHandler: called CC=1 SCAR=0.0
Pod documented
229  
230  
231 =pod
232  
233 =back
234  
235 =head2 Internal Calls
236  
237 =over 4
238  
239 =item hadWaittingExceptions
240  
241 return true if there is waitting exceptions, false elsewhere
242  
243 =cut
244 28 ▶sub hadWaittingExceptions { return scalar(@{$PException::_XCPHDL_->{EXCEPTIONS}})>0 }
Subroutine hadWaittingExceptions: called CC=1 SCAR=0.0
Pod documented
245  
246  
247 =pod
248  
249 =item newTypedCatching
250  
251 create an object for catching methods.
252 when a catch happens,
253  
254 =cut
255 8 ▶sub newTypedCatching($$$) {
Subroutine newTypedCatching: called CC=3 SCAR=11.0
Pod documented
256 8 my ( $s, $c, $sig ) = @_;
257  
258 8 ▶ if ( !defined( $c ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 3 5 8
259 3 ▶ { $c = sub {} unless ( $c = $s->can( "handleException" ) ) }
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless $c = $s->can("handleException") 0 3 3
260  
261 10 ▶ return bless sub() {
Subroutine __ANON__: called CC=2 SCAR=6.9
262 10 debug( 1, $sig, $s, $c, @{$PException::_XCPHDL_->{EXCEPTIONS}} );
263  
264 10 ▶ if ( my @tmp = grep { $_->isa($s) } @{$PException::_XCPHDL_->{EXCEPTIONS}} )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@tmp) = grep({$_->isa($s);} @{$$PException::_XCPHDL_{"EXCEPTIONS"};})) 8 2 10
265 {
266 8 @{$PException::_XCPHDL_->{EXCEPTIONS}} =
267 8 grep { !($_->isa($s)) } @{$PException::_XCPHDL_->{EXCEPTIONS}};
268 8 &$c(@tmp);
269 # @{$PException::_XCPHDL_->{EXCEPTIONS}} = ( @{$PException::_XCPHDL_->{EXCEPTIONS}}, @tmp );
270 8 return 1;
271 }
272 2 return 0;
273 8 }, "PException::$sig";
274 }
275  
276 =pod
277  
278 =item checkException
279  
280 control if there is some exceptions to catch & treat in an stack
281  
282 =cut
283 14 ▶sub checkException(@) {
Subroutine checkException: called CC=2 SCAR=6.9
Pod documented
284 14 foreach(@_){&{$_}()}
285 14 return hadWaittingExceptions();
286 }
287  
288  
289 =pod
290  
291 =item handleException
292  
293 code for handling a exception. it's an empty sub actually.
294 you overload it if you want a default handler for an exception.
295  
296 =cut
297 0 ▶sub handleException { }
Subroutine handleException: not called CC=1 SCAR=0.0
Pod documented
298  
299  
300 =pod
301  
302 =item __onflyhandler
303  
304 the handler which intercept __WARN__ or __DIE__ signal
305  
306 =cut
307 8 ▶sub __onflyhandler {
Subroutine __onflyhandler: called CC=8 SCAR=23.6
308 8 ▶ if ( hadWaittingExceptions() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (hadWaittingExceptions()) { } 8 0 8
309 8 my ( @tmp );
310 # debug( 1, "FLYS HANDLER", "Begin" );
311  
312 ## get the current stack of onfly exception
313 8 ▶ if ( scalar( @{$PException::_XCPHDL_->{STACKFLY}} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$PException::_XCPHDL_->{'STACKFLY'};}) { } 8 0 8
314 8 @tmp = @{$PException::_XCPHDL_->{STACKFLY}->[
315 8 $#{$PException::_XCPHDL_->{STACKFLY}}]};
316 } else {
317 0 my ( $i, @tmp ) = ( 0 );
318  
319 0 ▶ while( @tmp = caller( $i++ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp = caller $i++) 0 0 0
320 0 { print STDERR "$i\t> " . join( " + ", grep { defined $_ } @tmp, "\n" ) }
321  
322 0 for ( @{$PException::_XCPHDL_->{EXCEPTIONS}} )
323 0 { print STDERR "\t>$_ - ", $$_ , "\n" }
324 0 die( "ya1kouille! PException thrown with no try block" )
325 }
326  
327 # debug( 2, "FLYS HANDLER", @tmp );
328  
329 ## add flying handlers if fresh exception
330 2 push( @tmp, @{$PException::_XCPHDL_->{FLYS}} )
331 if ( ( $PException::_XCPHDL_->{FRESH} ) &&
332 8 ▶ scalar( @{$PException::_XCPHDL_->{FLYS}} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $PException::_XCPHDL_->{"FRESH"} and scalar @{$PException::_XCPHDL_->{"FLYS"};} 2 6 8
Condition100%2/2
$PException::_XCPHDL_->{"FRESH"} and scalar @{$PException::_XCPHDL_->{"FLYS"};}
!ll
17
MC/DC100%1/1
$PException::_XCPHDL_->{"FRESH"} and scalar @{$PException::_XCPHDL_->{"FLYS"};}
$PException::_XCPHDL_->{"FRESH"}
Truth table100%2/2
A
A= $PException::_XCPHDL_->{"FRESH"}
Aresult
00
11
333  
334 8 $PException::_XCPHDL_->{ONFLY} = 1;
335  
336 8 ▶ if ( checkException( @tmp ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&checkException(@tmp)) 6 2 8
337 # debug( 2, "FLYS HANDLER", "Following" );
338 6 $PException::_XCPHDL_->{ONFLY} = 0;
339 6 $PException::_XCPHDL_->{CANDIE}= 1;
340 6 throw();
341 }
342 # debug( 2, "FLYS HANDLER", "End" );
343 2 undef $@;
344 2 $PException::_XCPHDL_->{ONFLY} = 0;
345 0 } else { print STDERR $@ }
346 }
347  
348 __END__
↑ Summary