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.29';
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
440 it. It does not change the internal representation of strings, but only how
441 they are interpreted.
443 C<no feature 'unicode_strings'> tells the compiler to use the traditional
444 Perl semantics wherein the native character set semantics is used unless it is
445 clear to Perl that Unicode is desired. This can lead to some surprises
446 when the behavior suddenly changes. (See
447 L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are
448 potentially using Unicode in your program, the
449 C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
451 This feature is available starting with Perl 5.12; was almost fully
452 implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>.
454 =head2 The 'unicode_eval' and 'evalbytes' features
456 Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a
457 string, will evaluate it as a string of characters, ignoring any
458 C<use utf8> declarations. C<use utf8> exists to declare the encoding of
459 the script, which only makes sense for a stream of bytes, not a string of
460 characters. Source filters are forbidden, as they also really only make
461 sense on strings of bytes. Any attempt to activate a source filter will
464 The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates
465 the argument passed to it as a string of bytes. It dies if the string
466 contains any characters outside the 8-bit range. Source filters work
467 within C<evalbytes>: they apply to the contents of the string being
470 Together, these two features are intended to replace the historical C<eval>
471 function, which has (at least) two bugs in it, that cannot easily be fixed
472 without breaking existing programs:
478 C<eval> behaves differently depending on the internal encoding of the
479 string, sometimes treating its argument as a string of bytes, and sometimes
480 as a string of characters.
484 Source filters activated within C<eval> leak out into whichever I<file>
485 scope is currently being compiled. To give an example with the CPAN module
488 BEGIN { eval "use Semi::Semicolons; # not filtered here " }
491 C<evalbytes> fixes that to work the way one would expect:
493 use feature "evalbytes";
494 BEGIN { evalbytes "use Semi::Semicolons; # filtered " }
499 These two features are available starting with Perl 5.16.
501 =head2 The 'current_sub' feature
503 This provides the C<__SUB__> token that returns a reference to the current
504 subroutine or C<undef> outside of a subroutine.
506 This feature is available starting with Perl 5.16.
508 =head2 The 'array_base' feature
510 This feature supports the legacy C<$[> variable. See L<perlvar/$[> and
511 L<arybase>. It is on by default but disabled under C<use v5.16> (see
512 L</IMPLICIT LOADING>, below).
514 This feature is available under this name starting with Perl 5.16. In
515 previous versions, it was simply on all the time, and this pragma knew
518 =head2 The 'fc' feature
520 C<use feature 'fc'> tells the compiler to enable the C<fc> function,
521 which implements Unicode casefolding.
523 See L<perlfunc/fc> for details.
525 This feature is available from Perl 5.16 onwards.
527 =head1 FEATURE BUNDLES
529 It's possible to load multiple features together, using
530 a I<feature bundle>. The name of a feature bundle is prefixed with
531 a colon, to distinguish it from an actual feature.
535 The following feature bundles are available:
537 bundle features included
538 --------- -----------------
540 The C<:default> bundle represents the feature set that is enabled before
541 any C<use feature> or C<no feature> declaration.
543 Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
544 no effect. Feature bundles are guaranteed to be the same for all sub-versions.
546 use feature ":5.14.0"; # same as ":5.14"
547 use feature ":5.14.1"; # same as ":5.14"
549 =head1 IMPLICIT LOADING
551 Instead of loading feature bundles by name, it is easier to let Perl do
552 implicit loading of a feature bundle for you.
554 There are two ways to load the C<feature> pragma implicitly:
560 By using the C<-E> switch on the Perl command-line instead of C<-e>.
561 That will enable the feature bundle for that version of Perl in the
562 main compilation unit (that is, the one-liner that follows C<-E>).
566 By explicitly requiring a minimum Perl version number for your program, with
567 the C<use VERSION> construct. That is,
576 and so on. Note how the trailing sub-version
577 is automatically stripped from the
580 But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
584 with the same effect.
586 If the required version is older than Perl 5.10, the ":default" feature
587 bundle is automatically loaded instead.
597 croak("No features specified");
606 # A bare C<no feature> should reset to the default bundle
608 $^H &= ~($hint_uni8bit|$hint_mask);
618 my $bundle_number = $^H & $hint_mask;
619 my $features = $bundle_number != $hint_mask
620 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
622 # Features are enabled implicitly via bundle hints.
623 # Delete any keys that may be left over from last time.
624 delete @^H{ values(%feature) };
627 $^H{$feature{$_}} = 1;
628 $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
633 if (substr($name, 0, 1) eq ":") {
634 my $v = substr($name, 1);
635 if (!exists $feature_bundle{$v}) {
636 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
637 if (!exists $feature_bundle{$v}) {
638 unknown_feature_bundle(substr($name, 1));
641 unshift @_, @{$feature_bundle{$v}};
644 if (!exists $feature{$name}) {
645 unknown_feature($name);
648 $^H{$feature{$name}} = 1;
649 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
651 delete $^H{$feature{$name}};
652 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
657 sub unknown_feature {
659 croak(sprintf('Feature "%s" is not supported by Perl %vd',
663 sub unknown_feature_bundle {
665 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',