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