File Coverage

blib/lib/Feature/Compat/Try.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


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