This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #115066] Fix wrongly nested ‘use’ deparsing
[perl5.git] / lib / feature.pm
CommitLineData
69bcf1d3
FC
1# -*- buffer-read-only: t -*-
2# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
3# This file is built by regen/feature.pl.
4# Any changes made here will be lost!
5
0d863452
RH
6package feature;
7
d24d2f9e 8our $VERSION = '1.38';
0d863452 9
0bb01b05 10our %feature = (
2a4315f8 11 fc => 'feature_fc',
47e9d84a
EB
12 say => 'feature_say',
13 state => 'feature_state',
1863b879 14 switch => 'feature_switch',
7d789282 15 evalbytes => 'feature_evalbytes',
b9a36c95 16 postderef => 'feature_postderef',
3fff3427 17 array_base => 'feature_arybase',
30d9c59b 18 signatures => 'feature_signatures',
84ed0108 19 current_sub => 'feature___SUB__',
baabe3fb 20 refaliasing => 'feature_refaliasing',
ebd25686 21 lexical_subs => 'feature_lexsubs',
158becca 22 postderef_qq => 'feature_postderef_qq',
cda6b701 23 unicode_eval => 'feature_unieval',
47e9d84a 24 unicode_strings => 'feature_unicode',
bc9b29db
RH
25);
26
0d2bd2aa 27our %feature_bundle = (
69bcf1d3
FC
28 "5.10" => [qw(array_base say state switch)],
29 "5.11" => [qw(array_base say state switch unicode_strings)],
2a4315f8 30 "5.15" => [qw(current_sub evalbytes fc say state switch unicode_eval unicode_strings)],
baabe3fb 31 "all" => [qw(array_base current_sub evalbytes fc lexical_subs postderef postderef_qq refaliasing say signatures state switch unicode_eval unicode_strings)],
69bcf1d3 32 "default" => [qw(array_base)],
0d863452 33);
d052521a 34
88da30d7
FC
35$feature_bundle{"5.12"} = $feature_bundle{"5.11"};
36$feature_bundle{"5.13"} = $feature_bundle{"5.11"};
37$feature_bundle{"5.14"} = $feature_bundle{"5.11"};
38$feature_bundle{"5.16"} = $feature_bundle{"5.15"};
d6402ebe 39$feature_bundle{"5.17"} = $feature_bundle{"5.15"};
52fc5c56 40$feature_bundle{"5.18"} = $feature_bundle{"5.15"};
d09258e7
RS
41$feature_bundle{"5.19"} = $feature_bundle{"5.15"};
42$feature_bundle{"5.20"} = $feature_bundle{"5.15"};
b530a4ea
RS
43$feature_bundle{"5.21"} = $feature_bundle{"5.15"};
44$feature_bundle{"5.22"} = $feature_bundle{"5.15"};
88da30d7 45$feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
69bcf1d3 46
0bb01b05
FC
47our $hint_shift = 26;
48our $hint_mask = 0x1c000000;
49our @hint_bundles = qw( default 5.10 5.11 5.15 );
ada44f8c 50
69bcf1d3
FC
51# This gets set (for now) in $^H as well as in %^H,
52# for runtime speed of the uc/lc/ucfirst/lcfirst functions.
53# See HINT_UNI_8_BIT in perl.h.
54our $hint_uni8bit = 0x00000800;
7dfde25d 55
0d863452 56# TODO:
1c321dc6 57# - think about versioned features (use feature switch => 2)
0d863452
RH
58
59=head1 NAME
60
e1b711da 61feature - Perl pragma to enable new features
0d863452
RH
62
63=head1 SYNOPSIS
64
47e9d84a 65 use feature qw(say switch);
0d863452 66 given ($foo) {
0b25e784
DG
67 when (1) { say "\$foo == 1" }
68 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
69 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
70 when ($_ > 100) { say "\$foo > 100" }
71 default { say "None of the above" }
0d863452
RH
72 }
73
ec488c7f
RGS
74 use feature ':5.10'; # loads all features available in perl 5.10
75
0b25e784
DG
76 use v5.10; # implicitly loads :5.10 feature bundle
77
0d863452
RH
78=head1 DESCRIPTION
79
80It is usually impossible to add new syntax to Perl without breaking
b22bbcf0 81some existing programs. This pragma provides a way to minimize that
1863b879
RGS
82risk. New syntactic constructs, or new semantic meanings to older
83constructs, can be enabled by C<use feature 'foo'>, and will be parsed
b22bbcf0 84only when the appropriate feature pragma is in scope. (Nevertheless, the
4a904372
FC
85C<CORE::> prefix provides access to all Perl keywords, regardless of this
86pragma.)
0d863452 87
9eb27be9
RGS
88=head2 Lexical effect
89
90Like other pragmas (C<use strict>, for example), features have a lexical
301381dc 91effect. C<use feature qw(foo)> will only make the feature "foo" available
9eb27be9
RGS
92from that point to the end of the enclosing block.
93
94 {
95 use feature 'say';
96 say "say is available here";
97 }
98 print "But not here.\n";
99
5e36ed56
RGS
100=head2 C<no feature>
101
b22bbcf0 102Features can also be turned off by using C<no feature "foo">. This too
5e36ed56
RGS
103has lexical effect.
104
105 use feature 'say';
106 say "say is available here";
107 {
108 no feature 'say';
109 print "But not here.\n";
110 }
111 say "Yet it is here.";
112
39ec54a5
RS
113C<no feature> with no features specified will reset to the default group. To
114disable I<all> features (an unusual request!) use C<no feature ':all'>.
5e36ed56 115
0b25e784
DG
116=head1 AVAILABLE FEATURES
117
0d863452
RH
118=head2 The 'say' feature
119
0b25e784 120C<use feature 'say'> tells the compiler to enable the Perl 6 style
9eb27be9 121C<say> function.
0d863452
RH
122
123See L<perlfunc/say> for details.
124
0b25e784
DG
125This feature is available starting with Perl 5.10.
126
127=head2 The 'state' feature
712d05cf
RGS
128
129C<use feature 'state'> tells the compiler to enable C<state>
9eb27be9 130variables.
712d05cf 131
e60bcc8b
RGS
132See L<perlsub/"Persistent Private Variables"> for details.
133
0b25e784
DG
134This feature is available starting with Perl 5.10.
135
47e9d84a
EB
136=head2 The 'switch' feature
137
7caca87c
DB
138B<WARNING>: Because the L<smartmatch operator|perlop/"Smartmatch Operator"> is
139experimental, Perl will warn when you use this feature, unless you have
140explicitly disabled the warning:
141
142 no warnings "experimental::smartmatch";
143
47e9d84a
EB
144C<use feature 'switch'> tells the compiler to enable the Perl 6
145given/when construct.
146
48238296 147See L<perlsyn/"Switch Statements"> for details.
47e9d84a 148
0b25e784
DG
149This feature is available starting with Perl 5.10.
150
151=head2 The 'unicode_strings' feature
1863b879 152
850b7ec9 153C<use feature 'unicode_strings'> tells the compiler to use Unicode rules
20db7501
KW
154in all string operations executed within its scope (unless they are also
155within the scope of either C<use locale> or C<use bytes>). The same applies
156to all regular expressions compiled within the scope, even if executed outside
2269d15c
KW
157it. It does not change the internal representation of strings, but only how
158they are interpreted.
20db7501
KW
159
160C<no feature 'unicode_strings'> tells the compiler to use the traditional
850b7ec9 161Perl rules wherein the native character set rules is used unless it is
20db7501
KW
162clear to Perl that Unicode is desired. This can lead to some surprises
163when the behavior suddenly changes. (See
164L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are
165potentially using Unicode in your program, the
166C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
167
2e2b2571
KW
168This feature is available starting with Perl 5.12; was almost fully
169implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>.
1863b879 170
0b25e784 171=head2 The 'unicode_eval' and 'evalbytes' features
7289c5e6
FC
172
173Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a
174string, will evaluate it as a string of characters, ignoring any
175C<use utf8> declarations. C<use utf8> exists to declare the encoding of
176the script, which only makes sense for a stream of bytes, not a string of
177characters. Source filters are forbidden, as they also really only make
178sense on strings of bytes. Any attempt to activate a source filter will
179result in an error.
180
181The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates
182the argument passed to it as a string of bytes. It dies if the string
183contains any characters outside the 8-bit range. Source filters work
184within C<evalbytes>: they apply to the contents of the string being
185evaluated.
186
187Together, these two features are intended to replace the historical C<eval>
188function, which has (at least) two bugs in it, that cannot easily be fixed
189without breaking existing programs:
190
191=over
192
193=item *
194
195C<eval> behaves differently depending on the internal encoding of the
196string, sometimes treating its argument as a string of bytes, and sometimes
197as a string of characters.
198
199=item *
200
201Source filters activated within C<eval> leak out into whichever I<file>
202scope is currently being compiled. To give an example with the CPAN module
203L<Semi::Semicolons>:
204
205 BEGIN { eval "use Semi::Semicolons; # not filtered here " }
206 # filtered here!
207
208C<evalbytes> fixes that to work the way one would expect:
209
210 use feature "evalbytes";
211 BEGIN { evalbytes "use Semi::Semicolons; # filtered " }
212 # not filtered
213
214=back
215
216These two features are available starting with Perl 5.16.
217
84ed0108
FC
218=head2 The 'current_sub' feature
219
220This provides the C<__SUB__> token that returns a reference to the current
221subroutine or C<undef> outside of a subroutine.
222
223This feature is available starting with Perl 5.16.
224
01868d00
FC
225=head2 The 'array_base' feature
226
227This feature supports the legacy C<$[> variable. See L<perlvar/$[> and
228L<arybase>. It is on by default but disabled under C<use v5.16> (see
229L</IMPLICIT LOADING>, below).
230
231This feature is available under this name starting with Perl 5.16. In
232previous versions, it was simply on all the time, and this pragma knew
233nothing about it.
234
2a4315f8
BF
235=head2 The 'fc' feature
236
237C<use feature 'fc'> tells the compiler to enable the C<fc> function,
238which implements Unicode casefolding.
239
240See L<perlfunc/fc> for details.
241
242This feature is available from Perl 5.16 onwards.
243
ca40957e
FC
244=head2 The 'lexical_subs' feature
245
246B<WARNING>: This feature is still experimental and the implementation may
64fbf0dd
FC
247change in future versions of Perl. For this reason, Perl will
248warn when you use the feature, unless you have explicitly disabled the
ca40957e
FC
249warning:
250
f1d34ca8 251 no warnings "experimental::lexical_subs";
ca40957e
FC
252
253This enables declaration of subroutines via C<my sub foo>, C<state sub foo>
254and C<our sub foo> syntax. See L<perlsub/Lexical Subroutines> for details.
255
256This feature is available from Perl 5.18 onwards.
257
30d9c59b
Z
258=head2 The 'signatures' feature
259
260B<WARNING>: This feature is still experimental and the implementation may
261change in future versions of Perl. For this reason, Perl will
262warn when you use the feature, unless you have explicitly disabled the
263warning:
264
265 no warnings "experimental::signatures";
266
267This enables unpacking of subroutine arguments into lexical variables
268by syntax such as
269
270 sub foo ($left, $right) {
271 return $left + $right;
272 }
273
274See L<perlsub/Signatures> for details.
275
276This feature is available from Perl 5.20 onwards.
277
baabe3fb 278=head2 The 'refaliasing' feature
82848c10
FC
279
280B<WARNING>: This feature is still experimental and the implementation may
281change in future versions of Perl. For this reason, Perl will
282warn when you use the feature, unless you have explicitly disabled the
283warning:
284
baabe3fb 285 no warnings "experimental::refaliasing";
82848c10
FC
286
287This enables aliasing via assignment to references:
288
289 \$a = \$b; # $a and $b now point to the same scalar
290 \@a = \@b; # to the same array
291 \%a = \%b;
292 \&a = \&b;
293 foreach \%hash (@array_of_hash_refs) {
294 ...
295 }
296
297See L<perlref/Assigning to References> for details.
298
299This feature is available from Perl 5.22 onwards.
300
bc9b29db
RH
301=head1 FEATURE BUNDLES
302
0b25e784 303It's possible to load multiple features together, using
b22bbcf0 304a I<feature bundle>. The name of a feature bundle is prefixed with
0b25e784
DG
305a colon, to distinguish it from an actual feature.
306
307 use feature ":5.10";
308
309The following feature bundles are available:
310
311 bundle features included
312 --------- -----------------
01868d00 313 :default array_base
0b25e784 314
01868d00 315 :5.10 say state switch array_base
0b25e784 316
01868d00 317 :5.12 say state switch unicode_strings array_base
0b25e784 318
01868d00 319 :5.14 say state switch unicode_strings array_base
0b25e784
DG
320
321 :5.16 say state switch unicode_strings
2a4315f8 322 unicode_eval evalbytes current_sub fc
0b25e784 323
52fc5c56
FC
324 :5.18 say state switch unicode_strings
325 unicode_eval evalbytes current_sub fc
326
d09258e7
RS
327 :5.20 say state switch unicode_strings
328 unicode_eval evalbytes current_sub fc
329
b530a4ea
RS
330 :5.22 say state switch unicode_strings
331 unicode_eval evalbytes current_sub fc
332
01868d00
FC
333The C<:default> bundle represents the feature set that is enabled before
334any C<use feature> or C<no feature> declaration.
a3a91442
JV
335
336Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
b22bbcf0 337no effect. Feature bundles are guaranteed to be the same for all sub-versions.
bc9b29db 338
0b25e784
DG
339 use feature ":5.14.0"; # same as ":5.14"
340 use feature ":5.14.1"; # same as ":5.14"
a3a91442 341
7dfde25d
RGS
342=head1 IMPLICIT LOADING
343
0b25e784
DG
344Instead of loading feature bundles by name, it is easier to let Perl do
345implicit loading of a feature bundle for you.
346
347There are two ways to load the C<feature> pragma implicitly:
7dfde25d
RGS
348
349=over 4
350
351=item *
352
0b25e784
DG
353By using the C<-E> switch on the Perl command-line instead of C<-e>.
354That will enable the feature bundle for that version of Perl in the
355main compilation unit (that is, the one-liner that follows C<-E>).
7dfde25d
RGS
356
357=item *
358
0b25e784 359By explicitly requiring a minimum Perl version number for your program, with
b22bbcf0 360the C<use VERSION> construct. That is,
7dfde25d 361
0b25e784 362 use v5.10.0;
7dfde25d
RGS
363
364will do an implicit
365
39ec54a5 366 no feature ':all';
82cfb3a2 367 use feature ':5.10';
7dfde25d 368
b22bbcf0
FC
369and so on. Note how the trailing sub-version
370is automatically stripped from the
82cfb3a2 371version.
7dfde25d 372
8d115822
RB
373But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
374
375 use 5.010;
376
377with the same effect.
378
0b25e784 379If the required version is older than Perl 5.10, the ":default" feature
01868d00 380bundle is automatically loaded instead.
70397346 381
7dfde25d
RGS
382=back
383
0d863452
RH
384=cut
385
386sub import {
0d863452 387 my $class = shift;
36143a0c
NC
388
389 if (!@_) {
0b25e784 390 croak("No features specified");
0d863452 391 }
36143a0c 392
d3757264 393 __common(1, @_);
0d863452
RH
394}
395
396sub unimport {
397 my $class = shift;
398
39ec54a5 399 # A bare C<no feature> should reset to the default bundle
bc9b29db 400 if (!@_) {
39ec54a5
RS
401 $^H &= ~($hint_uni8bit|$hint_mask);
402 return;
bc9b29db
RH
403 }
404
d3757264
NC
405 __common(0, @_);
406}
407
408
409sub __common {
410 my $import = shift;
0c8d5017
NC
411 my $bundle_number = $^H & $hint_mask;
412 my $features = $bundle_number != $hint_mask
413 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
414 if ($features) {
da5b5421 415 # Features are enabled implicitly via bundle hints.
d9ee6ccb
NC
416 # Delete any keys that may be left over from last time.
417 delete @^H{ values(%feature) };
418 $^H |= $hint_mask;
419 for (@$features) {
420 $^H{$feature{$_}} = 1;
421 $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
422 }
da5b5421 423 }
bc9b29db 424 while (@_) {
0b25e784
DG
425 my $name = shift;
426 if (substr($name, 0, 1) eq ":") {
427 my $v = substr($name, 1);
428 if (!exists $feature_bundle{$v}) {
429 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
430 if (!exists $feature_bundle{$v}) {
431 unknown_feature_bundle(substr($name, 1));
432 }
433 }
434 unshift @_, @{$feature_bundle{$v}};
435 next;
436 }
36143a0c 437 if (!exists $feature{$name}) {
0b25e784
DG
438 unknown_feature($name);
439 }
d3757264
NC
440 if ($import) {
441 $^H{$feature{$name}} = 1;
442 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
443 } else {
0b25e784 444 delete $^H{$feature{$name}};
1863b879 445 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
0b25e784 446 }
0d863452 447 }
0d863452
RH
448}
449
b42943c4
RGS
450sub unknown_feature {
451 my $feature = shift;
452 croak(sprintf('Feature "%s" is not supported by Perl %vd',
0b25e784 453 $feature, $^V));
b42943c4
RGS
454}
455
456sub unknown_feature_bundle {
457 my $feature = shift;
458 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
0b25e784 459 $feature, $^V));
b42943c4
RGS
460}
461
462sub croak {
463 require Carp;
464 Carp::croak(@_);
465}
466
0d863452 4671;
69bcf1d3
FC
468
469# ex: set ro: