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 -- leonerd@leonerd.org.uk
5              
6             package Feature::Compat::Try 0.03;
7              
8 6     6   362096 use v5.14;
  6         89  
9 6     6   37 use warnings;
  6         9  
  6         174  
10 6     6   31 use feature ();
  6         12  
  6         225  
11              
12 6     6   46 use constant HAVE_FEATURE_TRY => defined $feature::feature{try};
  6         32  
  6         1250  
13              
14             =head1 NAME
15              
16             C - make C 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 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 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 statement provides the main body of code that will be invoked, and
66             must be followed by a C statement.
67              
68             Execution of the C 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 block may contain a C 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 block, in which
75             circumstance only the C itself would return, not the entire function.
76              
77             The body of a C block may contain loop control expressions (C,
78             C, C) which will have their usual effect on any loops that the
79             C block is contained by.
80              
81             The parsing rules for the set of statements (the C block and its
82             associated C) 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 statement provides a block of code to the preceding C
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 statement causes any exception thrown by the
98             preceding C block to be non-fatal to the surrounding code. If the
99             C block wishes to optionally handle some exceptions but not others, it
100             can re-raise it (or another exception) by calling C in the usual manner.
101              
102             As with C, the body of a C block may also contain a C
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, C or C) which also have their
106             usual effect.
107              
108             =cut
109              
110             sub import
111             {
112 6     6   39 if( HAVE_FEATURE_TRY ) {
113             feature->import(qw( try ));
114             require warnings;
115             warnings->unimport(qw( experimental::try ));
116             }
117             else {
118 6         3133 require Syntax::Keyword::Try;
119 6         5654 Syntax::Keyword::Try->VERSION( '0.22' );
120 6         30 Syntax::Keyword::Try->import(qw( try -no_finally -require_var ));
121             }
122             }
123              
124             =head1 AUTHOR
125              
126             Paul Evans
127              
128             =cut
129              
130             0x55AA;