Debian Codegraph @

README

raw
1 NAME
2
3 Feature::Compat::Try - make try/catch syntax available
4
5 SYNOPSIS
6
7 use Feature::Compat::Try;
8
9 sub foo
10 {
11 try {
12 attempt_a_thing();
13 return "success";
14 }
15 catch ($e) {
16 warn "It failed - $e";
17 return "failure";
18 }
19 }
20
21 DESCRIPTION
22
23 This module makes syntax support for try/catch control flow easily
24 available.
25
26 Perl added such syntax at version 5.34.0, and extended it to support
27 optional finally blocks at 5.35.9, which is enabled by
28
29 use feature 'try';
30
31 On that version of perl or later, this module simply enables the core
32 feature equivalent to using it directly. On such perls, this module
33 will install with no non-core dependencies, and requires no C compiler.
34
35 On older versions of perl before such syntax is available, it is
36 currently provided instead using the Syntax::Keyword::Try module,
37 imported with a special set of options to configure it to recognise
38 exactly and only the same syntax as the core perl feature, thus
39 ensuring that any code using it will still continue to function on that
40 newer perl.
41
42 KEYWORDS
43
44 try
45
46 try {
47 STATEMENTS...
48 }
49 ...
50
51 A try statement provides the main body of code that will be invoked,
52 and must be followed by a catch statement. It may optionally be
53 followed by a finally statement.
54
55 Execution of the try statement itself begins from the block given to
56 the statement and continues until either it throws an exception, or
57 completes successfully by reaching the end of the block.
58
59 The body of a try {} block may contain a return expression. If
60 executed, such an expression will cause the entire containing function
61 to return with the value provided. This is different from a plain eval
62 {} block, in which circumstance only the eval itself would return, not
63 the entire function.
64
65 The body of a try {} block may contain loop control expressions (redo,
66 next, last) which will have their usual effect on any loops that the
67 try {} block is contained by.
68
69 The parsing rules for the set of statements (the try block and its
70 associated catch) are such that they are parsed as a self-contained
71 statement. Because of this, there is no need to end with a terminating
72 semicolon.
73
74 Even though it parses as a statement and not an expression, a try block
75 can still yield a value if it appears as the final statement in its
76 containing sub or do block. For example:
77
78 my $result = do {
79 try { attempt_func() }
80 catch ($e) { "Fallback Value" }
81 };
82
83 catch
84
85 ...
86 catch ($var) {
87 STATEMENTS...
88 }
89
90 A catch statement provides a block of code to the preceding try
91 statement that will be invoked in the case that the main block of code
92 throws an exception. A new lexical variable is created to store the
93 exception in.
94
95 Presence of this catch statement causes any exception thrown by the
96 preceding try block to be non-fatal to the surrounding code. If the
97 catch block wishes to optionally handle some exceptions but not others,
98 it can re-raise it (or another exception) by calling die in the usual
99 manner.
100
101 As with try, the body of a catch {} block may also contain a return
102 expression, which as before, has its usual meaning, causing the entire
103 containing function to return with the given value. The body may also
104 contain loop control expressions (redo, next or last) which also have
105 their usual effect.
106
107 finally
108
109 ...
110 finally {
111 STATEMENTS...
112 }
113
114 A finally statement provides an optional block of code to the preceding
115 try/catch pair which is executed afterwards, both in the case of a
116 normal execution or a thrown exception. This code block may be used to
117 provide whatever clean-up operations might be required by preceding
118 code.
119
120 Because it is executed during a stack cleanup operation, a finally {}
121 block may not cause the containing function to return, or to alter the
122 return value of it. It also cannot see the containing function's @_
123 arguments array (though as it is block scoped within the function, it
124 will continue to share any normal lexical variables declared up until
125 that point). It is protected from disturbing the value of $@. If the
126 finally {} block code throws an exception, this will be printed as a
127 warning and discarded, leaving $@ containing the original exception, if
128 one existed. =head1 COMPATIBILITY NOTES
129
130 This module may use either Syntax::Keyword::Try or the perl core try
131 feature to implement its syntax. While the two behave very similarly,
132 and both conform to the description given above, the following
133 differences should be noted.
134
135 * Visibility to caller()
136
137 The Syntax::Keyword::Try module implements try blocks by using eval
138 frames. As a result, they are visible to the caller() function and
139 hence to things like Carp::longmess when viewed as stack traces.
140
141 By comparison, core's feature 'try' creates a new kind of context
142 stack entry that is ignored by caller() and hence these blocks do not
143 show up in stack traces.
144
145 This should not matter to most use-cases - e.g. even Carp::croak will
146 be fine here. But if you are using caller() with calculated indexes
147 to inspect the state of callers to your code and there may be try
148 frames in the way, you will need to somehow account for the
149 difference in stack height.
150
151 * B::Deparse
152
153 The core feature 'try' is implemented by emitting real opcodes that
154 represent its behaviour, which is recognised by the version of
155 B::Deparse that ships with core perl. As a result, any code using
156 this implementation will deparse currently with tools like perl
157 -MO=Deparse ..., or others related to it such as coverage checkers.
158
159 By comparison, since Syntax::Keyword::Try uses OP_CUSTOM it is not
160 recognised by B::Deparse and so attempts to deparse this will result
161 in error messages like
162
163 unexpected OP_CUSTOM (catch) at ...
164
165 This is rather unavoidable due to the way that B::Deparse is
166 implemented and does not easily support custom operators.
167
168 See also https://rt.cpan.org/Ticket/Display.html?id=134812.
169
170 AUTHOR
171
172 Paul Evans <[email protected]>
173