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-2023 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Feature::Compat::Class 0.05; |
7
|
|
|
|
|
|
|
|
8
|
12
|
|
|
12
|
|
798575
|
use v5.14; |
|
12
|
|
|
|
|
157
|
|
9
|
12
|
|
|
12
|
|
68
|
use warnings; |
|
12
|
|
|
|
|
30
|
|
|
12
|
|
|
|
|
336
|
|
10
|
12
|
|
|
12
|
|
67
|
use feature (); |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
561
|
|
11
|
|
|
|
|
|
|
|
12
|
12
|
|
|
12
|
|
93
|
use constant HAVE_FEATURE_CLASS => defined $feature::feature{class}; |
|
12
|
|
|
|
|
28
|
|
|
12
|
|
|
|
|
3260
|
|
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 |
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
|
12
|
|
|
12
|
|
83
|
if( HAVE_FEATURE_CLASS ) { |
73
|
|
|
|
|
|
|
feature->import(qw( class )); |
74
|
|
|
|
|
|
|
require warnings; |
75
|
|
|
|
|
|
|
warnings->unimport(qw( experimental::class )); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
12
|
|
|
|
|
7921
|
require Object::Pad; |
79
|
12
|
|
|
|
|
140627
|
Object::Pad->VERSION( '0.78' ); |
80
|
12
|
|
|
|
|
72
|
Object::Pad->import(qw( class method field ADJUST ), |
81
|
|
|
|
|
|
|
':experimental(init_expr)', |
82
|
|
|
|
|
|
|
':config(' . |
83
|
|
|
|
|
|
|
'always_strict only_class_attrs=isa only_field_attrs=param ' . |
84
|
|
|
|
|
|
|
'no_field_block no_adjust_attrs no_implicit_pragmata' . |
85
|
|
|
|
|
|
|
')', |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 KEYWORDS |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
The keywords provided by this module offer a subset of the abilities of those |
93
|
|
|
|
|
|
|
provided by C, restricted to specifically only what is commonly |
94
|
|
|
|
|
|
|
supported by the core syntax as well. In general, the reader should first |
95
|
|
|
|
|
|
|
consult the documentation for the corresponding C keyword, but |
96
|
|
|
|
|
|
|
the following notes may be of interest: |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 class |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
class NAME { ... } |
101
|
|
|
|
|
|
|
class NAME VERSION { ... } |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
class NAME; ... |
104
|
|
|
|
|
|
|
class NAME VERSION; ... |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
See also L. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
There is no ability to declare any roles with C<:does>. The legacy subkeywords |
109
|
|
|
|
|
|
|
for these are equally not supported. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
The C<:repr> attribute is also not supported; the default representation type |
112
|
|
|
|
|
|
|
will always be selected. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
The C<:strict(params)> attribute is not available, but all constructed classes |
115
|
|
|
|
|
|
|
will behave as if the attribute had been declared. Every generated constructor |
116
|
|
|
|
|
|
|
will check its parameters for key names left unhandled by C blocks, |
117
|
|
|
|
|
|
|
and throw an exception if any remain. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
The following class attributes are supported: |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head3 :isa |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
:isa(CLASS) |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
:isa(CLASS CLASSVER) |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
I |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Declares a superclass that this class extends. At most one superclass is |
130
|
|
|
|
|
|
|
supported. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
If the package providing the superclass does not exist, an attempt is made to |
133
|
|
|
|
|
|
|
load it by code equivalent to |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
require CLASS (); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
and thus it must either already exist, or be locatable via the usual C<@INC> |
138
|
|
|
|
|
|
|
mechanisms. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
An optional version check can also be supplied; it performs the equivalent of |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
BaseClass->VERSION( $ver ) |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Note that C blocks B implicitly enable the C and |
145
|
|
|
|
|
|
|
C pragmata; either when using the core feature or C. |
146
|
|
|
|
|
|
|
This is to avoid surprises when eventually switching to purely using the core |
147
|
|
|
|
|
|
|
perl feature, which will not do that. Remember however that a C |
148
|
|
|
|
|
|
|
of a version C or above will enable both these pragmata anyway, so that |
149
|
|
|
|
|
|
|
will be sufficient. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 method |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
method NAME { ... } |
154
|
|
|
|
|
|
|
method NAME; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
See also L. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Attributes are not supported, other than the usual ones provided by perl |
159
|
|
|
|
|
|
|
itself. Of these, only C<:lvalue> is particularly useful. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Lexical methods are not supported. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 field |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
field $NAME; |
166
|
|
|
|
|
|
|
field @NAME; |
167
|
|
|
|
|
|
|
field %NAME; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
field $NAME = EXPR; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
field $NAME :ATTRS... = EXPR; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
See also L. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Most field attributes are not supported. In particular, rather than using the |
176
|
|
|
|
|
|
|
accessor-generator attributes you will have to create accessor methods |
177
|
|
|
|
|
|
|
yourself; such as |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
field $var; |
180
|
|
|
|
|
|
|
method var { return $var; } |
181
|
|
|
|
|
|
|
method set_var ($new_var) { $var = $new_var; } |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
I fields of any type may take initialising expressions. |
184
|
|
|
|
|
|
|
Initialiser blocks are not supported. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
field $five = 5; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
The following field attributes are supported: |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head3 :param |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
field $var :param; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
field $var :param(name) |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
I |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Declares that the constructor will take a named parameter to set the value for |
199
|
|
|
|
|
|
|
this field in a new instance. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
field $var :param = EXPR; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Without a defaulting expression, the parameter is mandatory. When combined |
204
|
|
|
|
|
|
|
with a defaulting expression, the parameter is optional and the default will |
205
|
|
|
|
|
|
|
only apply if the named parameter was not passed to the constructor. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
field $var :param //= EXPR; |
208
|
|
|
|
|
|
|
field $var :param ||= EXPR; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
With both the C<:param> attribute and a defaulting expression, the operator |
211
|
|
|
|
|
|
|
can also be written as C/=> or C<||=>. In this case, the defaulting |
212
|
|
|
|
|
|
|
expression will be used even if the caller passed an undefined value (for |
213
|
|
|
|
|
|
|
C/=>) or a false value (for C<||=>). This simplifies many situations where |
214
|
|
|
|
|
|
|
C would not be a valid value for a field parameter. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
class C { |
217
|
|
|
|
|
|
|
field $timeout :param //= 20; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
C->new( timeout => $args{timeout} ); |
221
|
|
|
|
|
|
|
# default applies if %args has no 'timeout' key, or if its value is undef |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 ADJUST |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
ADJUST { ... } |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
See also L. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Attributes are not supported; in particular the C<:params> attribute of |
230
|
|
|
|
|
|
|
C v0.70. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 Other Keywords |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
The following other keywords provided by C are not supported here |
235
|
|
|
|
|
|
|
at all: |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
role |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
BUILD, ADJUSTPARAMS |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
has |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
requires |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=cut |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 COMPATIBILITY NOTES |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This module may use either L or the perl core C feature to |
250
|
|
|
|
|
|
|
implement its syntax. While the two behave very similarly and both conform to |
251
|
|
|
|
|
|
|
the description given above, the following differences should be noted. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=over 4 |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item Fields in later field expressions |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
The core perl C feature makes every field variable visible to the |
258
|
|
|
|
|
|
|
initialising expression of later fields. For example, |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
field $one = 1; |
261
|
|
|
|
|
|
|
field $two = $one + 1; |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
This is not currently supported by C. As a result, it is possible |
264
|
|
|
|
|
|
|
to write code that works fine with the core perl feature but older perls |
265
|
|
|
|
|
|
|
cannot support by using C. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=back |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=cut |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 AUTHOR |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Paul Evans |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=cut |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
0x55AA; |