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