blib/lib/Feature/Compat/Try.pm

filter: Statementstmt 100% 16 / 16 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 21 / 21 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary
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 -- leonerd@leonerd.org.uk
5  
6 package Feature::Compat::Try 0.04;
7  
8 8 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use feature ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use constant HAVE_FEATURE_TRY => defined $feature::feature{try};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 C<Feature::Compat::Try> - make C<try/catch> syntax available
17  
18 =head1 SYNOPSIS
19  
20 use Feature::Compat::Try;
21  
22 sub foo
23 {
24 try {
25 attempt_a_thing();
26 return "success";
27 }
28 catch ($e) {
29 warn "It failed - $e";
30 return "failure";
31 }
32 }
33  
34 =head1 DESCRIPTION
35  
36 This module is written in preparation for when perl will gain true native
37 syntax support for C<try/catch> control flow.
38  
39 Perl added such syntax in the development version 5.33.7, which is enabled
40 by
41  
42 use feature 'try';
43  
44 On that version of perl or later, this module simply enables the core feature
45 equivalent to using it directly. On such perls, this module will install with
46 no non-core dependencies, and requires no C compiler.
47  
48 On older versions of perl before such syntax is available, it is currently
49 provided instead using the L<Syntax::Keyword::Try> module, imported with a
50 special set of options to configure it to recognise exactly and only the same
51 syntax as the core perl feature, thus ensuring that any code using it will
52 still continue to function on that newer perl.
53  
54 =cut
55  
56 =head1 KEYWORDS
57  
58 =head2 try
59  
60 try {
61 STATEMENTS...
62 }
63 ...
64  
65 A C<try> statement provides the main body of code that will be invoked, and
66 must be followed by a C<catch> statement.
67  
68 Execution of the C<try> statement itself begins from the block given to the
69 statement and continues until either it throws an exception, or completes
70 successfully by reaching the end of the block.
71  
72 The body of a C<try {}> block may contain a C<return> expression. If executed,
73 such an expression will cause the entire containing function to return with
74 the value provided. This is different from a plain C<eval {}> block, in which
75 circumstance only the C<eval> itself would return, not the entire function.
76  
77 The body of a C<try {}> block may contain loop control expressions (C<redo>,
78 C<next>, C<last>) which will have their usual effect on any loops that the
79 C<try {}> block is contained by.
80  
81 The parsing rules for the set of statements (the C<try> block and its
82 associated C<catch>) are such that they are parsed as a self-contained
83 statement. Because of this, there is no need to end with a terminating
84 semicolon.
85  
86 Even though it parses as a statement and not an expression, a C<try> block can
87 still yield a value if it appears as the final statement in its containing
88 C<sub> or C<do> block. For example:
89  
90 my $result = do {
91 try { attempt_func() }
92 catch ($e) { "Fallback Value" }
93 };
94  
95 =head2 catch
96  
97 ...
98 catch ($var) {
99 STATEMENTS...
100 }
101  
102 A C<catch> statement provides a block of code to the preceding C<try>
103 statement that will be invoked in the case that the main block of code throws
104 an exception. A new lexical variable is created to store the exception in.
105  
106 Presence of this C<catch> statement causes any exception thrown by the
107 preceding C<try> block to be non-fatal to the surrounding code. If the
108 C<catch> block wishes to optionally handle some exceptions but not others, it
109 can re-raise it (or another exception) by calling C<die> in the usual manner.
110  
111 As with C<try>, the body of a C<catch {}> block may also contain a C<return>
112 expression, which as before, has its usual meaning, causing the entire
113 containing function to return with the given value. The body may also contain
114 loop control expressions (C<redo>, C<next> or C<last>) which also have their
115 usual effect.
116  
117 =cut
118  
119 sub import
120 {
121 8 ▶ if( HAVE_FEATURE_TRY ) {
Subroutine import: called CC=1 SCAR=0.0
122 8 feature->import(qw( try ));
123 8 require warnings;
124 8 warnings->unimport(qw( experimental::try ));
125 }
126 else {
127 require Syntax::Keyword::Try;
128 Syntax::Keyword::Try->VERSION( '0.22' );
129 Syntax::Keyword::Try->import(qw( try -no_finally -require_var ));
130 }
131 }
132  
133 =head1 COMPATIBILITY NOTES
134  
135 This module may use either L<Syntax::Keyword::Try> or the perl core C<try>
136 feature to implement its syntax. While the two behave very similarly, and both
137 conform to the description given above, the following differences should be
138 noted.
139  
140 =over 4
141  
142 =item * Visibility to C<caller()>
143  
144 The C<Syntax::Keyword::Try> module implements C<try> blocks by using C<eval>
145 frames. As a result, they are visible to the C<caller()> function and hence to
146 things like C<Carp::longmess> when viewed as stack traces.
147  
148 By comparison, core's C<feature 'try'> creates a new kind of context stack
149 entry that is ignored by C<caller()> and hence these blocks do not show up in
150 stack traces.
151  
152 This should not matter to most use-cases - e.g. even C<Carp::croak> will be
153 fine here. But if you are using C<caller()> with calculated indexes to inspect
154 the state of callers to your code and there may be C<try> frames in the way,
155 you will need to somehow account for the difference in stack height.
156  
157 =back
158  
159 =cut
160  
161 =head1 AUTHOR
162  
163 Paul Evans <leonerd@leonerd.org.uk>
164  
165 =cut
166  
167 8 0x55AA;
« Build ↑ Summary