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