XML/Template/Element/Exception.pm untested

filter: Statementstmt 0.0% 0 / 28 Branchbran n/a Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 45 SCAR 6.9
CC
1
Cov
0%
throw
2.0
try
2.0
catch
2.0
CRAP 2.0
1 ###############################################################################
2 # XML::Template::Element::Exception
3 #
4 # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu>
5 # All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 ###############################################################################
10 package XML::Template::Element::Exception;
11 use base qw(XML::Template::Element);
12  
13 use strict;
14 use XML::Template::Element;
15  
16  
17 =pod
18  
19 =head1 NAME
20  
21 XML::Template::Element::Exception - XML::Template plugin module for the
22 exception namespace.
23  
24 =head1 SYNOPSIS
25  
26 This XML::Template plugin module implements the exception namespace
27 tagset. The exception namespace includes tags that handle exceptions.
28  
29 =head1 CONSTRUCTOR
30  
31 XML::Template::Element::Block inherits its constructor method, C<new>,
32 from L<XML::Template::Element>.
33  
34 =head1 EXCEPTION TAGSET METHODS
35  
36 =head2 throw
37  
38 This method implements the tag C<throw> which throws an exception. The
39 following attributes are used:
40  
41 =over 4
42  
43 =item name
44  
45 The name of the exception to throw.
46  
47 =item info
48  
49 A description of what caused the exception.
50  
51 =back
52  
53 =cut
54  
55 sub throw {
56 my $self = shift;
57 my ($code, $attribs) = @_;
58  
59 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
60 my $info = $self->get_attrib ($attribs, 'info') || 'undef';
61  
62 my $outcode = qq!
63 die XML::Template::Exception->new ($name, $info);
64 !;
65  
66 return $outcode;
67 }
68  
69 =pod
70  
71 =head2 try
72  
73 This method implements the beginning of the try structure. The content of
74 this element will be evaluated. If an exception is thrown it can be
75 caught by the child element, C<catch>.
76  
77 =cut
78  
79 sub try {
80 my $self = shift;
81 my ($code, $attribs) = @_;
82  
83 my $outcode = qq!
84 do {
85 my \$caught = 0;
86  
87 my \$__eval_error;
88 my \$first = 1;
89  
90 eval {
91 $code
92 };
93  
94 die \$\@ if \$\@ && \! \$caught;
95 };
96 !;
97 #print $outcode;
98  
99 return $outcode;
100 }
101  
102 =pod
103  
104 =head2 catch
105  
106 This method implements the catch section of a try structure. If the
107 content of the C<exception> element raises an exception and is caught by
108 this element, the content will be evaluated. The following attributes
109 are used:
110  
111 =over 4
112  
113 =item name
114  
115 The name of the exception to catch.
116  
117 =back
118  
119 =cut
120  
121 sub catch {
122 my $self = shift;
123 my ($code, $attribs) = @_;
124  
125 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
126  
127 my $outcode = qq!
128 };
129 if (\$first) {
130 \$__eval_error = \$\@ if \$\@;
131 \$first = 0;
132 }
133  
134 if (defined \$__eval_error && \! \$caught) {
135 my \$exception = ref (\$__eval_error)
136 ? \$__eval_error
137 : XML::Template::Exception->new (undef, \$__eval_error);
138 \$vars->set ('Exception.type' => \$exception->type);
139 \$vars->set ('Exception.info' => \$exception->info);
140 if (defined $name) {
141 if (\$exception->isa ($name)) {
142 \$caught = 1;
143 $code
144 }
145 } else {
146 \$caught = 1;
147 $code
148 }
149 !;
150  
151 return $outcode;
152 }
153  
154 =pod
155  
156 =head2 else
157  
158 This method implements the else section of a try structure. If no
159 exception is matched by the C<catch> elements, the content of this element
160 will be evaluated.
161  
162 =cut
163  
164 sub else {
165 my $self = shift;
166 my ($code, $attribs) = @_;
167  
168 my $outcode = qq!
169 };
170 if (\! defined \$__eval_error) {
171 $code
172 !;
173  
174 return $outcode;
175 }
176  
177 =pod
178  
179 =head1 AUTHOR
180  
181 Jonathan Waxman
182 <jowaxman@bbl.med.upenn.edu>
183  
184 =head1 COPYRIGHT
185  
186 Copyright (c) 2002-2003 Jonathan A. Waxman
187 All rights reserved.
188  
189 This program is free software; you can redistribute it and/or
190 modify it under the same terms as Perl itself.
191  
192 =cut
193  
194  
195 1;