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 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.03;
7  
8 6 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use feature ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶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 =head2 catch
87  
88 ...
89 catch ($var) {
90 STATEMENTS...
91 }
92  
93 A C<catch> statement provides a block of code to the preceding C<try>
94 statement that will be invoked in the case that the main block of code throws
95 an exception. A new lexical variable is created to store the exception in.
96  
97 Presence of this C<catch> statement causes any exception thrown by the
98 preceding C<try> block to be non-fatal to the surrounding code. If the
99 C<catch> block wishes to optionally handle some exceptions but not others, it
100 can re-raise it (or another exception) by calling C<die> in the usual manner.
101  
102 As with C<try>, the body of a C<catch {}> block may also contain a C<return>
103 expression, which as before, has its usual meaning, causing the entire
104 containing function to return with the given value. The body may also contain
105 loop control expressions (C<redo>, C<next> or C<last>) which also have their
106 usual effect.
107  
108 =cut
109  
110 sub import
111 {
112 6 ▶ if( HAVE_FEATURE_TRY ) {
Subroutine import: called CC=1 SCAR=0.0
113 6 feature->import(qw( try ));
114 6 require warnings;
115 6 warnings->unimport(qw( experimental::try ));
116 }
117 else {
118 require Syntax::Keyword::Try;
119 Syntax::Keyword::Try->VERSION( '0.22' );
120 Syntax::Keyword::Try->import(qw( try -no_finally -require_var ));
121 }
122 }
123  
124 =head1 AUTHOR
125  
126 Paul Evans <leonerd@leonerd.org.uk>
127  
128 =cut
129  
130 6 0x55AA;
« Build ↑ Summary