3 # Regenerate (overwriting only if changed):
8 # from information hardcoded into this script and from two #defines
11 # This script is normally invoked from regen.pl.
14 require './regen/regen_lib.pl';
20 ###########################################################################
23 # (feature name) => (internal name, used in %^H and macro names)
29 evalbytes => 'evalbytes',
30 array_base => 'arybase',
31 current_sub => '__SUB__',
32 refaliasing => 'refaliasing',
33 postderef_qq => 'postderef_qq',
34 unicode_eval => 'unieval',
35 declared_refs => 'myref',
36 unicode_strings => 'unicode',
38 signatures => 'signatures',
41 # NOTE: If a feature is ever enabled in a non-contiguous range of Perl
42 # versions, any code below that uses %BundleRanges will have to
43 # be changed to account.
45 # 5.odd implies the next 5.even, but an explicit 5.even can override it.
46 my %feature_bundle = (
47 all => [ keys %feature ],
48 default => [qw(array_base)],
49 "5.9.5" => [qw(say state switch array_base)],
50 "5.10" => [qw(say state switch array_base)],
51 "5.11" => [qw(say state switch unicode_strings array_base)],
52 "5.13" => [qw(say state switch unicode_strings array_base)],
53 "5.15" => [qw(say state switch unicode_strings unicode_eval
54 evalbytes current_sub fc)],
55 "5.17" => [qw(say state switch unicode_strings unicode_eval
56 evalbytes current_sub fc)],
57 "5.19" => [qw(say state switch unicode_strings unicode_eval
58 evalbytes current_sub fc)],
59 "5.21" => [qw(say state switch unicode_strings unicode_eval
60 evalbytes current_sub fc)],
61 "5.23" => [qw(say state switch unicode_strings unicode_eval
62 evalbytes current_sub fc postderef_qq)],
63 "5.25" => [qw(say state switch unicode_strings unicode_eval
64 evalbytes current_sub fc postderef_qq)],
65 "5.27" => [qw(say state switch unicode_strings unicode_eval
66 evalbytes current_sub fc postderef_qq bitwise)],
67 "5.29" => [qw(say state switch unicode_strings unicode_eval
68 evalbytes current_sub fc postderef_qq bitwise)],
71 my @noops = qw( postderef lexical_subs );
74 ###########################################################################
75 # More data generated from the above
77 for (keys %feature_bundle) {
78 next unless /^5\.(\d*[13579])\z/;
79 $feature_bundle{"5.".($1+1)} ||= $feature_bundle{$_};
82 my %UniqueBundles; # "say state switch" => 5.10
83 my %Aliases; # 5.12 => 5.11
84 for( sort keys %feature_bundle ) {
85 my $value = join(' ', sort @{$feature_bundle{$_}});
86 if (exists $UniqueBundles{$value}) {
87 $Aliases{$_} = $UniqueBundles{$value};
90 $UniqueBundles{$value} = $_;
94 my %BundleRanges; # say => ['5.10', '5.15'] # unique bundles for values
96 sort { $a eq 'default' ? -1 : $b eq 'default' ? 1 : $a cmp $b }
99 next if $bund =~ /[^\d.]/ and $bund ne 'default';
100 for (@{$feature_bundle{$bund}}) {
101 if (@{$BundleRanges{$_} ||= []} == 2) {
102 $BundleRanges{$_}[1] = $bund
105 push @{$BundleRanges{$_}}, $bund;
114 open "perl.h", "<", "perl.h" or die "$0 cannot open perl.h: $!";
115 while (readline "perl.h") {
116 next unless /#\s*define\s+(HINT_FEATURE_MASK|HINT_UNI_8_BIT)/;
117 my $is_u8b = $1 =~ 8;
118 /(0x[A-Fa-f0-9]+)/ or die "No hex number in:\n\n$_\n ";
123 my $hex = $HintMask = $1;
124 my $bits = sprintf "%b", oct $1;
125 $bits =~ /^0*1+(0*)\z/
126 or die "Non-contiguous bits in $bits (binary for $hex):\n\n$_\n ";
127 $HintShift = length $1;
129 length sprintf "%b", scalar keys %UniqueBundles;
130 $bits =~ /1{$bits_needed}/
131 or die "Not enough bits (need $bits_needed)"
132 . " in $bits (binary for $hex):\n\n$_\n ";
134 if ($Uni8Bit && $HintMask) { last }
136 die "No HINT_FEATURE_MASK defined in perl.h" unless $HintMask;
137 die "No HINT_UNI_8_BIT defined in perl.h" unless $Uni8Bit;
142 ('default', grep !/[^\d.]/, sort values %UniqueBundles);
145 ###########################################################################
146 # Open files to be generated
149 open_new($_, '>', { by => 'regen/feature.pl' });
150 } 'lib/feature.pm', 'feature.h';
153 ###########################################################################
154 # Generate lib/feature.pm
157 last if /^FEATURES$/ ;
164 if (!defined $long or length $long < length) {
171 print $pm "our %feature = (\n";
172 my $width = length longest keys %feature;
173 for(sort { length $a <=> length $b || $a cmp $b } keys %feature) {
174 print $pm " $_" . " "x($width-length)
175 . " => 'feature_$feature{$_}',\n";
179 print $pm "our %feature_bundle = (\n";
180 $width = length longest values %UniqueBundles;
181 for( sort { $UniqueBundles{$a} cmp $UniqueBundles{$b} }
182 keys %UniqueBundles ) {
183 my $bund = $UniqueBundles{$_};
184 print $pm qq' "$bund"' . " "x($width-length $bund)
185 . qq' => [qw($_)],\n';
189 for (sort keys %Aliases) {
191 qq'\$feature_bundle{"$_"} = \$feature_bundle{"$Aliases{$_}"};\n';
194 print $pm "my \%noops = (\n";
195 print $pm " $_ => 1,\n", for @noops;
200 our \$hint_shift = $HintShift;
201 our \$hint_mask = $HintMask;
202 our \@hint_bundles = qw( @HintedBundles );
204 # This gets set (for now) in \$^H as well as in %^H,
205 # for runtime speed of the uc/lc/ucfirst/lcfirst functions.
206 # See HINT_UNI_8_BIT in perl.h.
207 our \$hint_uni8bit = $Uni8Bit;
212 last if /^PODTURES$/ ;
216 select +(select($pm), $~ = 'PODTURES')[0];
218 ^<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
219 $::bundle, $::feature
222 for ('default', sort grep /\.\d[02468]/, keys %feature_bundle) {
224 $::feature = join ' ', @{$feature_bundle{$_}};
233 read_only_bottom_close_and_rename($pm);
236 ###########################################################################
241 #if defined(PERL_CORE) || defined (PERL_EXT)
243 #define HINT_FEATURE_SHIFT $HintShift
248 for (@HintedBundles) {
249 (my $key = uc) =~ y/.//d;
250 print $h "#define FEATURE_BUNDLE_$key ", $count++, "\n";
254 #define FEATURE_BUNDLE_CUSTOM (HINT_FEATURE_MASK >> HINT_FEATURE_SHIFT)
256 #define CURRENT_HINTS \
257 (PL_curcop == &PL_compiling ? PL_hints : PL_curcop->cop_hints)
258 #define CURRENT_FEATURE_BUNDLE \
259 ((CURRENT_HINTS & HINT_FEATURE_MASK) >> HINT_FEATURE_SHIFT)
261 /* Avoid using ... && Perl_feature_is_enabled(...) as that triggers a bug in
262 the HP-UX cc on PA-RISC */
263 #define FEATURE_IS_ENABLED(name) \
265 & HINT_LOCALIZE_HH) \
266 ? Perl_feature_is_enabled(aTHX_ STR_WITH_LEN(name)) : FALSE)
267 /* The longest string we pass in. */
270 my $longest_internal_feature_name = longest values %feature;
272 #define MAX_FEATURE_LEN (sizeof("$longest_internal_feature_name")-1)
277 sort { length $a <=> length $b || $a cmp $b } keys %feature
280 map { (my $__ = uc) =~ y/.//d; $__ } @{$BundleRanges{$_}};
281 my $name = $feature{$_};
283 if ($last && $first eq 'DEFAULT') { # '>= DEFAULT' warns
285 #define FEATURE_$NAME\_IS_ENABLED \\
287 CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_$last \\
288 || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
289 FEATURE_IS_ENABLED("$name")) \\
296 #define FEATURE_$NAME\_IS_ENABLED \\
298 (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_$first && \\
299 CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_$last) \\
300 || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
301 FEATURE_IS_ENABLED("$name")) \\
308 #define FEATURE_$NAME\_IS_ENABLED \\
310 CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_$first \\
311 || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
312 FEATURE_IS_ENABLED("$name")) \\
319 #define FEATURE_$NAME\_IS_ENABLED \\
321 CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
322 FEATURE_IS_ENABLED("$name") \\
331 #endif /* PERL_CORE or PERL_EXT */
334 PERL_STATIC_INLINE void
335 S_enable_feature_bundle(pTHX_ SV *ver)
337 SV *comp_ver = sv_newmortal();
338 PL_hints = (PL_hints &~ HINT_FEATURE_MASK)
342 for (reverse @HintedBundles[1..$#HintedBundles]) { # skip default
344 if ($numver eq '5.10') { $numver = '5.009005' } # special case
345 else { $numver =~ s/\./.0/ } # 5.11 => 5.011
346 (my $macrover = $_) =~ y/.//d;
348 (sv_setnv(comp_ver, $numver),
349 vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
350 ? FEATURE_BUNDLE_$macrover :
355 FEATURE_BUNDLE_DEFAULT
356 ) << HINT_FEATURE_SHIFT;
358 assert(PL_curcop == &PL_compiling);
359 if (FEATURE_UNICODE_IS_ENABLED) PL_hints |= HINT_UNI_8_BIT;
360 else PL_hints &= ~HINT_UNI_8_BIT;
362 #endif /* PERL_IN_OP_C */
365 read_only_bottom_close_and_rename($h);
368 ###########################################################################
369 # Template for feature.pm
374 our $VERSION = '1.53';
379 # - think about versioned features (use feature switch => 2)
383 feature - Perl pragma to enable new features
387 use feature qw(say switch);
389 when (1) { say "\$foo == 1" }
390 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
391 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
392 when ($_ > 100) { say "\$foo > 100" }
393 default { say "None of the above" }
396 use feature ':5.10'; # loads all features available in perl 5.10
398 use v5.10; # implicitly loads :5.10 feature bundle
402 It is usually impossible to add new syntax to Perl without breaking
403 some existing programs. This pragma provides a way to minimize that
404 risk. New syntactic constructs, or new semantic meanings to older
405 constructs, can be enabled by C<use feature 'foo'>, and will be parsed
406 only when the appropriate feature pragma is in scope. (Nevertheless, the
407 C<CORE::> prefix provides access to all Perl keywords, regardless of this
410 =head2 Lexical effect
412 Like other pragmas (C<use strict>, for example), features have a lexical
413 effect. C<use feature qw(foo)> will only make the feature "foo" available
414 from that point to the end of the enclosing block.
418 say "say is available here";
420 print "But not here.\n";
424 Features can also be turned off by using C<no feature "foo">. This too
428 say "say is available here";
431 print "But not here.\n";
433 say "Yet it is here.";
435 C<no feature> with no features specified will reset to the default group. To
436 disable I<all> features (an unusual request!) use C<no feature ':all'>.
438 =head1 AVAILABLE FEATURES
440 =head2 The 'say' feature
442 C<use feature 'say'> tells the compiler to enable the Perl 6 style
445 See L<perlfunc/say> for details.
447 This feature is available starting with Perl 5.10.
449 =head2 The 'state' feature
451 C<use feature 'state'> tells the compiler to enable C<state>
454 See L<perlsub/"Persistent Private Variables"> for details.
456 This feature is available starting with Perl 5.10.
458 =head2 The 'switch' feature
460 B<WARNING>: Because the L<smartmatch operator|perlop/"Smartmatch Operator"> is
461 experimental, Perl will warn when you use this feature, unless you have
462 explicitly disabled the warning:
464 no warnings "experimental::smartmatch";
466 C<use feature 'switch'> tells the compiler to enable the Perl 6
467 given/when construct.
469 See L<perlsyn/"Switch Statements"> for details.
471 This feature is available starting with Perl 5.10.
473 =head2 The 'unicode_strings' feature
475 C<use feature 'unicode_strings'> tells the compiler to use Unicode rules
476 in all string operations executed within its scope (unless they are also
477 within the scope of either C<use locale> or C<use bytes>). The same applies
478 to all regular expressions compiled within the scope, even if executed outside
479 it. It does not change the internal representation of strings, but only how
480 they are interpreted.
482 C<no feature 'unicode_strings'> tells the compiler to use the traditional
483 Perl rules wherein the native character set rules is used unless it is
484 clear to Perl that Unicode is desired. This can lead to some surprises
485 when the behavior suddenly changes. (See
486 L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are
487 potentially using Unicode in your program, the
488 C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
490 This feature is available starting with Perl 5.12; was almost fully
491 implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>;
492 was extended further in Perl 5.26 to cover L<the range
493 operator|perlop/Range Operators>; and was extended again in Perl 5.28 to
494 cover L<special-cased whitespace splitting|perlfunc/split>.
496 =head2 The 'unicode_eval' and 'evalbytes' features
498 Together, these two features are intended to replace the legacy string
499 C<eval> function, which behaves problematically in some instances. They are
500 available starting with Perl 5.16, and are enabled by default by a
501 S<C<use 5.16>> or higher declaration.
503 C<unicode_eval> changes the behavior of plain string C<eval> to work more
504 consistently, especially in the Unicode world. Certain (mis)behaviors
505 couldn't be changed without breaking some things that had come to rely on
506 them, so the feature can be enabled and disabled. Details are at
507 L<perlfunc/Under the "unicode_eval" feature>.
509 C<evalbytes> is like string C<eval>, but operating on a byte stream that is
510 not UTF-8 encoded. Details are at L<perlfunc/evalbytes EXPR>. Without a
511 S<C<use feature 'evalbytes'>> nor a S<C<use v5.16>> (or higher) declaration in
512 the current scope, you can still access it by instead writing
515 =head2 The 'current_sub' feature
517 This provides the C<__SUB__> token that returns a reference to the current
518 subroutine or C<undef> outside of a subroutine.
520 This feature is available starting with Perl 5.16.
522 =head2 The 'array_base' feature
524 This feature supports the legacy C<$[> variable. See L<perlvar/$[> and
525 L<arybase>. It is on by default but disabled under C<use v5.16> (see
526 L</IMPLICIT LOADING>, below).
528 This feature is available under this name starting with Perl 5.16. In
529 previous versions, it was simply on all the time, and this pragma knew
532 =head2 The 'fc' feature
534 C<use feature 'fc'> tells the compiler to enable the C<fc> function,
535 which implements Unicode casefolding.
537 See L<perlfunc/fc> for details.
539 This feature is available from Perl 5.16 onwards.
541 =head2 The 'lexical_subs' feature
543 In Perl versions prior to 5.26, this feature enabled
544 declaration of subroutines via C<my sub foo>, C<state sub foo>
545 and C<our sub foo> syntax. See L<perlsub/Lexical Subroutines> for details.
547 This feature is available from Perl 5.18 onwards. From Perl 5.18 to 5.24,
548 it was classed as experimental, and Perl emitted a warning for its
549 usage, except when explicitly disabled:
551 no warnings "experimental::lexical_subs";
553 As of Perl 5.26, use of this feature no longer triggers a warning, though
554 the C<experimental::lexical_subs> warning category still exists (for
555 compatibility with code that disables it). In addition, this syntax is
556 not only no longer experimental, but it is enabled for all Perl code,
557 regardless of what feature declarations are in scope.
559 =head2 The 'postderef' and 'postderef_qq' features
561 The 'postderef_qq' feature extends the applicability of L<postfix
562 dereference syntax|perlref/Postfix Dereference Syntax> so that postfix array
563 and scalar dereference are available in double-quotish interpolations. For
564 example, it makes the following two statements equivalent:
566 my $s = "[@{ $h->{a} }]";
567 my $s = "[$h->{a}->@*]";
569 This feature is available from Perl 5.20 onwards. In Perl 5.20 and 5.22, it
570 was classed as experimental, and Perl emitted a warning for its
571 usage, except when explicitly disabled:
573 no warnings "experimental::postderef";
575 As of Perl 5.24, use of this feature no longer triggers a warning, though
576 the C<experimental::postderef> warning category still exists (for
577 compatibility with code that disables it).
579 The 'postderef' feature was used in Perl 5.20 and Perl 5.22 to enable
580 postfix dereference syntax outside double-quotish interpolations. In those
581 versions, using it triggered the C<experimental::postderef> warning in the
582 same way as the 'postderef_qq' feature did. As of Perl 5.24, this syntax is
583 not only no longer experimental, but it is enabled for all Perl code,
584 regardless of what feature declarations are in scope.
586 =head2 The 'signatures' feature
588 B<WARNING>: This feature is still experimental and the implementation may
589 change in future versions of Perl. For this reason, Perl will
590 warn when you use the feature, unless you have explicitly disabled the
593 no warnings "experimental::signatures";
595 This enables unpacking of subroutine arguments into lexical variables
598 sub foo ($left, $right) {
599 return $left + $right;
602 See L<perlsub/Signatures> for details.
604 This feature is available from Perl 5.20 onwards.
606 =head2 The 'refaliasing' feature
608 B<WARNING>: This feature is still experimental and the implementation may
609 change in future versions of Perl. For this reason, Perl will
610 warn when you use the feature, unless you have explicitly disabled the
613 no warnings "experimental::refaliasing";
615 This enables aliasing via assignment to references:
617 \$a = \$b; # $a and $b now point to the same scalar
618 \@a = \@b; # to the same array
621 foreach \%hash (@array_of_hash_refs) {
625 See L<perlref/Assigning to References> for details.
627 This feature is available from Perl 5.22 onwards.
629 =head2 The 'bitwise' feature
631 This makes the four standard bitwise operators (C<& | ^ ~>) treat their
632 operands consistently as numbers, and introduces four new dotted operators
633 (C<&. |. ^. ~.>) that treat their operands consistently as strings. The
634 same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).
636 See L<perlop/Bitwise String Operators> for details.
638 This feature is available from Perl 5.22 onwards. Starting in Perl 5.28,
639 C<use v5.28> will enable the feature. Before 5.28, it was still
640 experimental and would emit a warning in the "experimental::bitwise"
643 =head2 The 'declared_refs' feature
645 B<WARNING>: This feature is still experimental and the implementation may
646 change in future versions of Perl. For this reason, Perl will
647 warn when you use the feature, unless you have explicitly disabled the
650 no warnings "experimental::declared_refs";
652 This allows a reference to a variable to be declared with C<my>, C<state>,
653 our C<our>, or localized with C<local>. It is intended mainly for use in
654 conjunction with the "refaliasing" feature. See L<perlref/Declaring a
655 Reference to a Variable> for examples.
657 This feature is available from Perl 5.26 onwards.
659 =head1 FEATURE BUNDLES
661 It's possible to load multiple features together, using
662 a I<feature bundle>. The name of a feature bundle is prefixed with
663 a colon, to distinguish it from an actual feature.
667 The following feature bundles are available:
669 bundle features included
670 --------- -----------------
672 The C<:default> bundle represents the feature set that is enabled before
673 any C<use feature> or C<no feature> declaration.
675 Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
676 no effect. Feature bundles are guaranteed to be the same for all sub-versions.
678 use feature ":5.14.0"; # same as ":5.14"
679 use feature ":5.14.1"; # same as ":5.14"
681 =head1 IMPLICIT LOADING
683 Instead of loading feature bundles by name, it is easier to let Perl do
684 implicit loading of a feature bundle for you.
686 There are two ways to load the C<feature> pragma implicitly:
692 By using the C<-E> switch on the Perl command-line instead of C<-e>.
693 That will enable the feature bundle for that version of Perl in the
694 main compilation unit (that is, the one-liner that follows C<-E>).
698 By explicitly requiring a minimum Perl version number for your program, with
699 the C<use VERSION> construct. That is,
708 and so on. Note how the trailing sub-version
709 is automatically stripped from the
712 But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
716 with the same effect.
718 If the required version is older than Perl 5.10, the ":default" feature
719 bundle is automatically loaded instead.
721 Unlike C<use feature ":5.12">, saying C<use v5.12> (or any higher version)
722 also does the equivalent of C<use strict>; see L<perlfunc/use> for details.
732 croak("No features specified");
741 # A bare C<no feature> should reset to the default bundle
743 $^H &= ~($hint_uni8bit|$hint_mask);
753 my $bundle_number = $^H & $hint_mask;
754 my $features = $bundle_number != $hint_mask
755 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
757 # Features are enabled implicitly via bundle hints.
758 # Delete any keys that may be left over from last time.
759 delete @^H{ values(%feature) };
762 $^H{$feature{$_}} = 1;
763 $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
768 if (substr($name, 0, 1) eq ":") {
769 my $v = substr($name, 1);
770 if (!exists $feature_bundle{$v}) {
771 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
772 if (!exists $feature_bundle{$v}) {
773 unknown_feature_bundle(substr($name, 1));
776 unshift @_, @{$feature_bundle{$v}};
779 if (!exists $feature{$name}) {
780 if (exists $noops{$name}) {
783 unknown_feature($name);
786 $^H{$feature{$name}} = 1;
787 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
789 delete $^H{$feature{$name}};
790 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
795 sub unknown_feature {
797 croak(sprintf('Feature "%s" is not supported by Perl %vd',
801 sub unknown_feature_bundle {
803 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',