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)
28 evalbytes => 'evalbytes',
29 array_base => 'arybase',
30 current_sub => '__SUB__',
31 unicode_eval => 'unieval',
32 unicode_strings => 'unicode',
36 # NOTE: If a feature is ever enabled in a non-contiguous range of Perl
37 # versions, any code below that uses %BundleRanges will have to
38 # be changed to account.
40 # 5.odd implies the next 5.even, but an explicit 5.even can override it.
41 my %feature_bundle = (
42 all => [ keys %feature ],
43 default => [qw(array_base)],
44 "5.9.5" => [qw(say state switch array_base)],
45 "5.10" => [qw(say state switch array_base)],
46 "5.11" => [qw(say state switch unicode_strings array_base)],
47 "5.13" => [qw(say state switch unicode_strings array_base)],
48 "5.15" => [qw(say state switch unicode_strings unicode_eval
49 evalbytes current_sub fc)],
50 "5.17" => [qw(say state switch unicode_strings unicode_eval
51 evalbytes current_sub fc)],
55 ###########################################################################
56 # More data generated from the above
58 for (keys %feature_bundle) {
59 next unless /^5\.(\d*[13579])\z/;
60 $feature_bundle{"5.".($1+1)} ||= $feature_bundle{$_};
63 my %UniqueBundles; # "say state switch" => 5.10
64 my %Aliases; # 5.12 => 5.11
65 for( sort keys %feature_bundle ) {
66 my $value = join(' ', sort @{$feature_bundle{$_}});
67 if (exists $UniqueBundles{$value}) {
68 $Aliases{$_} = $UniqueBundles{$value};
71 $UniqueBundles{$value} = $_;
75 my %BundleRanges; # say => ['5.10', '5.15'] # unique bundles for values
77 sort { $a eq 'default' ? -1 : $b eq 'default' ? 1 : $a cmp $b }
80 next if $bund =~ /[^\d.]/ and $bund ne 'default';
81 for (@{$feature_bundle{$bund}}) {
82 if (@{$BundleRanges{$_} ||= []} == 2) {
83 $BundleRanges{$_}[1] = $bund
86 push @{$BundleRanges{$_}}, $bund;
95 open "perl.h", "perl.h" or die "$0 cannot open perl.h: $!";
96 while (readline "perl.h") {
97 next unless /#\s*define\s+(HINT_FEATURE_MASK|HINT_UNI_8_BIT)/;
99 /(0x[A-Fa-f0-9]+)/ or die "No hex number in:\n\n$_\n ";
104 my $hex = $HintMask = $1;
105 my $bits = sprintf "%b", oct $1;
106 $bits =~ /^0*1+(0*)\z/
107 or die "Non-contiguous bits in $bits (binary for $hex):\n\n$_\n ";
108 $HintShift = length $1;
110 length sprintf "%b", scalar keys %UniqueBundles;
111 $bits =~ /1{$bits_needed}/
112 or die "Not enough bits (need $bits_needed)"
113 . " in $bits (binary for $hex):\n\n$_\n ";
115 if ($Uni8Bit && $HintMask) { last }
117 die "No HINT_FEATURE_MASK defined in perl.h" unless $HintMask;
118 die "No HINT_UNI_8_BIT defined in perl.h" unless $Uni8Bit;
123 ('default', grep !/[^\d.]/, sort values %UniqueBundles);
126 ###########################################################################
127 # Open files to be generated
130 open_new($_, '>', { by => 'regen/feature.pl' });
131 } 'lib/feature.pm', 'feature.h';
134 ###########################################################################
135 # Generate lib/feature.pm
138 last if /^FEATURES$/ ;
145 if (!defined $long or length $long < length) {
152 print $pm "our %feature = (\n";
153 my $width = length longest keys %feature;
154 for(sort { length $a <=> length $b } keys %feature) {
155 print $pm " $_" . " "x($width-length)
156 . " => 'feature_$feature{$_}',\n";
160 print $pm "our %feature_bundle = (\n";
161 $width = length longest values %UniqueBundles;
162 for( sort { $UniqueBundles{$a} cmp $UniqueBundles{$b} }
163 keys %UniqueBundles ) {
164 my $bund = $UniqueBundles{$_};
165 print $pm qq' "$bund"' . " "x($width-length $bund)
166 . qq' => [qw($_)],\n';
170 for (sort keys %Aliases) {
172 qq'\$feature_bundle{"$_"} = \$feature_bundle{"$Aliases{$_}"};\n';
177 our \$hint_shift = $HintShift;
178 our \$hint_mask = $HintMask;
179 our \@hint_bundles = qw( @HintedBundles );
181 # This gets set (for now) in \$^H as well as in %^H,
182 # for runtime speed of the uc/lc/ucfirst/lcfirst functions.
183 # See HINT_UNI_8_BIT in perl.h.
184 our \$hint_uni8bit = $Uni8Bit;
189 last if /^PODTURES$/ ;
193 select +(select($pm), $~ = 'PODTURES')[0];
195 ^<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
196 $::bundle, $::feature
199 for ('default', sort grep /\.\d[02468]/, keys %feature_bundle) {
201 $::feature = join ' ', @{$feature_bundle{$_}};
210 read_only_bottom_close_and_rename($pm);
213 ###########################################################################
218 #if defined(PERL_CORE) || defined (PERL_EXT)
220 #define HINT_FEATURE_SHIFT $HintShift
225 for (@HintedBundles) {
226 (my $key = uc) =~ y/.//d;
227 print $h "#define FEATURE_BUNDLE_$key ", $count++, "\n";
231 #define FEATURE_BUNDLE_CUSTOM (HINT_FEATURE_MASK >> HINT_FEATURE_SHIFT)
233 #define CURRENT_HINTS \
234 (PL_curcop == &PL_compiling ? PL_hints : PL_curcop->cop_hints)
235 #define CURRENT_FEATURE_BUNDLE \
236 ((CURRENT_HINTS & HINT_FEATURE_MASK) >> HINT_FEATURE_SHIFT)
238 /* Avoid using ... && Perl_feature_is_enabled(...) as that triggers a bug in
239 the HP-UX cc on PA-RISC */
240 #define FEATURE_IS_ENABLED(name) \
242 & HINT_LOCALIZE_HH) \
243 ? Perl_feature_is_enabled(aTHX_ STR_WITH_LEN(name)) : FALSE)
244 /* The longest string we pass in. */
247 my $longest_internal_feature_name = longest values %feature;
249 #define MAX_FEATURE_LEN (sizeof("$longest_internal_feature_name")-1)
254 sort { length $a <=> length $b } keys %feature
257 map { (my $__ = uc) =~ y/.//d; $__ } @{$BundleRanges{$_}};
258 my $name = $feature{$_};
260 if ($last && $first eq 'DEFAULT') { # ‘>= DEFAULT’ warns
262 #define FEATURE_$NAME\_IS_ENABLED \\
264 CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_$last \\
265 || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
266 FEATURE_IS_ENABLED("$name")) \\
273 #define FEATURE_$NAME\_IS_ENABLED \\
275 (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_$first && \\
276 CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_$last) \\
277 || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
278 FEATURE_IS_ENABLED("$name")) \\
285 #define FEATURE_$NAME\_IS_ENABLED \\
287 CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_$first \\
288 || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
289 FEATURE_IS_ENABLED("$name")) \\
298 #endif /* PERL_CORE or PERL_EXT */
301 PERL_STATIC_INLINE void
302 S_enable_feature_bundle(pTHX_ SV *ver)
304 SV *comp_ver = sv_newmortal();
305 PL_hints = (PL_hints &~ HINT_FEATURE_MASK)
309 for (reverse @HintedBundles[1..$#HintedBundles]) { # skip default
311 if ($numver eq '5.10') { $numver = '5.009005' } # special case
312 else { $numver =~ s/\./.0/ } # 5.11 => 5.011
313 (my $macrover = $_) =~ y/.//d;
315 (sv_setnv(comp_ver, $numver),
316 vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
317 ? FEATURE_BUNDLE_$macrover :
322 FEATURE_BUNDLE_DEFAULT
323 ) << HINT_FEATURE_SHIFT;
325 assert(PL_curcop == &PL_compiling);
326 if (FEATURE_UNICODE_IS_ENABLED) PL_hints |= HINT_UNI_8_BIT;
327 else PL_hints &= ~HINT_UNI_8_BIT;
329 #endif /* PERL_IN_OP_C */
332 read_only_bottom_close_and_rename($h);
335 ###########################################################################
336 # Template for feature.pm
341 our $VERSION = '1.28';
346 # - think about versioned features (use feature switch => 2)
350 feature - Perl pragma to enable new features
354 use feature qw(say switch);
356 when (1) { say "\$foo == 1" }
357 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
358 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
359 when ($_ > 100) { say "\$foo > 100" }
360 default { say "None of the above" }
363 use feature ':5.10'; # loads all features available in perl 5.10
365 use v5.10; # implicitly loads :5.10 feature bundle
369 It is usually impossible to add new syntax to Perl without breaking
370 some existing programs. This pragma provides a way to minimize that
371 risk. New syntactic constructs, or new semantic meanings to older
372 constructs, can be enabled by C<use feature 'foo'>, and will be parsed
373 only when the appropriate feature pragma is in scope. (Nevertheless, the
374 C<CORE::> prefix provides access to all Perl keywords, regardless of this
377 =head2 Lexical effect
379 Like other pragmas (C<use strict>, for example), features have a lexical
380 effect. C<use feature qw(foo)> will only make the feature "foo" available
381 from that point to the end of the enclosing block.
385 say "say is available here";
387 print "But not here.\n";
391 Features can also be turned off by using C<no feature "foo">. This too
395 say "say is available here";
398 print "But not here.\n";
400 say "Yet it is here.";
402 C<no feature> with no features specified will reset to the default group. To
403 disable I<all> features (an unusual request!) use C<no feature ':all'>.
405 =head1 AVAILABLE FEATURES
407 =head2 The 'say' feature
409 C<use feature 'say'> tells the compiler to enable the Perl 6 style
412 See L<perlfunc/say> for details.
414 This feature is available starting with Perl 5.10.
416 =head2 The 'state' feature
418 C<use feature 'state'> tells the compiler to enable C<state>
421 See L<perlsub/"Persistent Private Variables"> for details.
423 This feature is available starting with Perl 5.10.
425 =head2 The 'switch' feature
427 C<use feature 'switch'> tells the compiler to enable the Perl 6
428 given/when construct.
430 See L<perlsyn/"Switch Statements"> for details.
432 This feature is available starting with Perl 5.10.
434 =head2 The 'unicode_strings' feature
436 C<use feature 'unicode_strings'> tells the compiler to use Unicode semantics
437 in all string operations executed within its scope (unless they are also
438 within the scope of either C<use locale> or C<use bytes>). The same applies
439 to all regular expressions compiled within the scope, even if executed outside
442 C<no feature 'unicode_strings'> tells the compiler to use the traditional
443 Perl semantics wherein the native character set semantics is used unless it is
444 clear to Perl that Unicode is desired. This can lead to some surprises
445 when the behavior suddenly changes. (See
446 L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are
447 potentially using Unicode in your program, the
448 C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
450 This feature is available starting with Perl 5.12; was almost fully
451 implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>.
453 =head2 The 'unicode_eval' and 'evalbytes' features
455 Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a
456 string, will evaluate it as a string of characters, ignoring any
457 C<use utf8> declarations. C<use utf8> exists to declare the encoding of
458 the script, which only makes sense for a stream of bytes, not a string of
459 characters. Source filters are forbidden, as they also really only make
460 sense on strings of bytes. Any attempt to activate a source filter will
463 The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates
464 the argument passed to it as a string of bytes. It dies if the string
465 contains any characters outside the 8-bit range. Source filters work
466 within C<evalbytes>: they apply to the contents of the string being
469 Together, these two features are intended to replace the historical C<eval>
470 function, which has (at least) two bugs in it, that cannot easily be fixed
471 without breaking existing programs:
477 C<eval> behaves differently depending on the internal encoding of the
478 string, sometimes treating its argument as a string of bytes, and sometimes
479 as a string of characters.
483 Source filters activated within C<eval> leak out into whichever I<file>
484 scope is currently being compiled. To give an example with the CPAN module
487 BEGIN { eval "use Semi::Semicolons; # not filtered here " }
490 C<evalbytes> fixes that to work the way one would expect:
492 use feature "evalbytes";
493 BEGIN { evalbytes "use Semi::Semicolons; # filtered " }
498 These two features are available starting with Perl 5.16.
500 =head2 The 'current_sub' feature
502 This provides the C<__SUB__> token that returns a reference to the current
503 subroutine or C<undef> outside of a subroutine.
505 This feature is available starting with Perl 5.16.
507 =head2 The 'array_base' feature
509 This feature supports the legacy C<$[> variable. See L<perlvar/$[> and
510 L<arybase>. It is on by default but disabled under C<use v5.16> (see
511 L</IMPLICIT LOADING>, below).
513 This feature is available under this name starting with Perl 5.16. In
514 previous versions, it was simply on all the time, and this pragma knew
517 =head2 The 'fc' feature
519 C<use feature 'fc'> tells the compiler to enable the C<fc> function,
520 which implements Unicode casefolding.
522 See L<perlfunc/fc> for details.
524 This feature is available from Perl 5.16 onwards.
526 =head1 FEATURE BUNDLES
528 It's possible to load multiple features together, using
529 a I<feature bundle>. The name of a feature bundle is prefixed with
530 a colon, to distinguish it from an actual feature.
534 The following feature bundles are available:
536 bundle features included
537 --------- -----------------
539 The C<:default> bundle represents the feature set that is enabled before
540 any C<use feature> or C<no feature> declaration.
542 Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
543 no effect. Feature bundles are guaranteed to be the same for all sub-versions.
545 use feature ":5.14.0"; # same as ":5.14"
546 use feature ":5.14.1"; # same as ":5.14"
548 =head1 IMPLICIT LOADING
550 Instead of loading feature bundles by name, it is easier to let Perl do
551 implicit loading of a feature bundle for you.
553 There are two ways to load the C<feature> pragma implicitly:
559 By using the C<-E> switch on the Perl command-line instead of C<-e>.
560 That will enable the feature bundle for that version of Perl in the
561 main compilation unit (that is, the one-liner that follows C<-E>).
565 By explicitly requiring a minimum Perl version number for your program, with
566 the C<use VERSION> construct. That is,
575 and so on. Note how the trailing sub-version
576 is automatically stripped from the
579 But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
583 with the same effect.
585 If the required version is older than Perl 5.10, the ":default" feature
586 bundle is automatically loaded instead.
596 croak("No features specified");
605 # A bare C<no feature> should reset to the default bundle
607 $^H &= ~($hint_uni8bit|$hint_mask);
617 my $bundle_number = $^H & $hint_mask;
618 my $features = $bundle_number != $hint_mask
619 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
621 # Features are enabled implicitly via bundle hints.
622 # Delete any keys that may be left over from last time.
623 delete @^H{ values(%feature) };
626 $^H{$feature{$_}} = 1;
627 $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
632 if (substr($name, 0, 1) eq ":") {
633 my $v = substr($name, 1);
634 if (!exists $feature_bundle{$v}) {
635 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
636 if (!exists $feature_bundle{$v}) {
637 unknown_feature_bundle(substr($name, 1));
640 unshift @_, @{$feature_bundle{$v}};
643 if (!exists $feature{$name}) {
644 unknown_feature($name);
647 $^H{$feature{$name}} = 1;
648 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
650 delete $^H{$feature{$name}};
651 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
656 sub unknown_feature {
658 croak(sprintf('Feature "%s" is not supported by Perl %vd',
662 sub unknown_feature_bundle {
664 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',