Debian Codegraph @

README

raw
1 NAME
2 TryCatch - first class try catch semantics for Perl, without source
3 filters.
4
5 DESCRIPTION
6 This module aims to provide a nicer syntax and method to catch errors in
7 Perl, similar to what is found in other languages (such as Java, Python
8 or C++). The standard method of using "eval {}; if ($@) {}" is often
9 prone to subtle bugs, primarily that its far too easy to stomp on the
10 error in error handlers. And also eval/if isn't the nicest idiom.
11
12 SYNOPSIS
13 use TryCatch;
14
15 sub foo {
16 my ($self) = @_;
17
18 try {
19 die Some::Class->new(code => 404 ) if $self->not_found;
20 return "return value from foo";
21 }
22 catch (Some::Class $e where { $_->code > 100 } ) {
23 }
24 }
25
26 SYNTAX
27 This module aims to give first class exception handling to perl via
28 'try' and 'catch' keywords. The basic syntax this module provides is
29 "try { # block }" followed by zero or more catch blocks. Each catch
30 block has an optional type constraint on it the resembles Perl6's method
31 signatures.
32
33 Also worth noting is that the error variable ($@) is localised to the
34 try/catch blocks and will not leak outside the scope, or stomp on a
35 previous value of $@.
36
37 The simplest case of a catch block is just
38
39 catch { ... }
40
41 where upon the error is available in the standard $@ variable and no
42 type checking is performed. The exception can instead be accessed via a
43 named lexical variable by providing a simple signature to the catch
44 block as follows:
45
46 catch ($err) { ... }
47
48 Type checking of the exception can be performed by specifing a type
49 constraint or where clauses in the signature as follows:
50
51 catch (TypeFoo $e) { ... }
52 catch (Dict[code => Int, message => Str] $err) { ... }
53
54 As shown in the above example, complex Moose types can be used,
55 including MooseX::Types style of type constraints
56
57 In addition to type checking via Moose type constraints, you can also
58 use where clauses to only match a certain sub-condition on an error. For
59 example, assuming that "HTTPError" is a suitably defined TC:
60
61 catch (HTTPError $e where { $_->code >= 400 && $_->code <= 499 } ) {
62 return "4XX error";
63 }
64 catch (HTTPError $e) {
65 return "other http code";
66 }
67
68 would return "4XX error" in the case of a 404 error, and "other http
69 code" in the case of a 302.
70
71 In the case where multiple catch blocks are present, the first one that
72 matches the type constraints (if any) will executed.
73
74 BENEFITS
75 return. You can put a return in a try block, and it would do the right
76 thing - namely return a value from the subroutine you are in, instead of
77 just from the eval block.
78
79 Type Checking. This is nothing you couldn't do manually yourself, it
80 does it for you using Moose type constraints.
81
82 TODO
83 * Decide on "finally" semantics w.r.t return values.
84
85 * Write some more documentation
86
87 * Split out the dependancy on Moose
88
89 SEE ALSO
90 MooseX::Types, Moose::Util::TypeConstraints, Parse::Method::Signatures.
91
92 AUTHOR
93 Ash Berlin <[email protected]>
94
95 THANKS
96 Thanks to Matt S Trout and Florian Ragwitz for work on Devel::Declare
97 and various B::Hooks modules
98
99 Vincent Pit for Scope::Upper that makes the return from block possible.
100
101 Zefram for providing support and XS guidance.
102
103 Xavier Bergade for the impetus to finally fix this module in 5.12.
104
105 LICENSE
106 Licensed under the same terms as Perl itself.
107