This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix Module::CoreList versions
[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
affe54fa 8our $VERSION = '1.52';
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',
cec892e7 15 bitwise => 'feature_bitwise',
7d789282 16 evalbytes => 'feature_evalbytes',
3fff3427 17 array_base => 'feature_arybase',
30d9c59b 18 signatures => 'feature_signatures',
84ed0108 19 current_sub => 'feature___SUB__',
baabe3fb 20 refaliasing => 'feature_refaliasing',
158becca 21 postderef_qq => 'feature_postderef_qq',
cda6b701 22 unicode_eval => 'feature_unieval',
82d83da3 23 declared_refs => 'feature_myref',
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)],
1c2511e0 31 "5.23" => [qw(current_sub evalbytes fc postderef_qq say state switch unicode_eval unicode_strings)],
401d2aaa 32 "5.27" => [qw(bitwise current_sub evalbytes fc postderef_qq say state switch unicode_eval unicode_strings)],
82d83da3 33 "all" => [qw(array_base bitwise current_sub declared_refs evalbytes fc postderef_qq refaliasing say signatures state switch unicode_eval unicode_strings)],
69bcf1d3 34 "default" => [qw(array_base)],
0d863452 35);
d052521a 36
88da30d7
FC
37$feature_bundle{"5.12"} = $feature_bundle{"5.11"};
38$feature_bundle{"5.13"} = $feature_bundle{"5.11"};
39$feature_bundle{"5.14"} = $feature_bundle{"5.11"};
40$feature_bundle{"5.16"} = $feature_bundle{"5.15"};
d6402ebe 41$feature_bundle{"5.17"} = $feature_bundle{"5.15"};
52fc5c56 42$feature_bundle{"5.18"} = $feature_bundle{"5.15"};
d09258e7
RS
43$feature_bundle{"5.19"} = $feature_bundle{"5.15"};
44$feature_bundle{"5.20"} = $feature_bundle{"5.15"};
b530a4ea
RS
45$feature_bundle{"5.21"} = $feature_bundle{"5.15"};
46$feature_bundle{"5.22"} = $feature_bundle{"5.15"};
2ad792cd 47$feature_bundle{"5.24"} = $feature_bundle{"5.23"};
2c5484a6
RS
48$feature_bundle{"5.25"} = $feature_bundle{"5.23"};
49$feature_bundle{"5.26"} = $feature_bundle{"5.23"};
401d2aaa 50$feature_bundle{"5.28"} = $feature_bundle{"5.27"};
88da30d7 51$feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
db629560
FC
52my %noops = (
53 postderef => 1,
54 lexical_subs => 1,
55);
69bcf1d3 56
0bb01b05
FC
57our $hint_shift = 26;
58our $hint_mask = 0x1c000000;
401d2aaa 59our @hint_bundles = qw( default 5.10 5.11 5.15 5.23 5.27 );
ada44f8c 60
69bcf1d3
FC
61# This gets set (for now) in $^H as well as in %^H,
62# for runtime speed of the uc/lc/ucfirst/lcfirst functions.
63# See HINT_UNI_8_BIT in perl.h.
64our $hint_uni8bit = 0x00000800;
7dfde25d 65
0d863452 66# TODO:
1c321dc6 67# - think about versioned features (use feature switch => 2)
0d863452
RH
68
69=head1 NAME
70
e1b711da 71feature - Perl pragma to enable new features
0d863452
RH
72
73=head1 SYNOPSIS
74
47e9d84a 75 use feature qw(say switch);
0d863452 76 given ($foo) {
0b25e784
DG
77 when (1) { say "\$foo == 1" }
78 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
79 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
80 when ($_ > 100) { say "\$foo > 100" }
81 default { say "None of the above" }
0d863452
RH
82 }
83
ec488c7f
RGS
84 use feature ':5.10'; # loads all features available in perl 5.10
85
0b25e784
DG
86 use v5.10; # implicitly loads :5.10 feature bundle
87
0d863452
RH
88=head1 DESCRIPTION
89
90It is usually impossible to add new syntax to Perl without breaking
b22bbcf0 91some existing programs. This pragma provides a way to minimize that
1863b879
RGS
92risk. New syntactic constructs, or new semantic meanings to older
93constructs, can be enabled by C<use feature 'foo'>, and will be parsed
b22bbcf0 94only when the appropriate feature pragma is in scope. (Nevertheless, the
4a904372
FC
95C<CORE::> prefix provides access to all Perl keywords, regardless of this
96pragma.)
0d863452 97
9eb27be9
RGS
98=head2 Lexical effect
99
100Like other pragmas (C<use strict>, for example), features have a lexical
301381dc 101effect. C<use feature qw(foo)> will only make the feature "foo" available
9eb27be9
RGS
102from that point to the end of the enclosing block.
103
104 {
105 use feature 'say';
106 say "say is available here";
107 }
108 print "But not here.\n";
109
5e36ed56
RGS
110=head2 C<no feature>
111
b22bbcf0 112Features can also be turned off by using C<no feature "foo">. This too
5e36ed56
RGS
113has lexical effect.
114
115 use feature 'say';
116 say "say is available here";
117 {
118 no feature 'say';
119 print "But not here.\n";
120 }
121 say "Yet it is here.";
122
39ec54a5
RS
123C<no feature> with no features specified will reset to the default group. To
124disable I<all> features (an unusual request!) use C<no feature ':all'>.
5e36ed56 125
0b25e784
DG
126=head1 AVAILABLE FEATURES
127
0d863452
RH
128=head2 The 'say' feature
129
0b25e784 130C<use feature 'say'> tells the compiler to enable the Perl 6 style
9eb27be9 131C<say> function.
0d863452
RH
132
133See L<perlfunc/say> for details.
134
0b25e784
DG
135This feature is available starting with Perl 5.10.
136
137=head2 The 'state' feature
712d05cf
RGS
138
139C<use feature 'state'> tells the compiler to enable C<state>
9eb27be9 140variables.
712d05cf 141
e60bcc8b
RGS
142See L<perlsub/"Persistent Private Variables"> for details.
143
0b25e784
DG
144This feature is available starting with Perl 5.10.
145
47e9d84a
EB
146=head2 The 'switch' feature
147
7caca87c
DB
148B<WARNING>: Because the L<smartmatch operator|perlop/"Smartmatch Operator"> is
149experimental, Perl will warn when you use this feature, unless you have
150explicitly disabled the warning:
151
152 no warnings "experimental::smartmatch";
153
7896dde7 154C<use feature 'switch'> tells the compiler to enable the Perl 6
47e9d84a
EB
155given/when construct.
156
48238296 157See L<perlsyn/"Switch Statements"> for details.
47e9d84a 158
0b25e784
DG
159This feature is available starting with Perl 5.10.
160
161=head2 The 'unicode_strings' feature
1863b879 162
850b7ec9 163C<use feature 'unicode_strings'> tells the compiler to use Unicode rules
20db7501
KW
164in all string operations executed within its scope (unless they are also
165within the scope of either C<use locale> or C<use bytes>). The same applies
166to all regular expressions compiled within the scope, even if executed outside
2269d15c
KW
167it. It does not change the internal representation of strings, but only how
168they are interpreted.
20db7501
KW
169
170C<no feature 'unicode_strings'> tells the compiler to use the traditional
850b7ec9 171Perl rules wherein the native character set rules is used unless it is
20db7501
KW
172clear to Perl that Unicode is desired. This can lead to some surprises
173when the behavior suddenly changes. (See
174L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are
175potentially using Unicode in your program, the
176C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
177
2e2b2571 178This feature is available starting with Perl 5.12; was almost fully
d6c970c7 179implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>;
20ae58f7
AC
180was extended further in Perl 5.26 to cover L<the range
181operator|perlop/Range Operators>; and was extended again in Perl 5.28 to
182cover L<special-cased whitespace splitting|perlfunc/split>.
1863b879 183
0b25e784 184=head2 The 'unicode_eval' and 'evalbytes' features
7289c5e6 185
9891e9b7
KW
186Together, these two features are intended to replace the legacy string
187C<eval> function, which behaves problematically in some instances. They are
188available starting with Perl 5.16, and are enabled by default by a
189S<C<use 5.16>> or higher declaration.
190
191C<unicode_eval> changes the behavior of plain string C<eval> to work more
192consistently, especially in the Unicode world. Certain (mis)behaviors
193couldn't be changed without breaking some things that had come to rely on
194them, so the feature can be enabled and disabled. Details are at
195L<perlfunc/Under the "unicode_eval" feature>.
196
197C<evalbytes> is like string C<eval>, but operating on a byte stream that is
198not UTF-8 encoded. Details are at L<perlfunc/evalbytes EXPR>. Without a
199S<C<use feature 'evalbytes'>> nor a S<C<use v5.16>> (or higher) declaration in
200the current scope, you can still access it by instead writing
201C<CORE::evalbytes>.
7289c5e6 202
84ed0108
FC
203=head2 The 'current_sub' feature
204
205This provides the C<__SUB__> token that returns a reference to the current
206subroutine or C<undef> outside of a subroutine.
207
208This feature is available starting with Perl 5.16.
209
01868d00
FC
210=head2 The 'array_base' feature
211
212This feature supports the legacy C<$[> variable. See L<perlvar/$[> and
213L<arybase>. It is on by default but disabled under C<use v5.16> (see
214L</IMPLICIT LOADING>, below).
215
216This feature is available under this name starting with Perl 5.16. In
217previous versions, it was simply on all the time, and this pragma knew
218nothing about it.
219
2a4315f8
BF
220=head2 The 'fc' feature
221
222C<use feature 'fc'> tells the compiler to enable the C<fc> function,
223which implements Unicode casefolding.
224
225See L<perlfunc/fc> for details.
226
227This feature is available from Perl 5.16 onwards.
228
ca40957e
FC
229=head2 The 'lexical_subs' feature
230
8f7d85af
FC
231In Perl versions prior to 5.26, this feature enabled
232declaration of subroutines via C<my sub foo>, C<state sub foo>
233and C<our sub foo> syntax. See L<perlsub/Lexical Subroutines> for details.
ca40957e 234
8f7d85af
FC
235This feature is available from Perl 5.18 onwards. From Perl 5.18 to 5.24,
236it was classed as experimental, and Perl emitted a warning for its
237usage, except when explicitly disabled:
ca40957e 238
8f7d85af 239 no warnings "experimental::lexical_subs";
ca40957e 240
8f7d85af
FC
241As of Perl 5.26, use of this feature no longer triggers a warning, though
242the C<experimental::lexical_subs> warning category still exists (for
243compatibility with code that disables it). In addition, this syntax is
244not only no longer experimental, but it is enabled for all Perl code,
245regardless of what feature declarations are in scope.
ca40957e 246
f86d720e
RS
247=head2 The 'postderef' and 'postderef_qq' features
248
1c2511e0
AC
249The 'postderef_qq' feature extends the applicability of L<postfix
250dereference syntax|perlref/Postfix Dereference Syntax> so that postfix array
251and scalar dereference are available in double-quotish interpolations. For
252example, it makes the following two statements equivalent:
f86d720e 253
1c2511e0
AC
254 my $s = "[@{ $h->{a} }]";
255 my $s = "[$h->{a}->@*]";
f86d720e 256
1c2511e0
AC
257This feature is available from Perl 5.20 onwards. In Perl 5.20 and 5.22, it
258was classed as experimental, and Perl emitted a warning for its
2ad792cd
AC
259usage, except when explicitly disabled:
260
261 no warnings "experimental::postderef";
262
1c2511e0 263As of Perl 5.24, use of this feature no longer triggers a warning, though
2ad792cd
AC
264the C<experimental::postderef> warning category still exists (for
265compatibility with code that disables it).
f86d720e 266
1c2511e0
AC
267The 'postderef' feature was used in Perl 5.20 and Perl 5.22 to enable
268postfix dereference syntax outside double-quotish interpolations. In those
269versions, using it triggered the C<experimental::postderef> warning in the
270same way as the 'postderef_qq' feature did. As of Perl 5.24, this syntax is
271not only no longer experimental, but it is enabled for all Perl code,
272regardless of what feature declarations are in scope.
273
30d9c59b
Z
274=head2 The 'signatures' feature
275
276B<WARNING>: This feature is still experimental and the implementation may
277change in future versions of Perl. For this reason, Perl will
278warn when you use the feature, unless you have explicitly disabled the
279warning:
280
281 no warnings "experimental::signatures";
282
283This enables unpacking of subroutine arguments into lexical variables
284by syntax such as
285
286 sub foo ($left, $right) {
287 return $left + $right;
288 }
289
290See L<perlsub/Signatures> for details.
291
292This feature is available from Perl 5.20 onwards.
293
baabe3fb 294=head2 The 'refaliasing' feature
82848c10
FC
295
296B<WARNING>: This feature is still experimental and the implementation may
297change in future versions of Perl. For this reason, Perl will
298warn when you use the feature, unless you have explicitly disabled the
299warning:
300
baabe3fb 301 no warnings "experimental::refaliasing";
82848c10
FC
302
303This enables aliasing via assignment to references:
304
305 \$a = \$b; # $a and $b now point to the same scalar
306 \@a = \@b; # to the same array
307 \%a = \%b;
308 \&a = \&b;
309 foreach \%hash (@array_of_hash_refs) {
310 ...
311 }
312
313See L<perlref/Assigning to References> for details.
314
315This feature is available from Perl 5.22 onwards.
316
70ea8edf
FC
317=head2 The 'bitwise' feature
318
70ea8edf
FC
319This makes the four standard bitwise operators (C<& | ^ ~>) treat their
320operands consistently as numbers, and introduces four new dotted operators
321(C<&. |. ^. ~.>) that treat their operands consistently as strings. The
322same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).
323
324See L<perlop/Bitwise String Operators> for details.
325
193789ac
FC
326This feature is available from Perl 5.22 onwards. Starting in Perl 5.28,
327C<use v5.28> will enable the feature. Before 5.28, it was still
328experimental and would emit a warning in the "experimental::bitwise"
329category.
70ea8edf 330
5c703779
FC
331=head2 The 'declared_refs' feature
332
333B<WARNING>: This feature is still experimental and the implementation may
334change in future versions of Perl. For this reason, Perl will
335warn when you use the feature, unless you have explicitly disabled the
336warning:
337
338 no warnings "experimental::declared_refs";
339
340This allows a reference to a variable to be declared with C<my>, C<state>,
341our C<our>, or localized with C<local>. It is intended mainly for use in
342conjunction with the "refaliasing" feature. See L<perlref/Declaring a
343Reference to a Variable> for examples.
344
345This feature is available from Perl 5.26 onwards.
346
bc9b29db
RH
347=head1 FEATURE BUNDLES
348
0b25e784 349It's possible to load multiple features together, using
b22bbcf0 350a I<feature bundle>. The name of a feature bundle is prefixed with
0b25e784
DG
351a colon, to distinguish it from an actual feature.
352
353 use feature ":5.10";
354
355The following feature bundles are available:
356
357 bundle features included
358 --------- -----------------
01868d00 359 :default array_base
0b25e784 360
01868d00 361 :5.10 say state switch array_base
0b25e784 362
01868d00 363 :5.12 say state switch unicode_strings array_base
0b25e784 364
01868d00 365 :5.14 say state switch unicode_strings array_base
0b25e784
DG
366
367 :5.16 say state switch unicode_strings
2a4315f8 368 unicode_eval evalbytes current_sub fc
0b25e784 369
52fc5c56
FC
370 :5.18 say state switch unicode_strings
371 unicode_eval evalbytes current_sub fc
372
d09258e7
RS
373 :5.20 say state switch unicode_strings
374 unicode_eval evalbytes current_sub fc
375
b530a4ea
RS
376 :5.22 say state switch unicode_strings
377 unicode_eval evalbytes current_sub fc
378
6031f0d2
RS
379 :5.24 say state switch unicode_strings
380 unicode_eval evalbytes current_sub fc
1c2511e0 381 postderef_qq
6031f0d2 382
2c5484a6
RS
383 :5.26 say state switch unicode_strings
384 unicode_eval evalbytes current_sub fc
385 postderef_qq
386
c35a148e
S
387 :5.28 say state switch unicode_strings
388 unicode_eval evalbytes current_sub fc
401d2aaa 389 postderef_qq bitwise
c35a148e 390
01868d00
FC
391The C<:default> bundle represents the feature set that is enabled before
392any C<use feature> or C<no feature> declaration.
a3a91442
JV
393
394Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
b22bbcf0 395no effect. Feature bundles are guaranteed to be the same for all sub-versions.
bc9b29db 396
0b25e784
DG
397 use feature ":5.14.0"; # same as ":5.14"
398 use feature ":5.14.1"; # same as ":5.14"
a3a91442 399
7dfde25d
RGS
400=head1 IMPLICIT LOADING
401
0b25e784
DG
402Instead of loading feature bundles by name, it is easier to let Perl do
403implicit loading of a feature bundle for you.
404
405There are two ways to load the C<feature> pragma implicitly:
7dfde25d
RGS
406
407=over 4
408
409=item *
410
0b25e784
DG
411By using the C<-E> switch on the Perl command-line instead of C<-e>.
412That will enable the feature bundle for that version of Perl in the
413main compilation unit (that is, the one-liner that follows C<-E>).
7dfde25d
RGS
414
415=item *
416
0b25e784 417By explicitly requiring a minimum Perl version number for your program, with
b22bbcf0 418the C<use VERSION> construct. That is,
7dfde25d 419
0b25e784 420 use v5.10.0;
7dfde25d
RGS
421
422will do an implicit
423
39ec54a5 424 no feature ':all';
82cfb3a2 425 use feature ':5.10';
7dfde25d 426
b22bbcf0
FC
427and so on. Note how the trailing sub-version
428is automatically stripped from the
82cfb3a2 429version.
7dfde25d 430
8d115822
RB
431But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
432
433 use 5.010;
434
435with the same effect.
436
0b25e784 437If the required version is older than Perl 5.10, the ":default" feature
01868d00 438bundle is automatically loaded instead.
70397346 439
affe54fa
AC
440Unlike C<use feature ":5.12">, saying C<use v5.12> (or any higher version)
441also does the equivalent of C<use strict>; see L<perlfunc/use> for details.
442
7dfde25d
RGS
443=back
444
0d863452
RH
445=cut
446
447sub import {
22055af9 448 shift;
36143a0c
NC
449
450 if (!@_) {
0b25e784 451 croak("No features specified");
0d863452 452 }
36143a0c 453
d3757264 454 __common(1, @_);
0d863452
RH
455}
456
457sub unimport {
22055af9 458 shift;
0d863452 459
39ec54a5 460 # A bare C<no feature> should reset to the default bundle
bc9b29db 461 if (!@_) {
39ec54a5
RS
462 $^H &= ~($hint_uni8bit|$hint_mask);
463 return;
bc9b29db
RH
464 }
465
d3757264
NC
466 __common(0, @_);
467}
468
469
470sub __common {
471 my $import = shift;
0c8d5017
NC
472 my $bundle_number = $^H & $hint_mask;
473 my $features = $bundle_number != $hint_mask
474 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
475 if ($features) {
da5b5421 476 # Features are enabled implicitly via bundle hints.
d9ee6ccb
NC
477 # Delete any keys that may be left over from last time.
478 delete @^H{ values(%feature) };
479 $^H |= $hint_mask;
480 for (@$features) {
481 $^H{$feature{$_}} = 1;
482 $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
483 }
da5b5421 484 }
bc9b29db 485 while (@_) {
0b25e784
DG
486 my $name = shift;
487 if (substr($name, 0, 1) eq ":") {
488 my $v = substr($name, 1);
489 if (!exists $feature_bundle{$v}) {
490 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
491 if (!exists $feature_bundle{$v}) {
492 unknown_feature_bundle(substr($name, 1));
493 }
494 }
495 unshift @_, @{$feature_bundle{$v}};
496 next;
497 }
36143a0c 498 if (!exists $feature{$name}) {
db629560
FC
499 if (exists $noops{$name}) {
500 next;
501 }
0b25e784
DG
502 unknown_feature($name);
503 }
d3757264
NC
504 if ($import) {
505 $^H{$feature{$name}} = 1;
506 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
507 } else {
0b25e784 508 delete $^H{$feature{$name}};
1863b879 509 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
0b25e784 510 }
0d863452 511 }
0d863452
RH
512}
513
b42943c4
RGS
514sub unknown_feature {
515 my $feature = shift;
516 croak(sprintf('Feature "%s" is not supported by Perl %vd',
0b25e784 517 $feature, $^V));
b42943c4
RGS
518}
519
520sub unknown_feature_bundle {
521 my $feature = shift;
522 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
0b25e784 523 $feature, $^V));
b42943c4
RGS
524}
525
526sub croak {
527 require Carp;
528 Carp::croak(@_);
529}
530
0d863452 5311;
69bcf1d3
FC
532
533# ex: set ro: