File Coverage

blib/lib/Erlang/Parser/Node/Try.pm
Criterion Covered Total %
statement 58 60 96.6
branch 12 14 85.7
condition 7 11 63.6
subroutine 2 2 100.0
pod 1 1 100.0
total 80 88 90.9


line stmt bran cond sub pod time code
1             # Copyright 2011-2012 Yuki Izumi. ( anneli AT cpan DOT org )
2             # This is free software; you can redistribute it and/or modify it under the
3             # same terms as Perl itself.
4              
5             package Erlang::Parser::Node::Try;
6              
7 3     3   11 use Moose;
  3         4  
  3         15  
8             with 'Erlang::Parser::Node';
9              
10             has 'exprs' => (is => 'rw', required => 1, isa => 'ArrayRef[Erlang::Parser::Node]');
11             has 'of' => (is => 'rw', required => 0, isa => 'Maybe[ArrayRef[Erlang::Parser::Node::Alt]]');
12             has 'catch' => (is => 'rw', required => 0, isa => 'Maybe[ArrayRef[Erlang::Parser::Node::Alt]]');
13             has 'aft' => (is => 'rw', required => 0, isa => 'Maybe[ArrayRef[Erlang::Parser::Node]]');
14              
15             sub print {
16 62     62 1 118 my ($self, $fh, $depth) = @_;
17 62   50     149 $depth ||= 0;
18              
19 62         122 print $fh "try\n";
20              
21 62         73 $depth++;
22 62         139 print $fh "\t" x $depth;
23              
24 62         93 my $first = 1;
25 62         114 foreach (@{$self->exprs}) {
  62         1672  
26 90 100       195 if ($first) { $first = 0 } else { print $fh ",\n", "\t" x $depth }
  62         91  
  28         59  
27 90         267 $_->print($fh, $depth);
28             }
29              
30 62         122 $depth--;
31 62         139 print $fh "\n", "\t" x $depth;
32              
33 62 100 66     1655 if (defined $self->of and @{$self->of}) {
  2         47  
34 2         7 print $fh "of\n";
35              
36 2         4 $depth++;
37 2         5 print $fh "\t" x $depth;
38              
39 2         4 my $first = 1;
40 2         2 foreach (@{$self->of}) {
  2         45  
41 4 100       7 if ($first) { $first = 0 } else { print $fh ";\n", "\t" x $depth }
  2         4  
  2         6  
42 4         11 $_->print($fh, $depth);
43             }
44              
45 2         4 $depth--;
46 2         8 print $fh "\n", "\t" x $depth;
47             }
48              
49 62 100 66     1659 if (defined $self->catch and @{$self->catch}) {
  52         1292  
50 52         95 print $fh "catch\n";
51              
52 52         62 $depth++;
53 52         85 print $fh "\t" x $depth;
54              
55 52         101 my $first = 1;
56 52         54 foreach (@{$self->catch}) {
  52         1308  
57 52 50       114 if ($first) { $first = 0 } else { print $fh ";\n", "\t" x $depth }
  52         77  
  0         0  
58 52         197 $_->print($fh, $depth);
59             }
60              
61 52         63 $depth--;
62 52         101 print $fh "\n", "\t" x $depth;
63             }
64              
65 62 100 66     1694 if (defined $self->aft and @{$self->aft}) {
  10         220  
66 10         22 print $fh "after\n";
67              
68 10         16 $depth++;
69 10         20 print $fh "\t" x $depth;
70              
71 10         13 my $first = 1;
72 10         10 foreach (@{$self->aft}) {
  10         223  
73 10 50       25 if ($first) { $first = 0 } else { print $fh ";\n", "\t" x $depth }
  10         13  
  0         0  
74 10         38 $_->print($fh, $depth);
75             }
76              
77 10         12 $depth--;
78 10         23 print $fh "\n", "\t" x $depth;
79             }
80              
81 62         173 print $fh "end";
82             }
83              
84             __PACKAGE__->meta->make_immutable;
85              
86             =head1 NAME
87              
88             Erlang::Parser::Node::Try - a try/catch clause
89              
90             =head1 DESCRIPTION
91              
92             A clause to catch exceptions. A block of expressions is evaluated; the last
93             expression's value is optionally then matched against patterns and guards, and
94             then a further block of statements executed. Exceptions raised therein can be
95             caught in the catch clause. Finally, cleanup statements can be invoked.
96              
97             =head2 Accessors
98              
99             =over 4
100              
101             =item C<exprs>
102              
103             A list of L<Erlang::Parser::Node>s; the last expression's value is that used in
104             the of clause.
105              
106             =item C<of>
107              
108             An optional list of L<Erlang::Parser::Node::Alt>s against which the last
109             expression in C<exprs> is matched.
110              
111             =item C<catch>
112              
113             An optional list of L<Erlang::Parser::Node::Alt>s for exceptions raised during
114             evaluation in C<exprs> and C<of>.
115              
116             =item C<aft>
117              
118             An optional list of L<Erlang::Parser::Node>s, executed after all previous
119             statements.
120              
121             =back
122              
123             =head2 Methods
124              
125             =over 4
126              
127             =item C<print>
128              
129             Pretty-prints the node to its filehandle argument.
130              
131             =back
132              
133             =head1 EXAMPLE
134              
135             try
136             {ok, X} = my_fun(),
137             binary_to_term(X)
138             catch
139             throw:Term -> Term
140             after
141             file:close(F)
142             end
143              
144             =cut
145              
146             1;
147              
148             # vim: set sw=4 ts=4: