This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Carp: Don’t choke on ISA constant
[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
20ae58f7 8our $VERSION = '1.49';
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)],
82d83da3 32 "all" => [qw(array_base bitwise current_sub declared_refs evalbytes fc postderef_qq refaliasing say signatures state switch unicode_eval unicode_strings)],
69bcf1d3 33 "default" => [qw(array_base)],
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"};
c35a148e
S
49$feature_bundle{"5.27"} = $feature_bundle{"5.23"};
50$feature_bundle{"5.28"} = $feature_bundle{"5.23"};
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;
2ad792cd 59our @hint_bundles = qw( default 5.10 5.11 5.15 5.23 );
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
47e9d84a
EB
154C<use feature 'switch'> tells the compiler to enable the Perl 6
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
319B<WARNING>: This feature is still experimental and the implementation may
320change in future versions of Perl. For this reason, Perl will
321warn when you use the feature, unless you have explicitly disabled the
322warning:
323
324 no warnings "experimental::bitwise";
325
326This makes the four standard bitwise operators (C<& | ^ ~>) treat their
327operands consistently as numbers, and introduces four new dotted operators
328(C<&. |. ^. ~.>) that treat their operands consistently as strings. The
329same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).
330
331See L<perlop/Bitwise String Operators> for details.
332
333This feature is available from Perl 5.22 onwards.
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 --------- -----------------
01868d00 363 :default array_base
0b25e784 364
01868d00 365 :5.10 say state switch array_base
0b25e784 366
01868d00 367 :5.12 say state switch unicode_strings array_base
0b25e784 368
01868d00 369 :5.14 say state switch unicode_strings array_base
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
393 postderef_qq
394
01868d00
FC
395The C<:default> bundle represents the feature set that is enabled before
396any C<use feature> or C<no feature> declaration.
a3a91442
JV
397
398Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
b22bbcf0 399no effect. Feature bundles are guaranteed to be the same for all sub-versions.
bc9b29db 400
0b25e784
DG
401 use feature ":5.14.0"; # same as ":5.14"
402 use feature ":5.14.1"; # same as ":5.14"
a3a91442 403
7dfde25d
RGS
404=head1 IMPLICIT LOADING
405
0b25e784
DG
406Instead of loading feature bundles by name, it is easier to let Perl do
407implicit loading of a feature bundle for you.
408
409There are two ways to load the C<feature> pragma implicitly:
7dfde25d
RGS
410
411=over 4
412
413=item *
414
0b25e784
DG
415By using the C<-E> switch on the Perl command-line instead of C<-e>.
416That will enable the feature bundle for that version of Perl in the
417main compilation unit (that is, the one-liner that follows C<-E>).
7dfde25d
RGS
418
419=item *
420
0b25e784 421By explicitly requiring a minimum Perl version number for your program, with
b22bbcf0 422the C<use VERSION> construct. That is,
7dfde25d 423
0b25e784 424 use v5.10.0;
7dfde25d
RGS
425
426will do an implicit
427
39ec54a5 428 no feature ':all';
82cfb3a2 429 use feature ':5.10';
7dfde25d 430
b22bbcf0
FC
431and so on. Note how the trailing sub-version
432is automatically stripped from the
82cfb3a2 433version.
7dfde25d 434
8d115822
RB
435But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
436
437 use 5.010;
438
439with the same effect.
440
0b25e784 441If the required version is older than Perl 5.10, the ":default" feature
01868d00 442bundle is automatically loaded instead.
70397346 443
7dfde25d
RGS
444=back
445
0d863452
RH
446=cut
447
448sub import {
22055af9 449 shift;
36143a0c
NC
450
451 if (!@_) {
0b25e784 452 croak("No features specified");
0d863452 453 }
36143a0c 454
d3757264 455 __common(1, @_);
0d863452
RH
456}
457
458sub unimport {
22055af9 459 shift;
0d863452 460
39ec54a5 461 # A bare C<no feature> should reset to the default bundle
bc9b29db 462 if (!@_) {
39ec54a5
RS
463 $^H &= ~($hint_uni8bit|$hint_mask);
464 return;
bc9b29db
RH
465 }
466
d3757264
NC
467 __common(0, @_);
468}
469
470
471sub __common {
472 my $import = shift;
0c8d5017
NC
473 my $bundle_number = $^H & $hint_mask;
474 my $features = $bundle_number != $hint_mask
475 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
476 if ($features) {
da5b5421 477 # Features are enabled implicitly via bundle hints.
d9ee6ccb
NC
478 # Delete any keys that may be left over from last time.
479 delete @^H{ values(%feature) };
480 $^H |= $hint_mask;
481 for (@$features) {
482 $^H{$feature{$_}} = 1;
483 $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
484 }
da5b5421 485 }
bc9b29db 486 while (@_) {
0b25e784
DG
487 my $name = shift;
488 if (substr($name, 0, 1) eq ":") {
489 my $v = substr($name, 1);
490 if (!exists $feature_bundle{$v}) {
491 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
492 if (!exists $feature_bundle{$v}) {
493 unknown_feature_bundle(substr($name, 1));
494 }
495 }
496 unshift @_, @{$feature_bundle{$v}};
497 next;
498 }
36143a0c 499 if (!exists $feature{$name}) {
db629560
FC
500 if (exists $noops{$name}) {
501 next;
502 }
0b25e784
DG
503 unknown_feature($name);
504 }
d3757264
NC
505 if ($import) {
506 $^H{$feature{$name}} = 1;
507 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
508 } else {
0b25e784 509 delete $^H{$feature{$name}};
1863b879 510 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
0b25e784 511 }
0d863452 512 }
0d863452
RH
513}
514
b42943c4
RGS
515sub unknown_feature {
516 my $feature = shift;
517 croak(sprintf('Feature "%s" is not supported by Perl %vd',
0b25e784 518 $feature, $^V));
b42943c4
RGS
519}
520
521sub unknown_feature_bundle {
522 my $feature = shift;
523 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
0b25e784 524 $feature, $^V));
b42943c4
RGS
525}
526
527sub croak {
528 require Carp;
529 Carp::croak(@_);
530}
531
0d863452 5321;
69bcf1d3
FC
533
534# ex: set ro: