blib/lib/Feature/Compat/Defer.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
↑ 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::Defer 0.02;
7  
8 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use feature ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use constant HAVE_FEATURE_DEFER => defined $feature::feature{defer};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 C<Feature::Compat::Defer> - make C<defer> syntax available
17  
18 =head1 SYNOPSIS
19  
20 use Feature::Compat::Defer;
21  
22 {
23 my $dbh = DBI->connect( ... ) or die "Cannot connect";
24 defer { $dbh->disconnect; }
25  
26 my $sth = $dbh->prepare( ... ) or die "Cannot prepare";
27 defer { $sth->finish; }
28  
29 ...
30 }
31  
32 =head1 DESCRIPTION
33  
34 This module provides a new syntax keyword, C<defer>, in a forward-compatible
35 way.
36  
37 The latest perl development source provides a C<defer> block syntax, under
38 the C<defer> named feature. If all goes well, this will become available at
39 development version 5.35.4, and included in the 5.36 release. On such perls,
40 this module simply enables that feature.
41  
42 On older versions of perl before such syntax is available. this module will
43 instead depend on and use L<Syntax::Keyword::Defer> to provide it.
44  
45 =cut
46  
47 =head1 KEYWORDS
48  
49 =head2 defer
50  
51 defer {
52 STATEMENTS...
53 }
54  
55 The C<defer> keyword introduces a block which runs its code body at the time
56 that its immediately surrounding code block finishes.
57  
58 When the C<defer> statement is encountered, the body of the code block is
59 pushed to a queue of pending operations, which is then flushed when the
60 surrounding block finishes for any reason - either by implicit fallthrough,
61 or explicit termination by C<return>, C<die> or any of the loop control
62 statements C<next>, C<last> or C<redo>.
63  
64 For more information, see additionally the documentation in
65 L<Syntax::Keyword::Defer/defer> and, on a recent enough perl,
66 L<perlsyn/"defer blocks">.
67  
68 =cut
69  
70 sub import
71 3 ▶{
Subroutine import: called CC=1 SCAR=0.0
72 3 if( HAVE_FEATURE_DEFER ) {
73 3 feature->import(qw( defer ));
74 3 require warnings;
75 3 warnings->unimport(qw( experimental::defer ));
76 }
77 else {
78 require Syntax::Keyword::Defer;
79 Syntax::Keyword::Defer->VERSION( '0.06' );
80 Syntax::Keyword::Defer->import(qw( defer ));
81 }
82 }
83  
84 =head1 COMPATIBILITY NOTES
85  
86 This module may use either L<Syntax::Keyword::Defer> or the perl core C<defer>
87 feature to implement its syntax. While the two behave very similarly, and both
88 conform to the description given above, the following differences should be
89 noted.
90  
91 =over 4
92  
93 =item * Double Exceptions
94  
95 Because C<defer> blocks will run during stack unwind because of exception
96 propagation it is possible to encounter a second exception within the block,
97 thus having two "in flight" at once. Neither C<Syntax::Keyword::Defer> nor
98 core's C<defer> feature currently guarantees what exception will be seen by
99 the caller in such a situation, other than that some kind of exception will
100 definitely happen.
101  
102 In particular, you should not rely on definitely receiving either the first,
103 or the final exception, in this situation.
104  
105 =item * Fragile Against Erroneous Control Flow
106  
107 The core C<defer> feature tries hard to forbid various kinds of problematic
108 control that jumps into or out of C<defer> blocks. In particular, things like
109 using C<last> to jump out of a control loop that is outside the C<defer> block
110 are banned.
111  
112 By comparison, there is less that C<Syntax::Keyword::Defer> can do about this
113 situation because it does not have access to as many parser and compiler
114 tricks as the core implementation. Therefore, there are some situations that
115 the core feature can prohibit statically, that C<Syntax::Keyword::Defer> can
116 only detect at runtime - if at all. There may be odd cases where prohibited
117 behaviour performs differently between the two implementations.
118  
119 As long as you don't do anything "weird" like using loop controls or C<goto>
120 to abuse the flow of control into or out of a C<defer> block, this should not
121 cause a problem.
122  
123 =back
124  
125 =cut
126  
127 =head1 AUTHOR
128  
129 Paul Evans <leonerd@leonerd.org.uk>
130  
131 =cut
132  
133 3 0x55AA;
↑ Summary