File Coverage

blib/lib/Feature/Compat/Class.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, 2022 -- leonerd@leonerd.org.uk
5              
6             package Feature::Compat::Class 0.04;
7              
8 11     11   703083 use v5.14;
  11         148  
9 11     11   62 use warnings;
  11         20  
  11         294  
10 11     11   58 use feature ();
  11         23  
  11         506  
11              
12 11     11   63 use constant HAVE_FEATURE_CLASS => defined $feature::feature{class};
  11         39  
  11         2707  
13              
14             =head1 NAME
15              
16             C - make C syntax available
17              
18             =head1 SYNOPSIS
19              
20             use Feature::Compat::Class;
21              
22             class Point {
23             field $x :param = 0;
24             field $y :param = 0;
25              
26             method move_to ($new_x, $new_y) {
27             $x = $new_x;
28             $y = $new_y;
29             }
30              
31             method describe {
32             say "A point at ($x, $y)";
33             }
34             }
35              
36             Point->new(x => 5, y => 10)->describe;
37              
38             =head1 DESCRIPTION
39              
40             This module provides the new C keyword and related others (C,
41             C and C) in a forward-compatible way.
42              
43             There is a branch of Perl development source code which provides this syntax,
44             under the C named feature. If all goes well, this will become available
45             in a stable release in due course. On such perls that contain the feature,
46             this module simple enables it.
47              
48             On older versions of perl before such syntax is availble in core, it is
49             currently provided instead using the L module, imported with a
50             special set of options to configure it to only recognise the same syntax as
51             the core perl feature, thus ensuring any code using it will still continue to
52             function on that newer perl.
53              
54             =head2 Perl Branch with C
55              
56             At time of writing, the C syntax is not part of mainline
57             perl source but is available in a branch. That branch currently resides at
58             L. It is intended this
59             will be migrated to the main C repository ahead of actually being merged
60             once development has progressed further.
61              
62             This module is a work-in-progress, because the underlying C
63             branch is too. Many of the limitations and inabilities listed below are a
64             result of the early-access nature of this branch, and are expected to be
65             lifted as work progresses towards a more featureful and complete
66             implementation.
67              
68             =cut
69              
70             sub import
71             {
72 11     11   72 if( HAVE_FEATURE_CLASS ) {
73             feature->import(qw( class ));
74             require warnings;
75             warnings->unimport(qw( experimental::class ));
76             }
77             else {
78 11         7041 require Object::Pad;
79 11         125557 Object::Pad->VERSION( '0.75' );
80 11         56 Object::Pad->import(qw( class method field ADJUST ),
81             ':experimental(init_expr)',
82             ':config(always_strict only_class_attrs=isa only_field_attrs=param no_field_block no_adjust_attrs)',
83             );
84             }
85             }
86              
87             =head1 KEYWORDS
88              
89             The keywords provided by this module offer a subset of the abilities of those
90             provided by C, restricted to specifically only what is commonly
91             supported by the core syntax as well. In general, the reader should first
92             consult the documentation for the corresponding C keyword, but
93             the following notes may be of interest:
94              
95             =head2 class
96              
97             class NAME { ... }
98             class NAME VERSION { ... }
99              
100             class NAME; ...
101             class NAME VERSION; ...
102              
103             See also L.
104              
105             There is no ability to declare any roles with C<:does>. The legacy subkeywords
106             for these are equally not supported.
107              
108             The C<:repr> attribute is also not supported; the default representation type
109             will always be selected.
110              
111             The C<:strict(params)> attribute is not available, but all constructed classes
112             will behave as if the attribute had been declared. Every generated constructor
113             will check its parameters for key names left unhandled by C blocks,
114             and throw an exception if any remain.
115              
116             The following class attributes are supported:
117              
118             =head3 :isa
119              
120             :isa(CLASS)
121              
122             :isa(CLASS CLASSVER)
123              
124             I
125              
126             Declares a superclass that this class extends. At most one superclass is
127             supported.
128              
129             If the package providing the superclass does not exist, an attempt is made to
130             load it by code equivalent to
131              
132             require CLASS ();
133              
134             and thus it must either already exist, or be locatable via the usual C<@INC>
135             mechanisms.
136              
137             An optional version check can also be supplied; it performs the equivalent of
138              
139             BaseClass->VERSION( $ver )
140              
141             =head2 method
142              
143             method NAME { ... }
144             method NAME;
145              
146             See also L.
147              
148             Attributes are not supported, other than the usual ones provided by perl
149             itself. Of these, only C<:lvalue> is particularly useful.
150              
151             Lexical methods are not supported.
152              
153             =head2 field
154              
155             field $NAME;
156             field @NAME;
157             field %NAME;
158              
159             field $NAME = EXPR;
160              
161             field $NAME :ATTRS... = EXPR;
162              
163             See also L.
164              
165             Most field attributes are not supported. In particular, rather than using the
166             accessor-generator attributes you will have to create accessor methods
167             yourself; such as
168              
169             field $var;
170             method var { return $var; }
171             method set_var ($new_var) { $var = $new_var; }
172              
173             I fields of any type may take initialising expressions.
174             Initialiser blocks are not supported.
175              
176             field $five = 5;
177              
178             The following field attributes are supported:
179              
180             =head3 :param
181              
182             field $var :param;
183              
184             field $var :param(name)
185              
186             I
187              
188             Declares that the constructor will take a named parameter to set the value for
189             this field in a new instance.
190              
191             field $var :param = EXPR;
192              
193             Without a defaulting expression, the parameter is mandatory. When combined
194             with a defaulting expression, the parameter is optional and the default will
195             only apply if the named parameter was not passed to the constructor.
196              
197             field $var :param //= EXPR;
198             field $var :param ||= EXPR;
199              
200             With both the C<:param> attribute and a defaulting expression, the operator
201             can also be written as C or C<||=>. In this case, the defaulting
202             expression will be used even if the caller passed an undefined value (for
203             C) or a false value (for C<||=>). This simplifies many situations where
204             C would not be a valid value for a field parameter.
205              
206             class C {
207             field $timeout :param //= 20;
208             }
209              
210             C->new( timeout => $args{timeout} );
211             # default applies if %args has no 'timeout' key, or if its value is undef
212              
213             =head2 ADJUST
214              
215             ADJUST { ... }
216              
217             See also L.
218              
219             Attributes are not supported; in particular the C<:params> attribute of
220             C v0.70.
221              
222             =head2 Other Keywords
223              
224             The following other keywords provided by C are not supported here
225             at all:
226              
227             role
228              
229             BUILD, ADJUSTPARAMS
230              
231             has
232              
233             requires
234              
235             =cut
236              
237             =head1 COMPATIBILITY NOTES
238              
239             This module may use either L or the perl core C feature to
240             implement its syntax. While the two behave very similarly and both conform to
241             the description given above, the following differences should be noted.
242              
243             =over 4
244              
245             =item Fields in later field expressions
246              
247             The core perl C feature makes every field variable visible to the
248             initialising expression of later fields. For example,
249              
250             field $one = 1;
251             field $two = $one + 1;
252              
253             This is not currently supported by C. As a result, it is possible
254             to write code that works fine with the core perl feature but older perls
255             cannot support by using C.
256              
257             =back
258              
259             =cut
260              
261             =head1 AUTHOR
262              
263             Paul Evans
264              
265             =cut
266              
267             0x55AA;