This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Change docs display for PERL_UNUSED_foo
[perl5.git] / regen / feature.pl
1 #!/usr/bin/perl
2
3 # Regenerate (overwriting only if changed):
4 #
5 #    lib/feature.pm
6 #    feature.h
7 #
8 # from information hardcoded into this script and from two #defines
9 # in perl.h.
10 #
11 # This script is normally invoked from regen.pl.
12
13 BEGIN {
14     require './regen/regen_lib.pl';
15     push @INC, './lib';
16 }
17
18 use strict;
19 use warnings;
20
21 ###########################################################################
22 # Hand-editable data
23
24 # (feature name) => (internal name, used in %^H and macro names)
25 my %feature = (
26     say             => 'say',
27     state           => 'state',
28     switch          => 'switch',
29     bitwise         => 'bitwise',
30     evalbytes       => 'evalbytes',
31     current_sub     => '__SUB__',
32     refaliasing     => 'refaliasing',
33     postderef_qq    => 'postderef_qq',
34     unicode_eval    => 'unieval',
35     declared_refs   => 'myref',
36     unicode_strings => 'unicode',
37     fc              => 'fc',
38     signatures      => 'signatures',
39     isa             => 'isa',
40     indirect        => 'indirect',
41     multidimensional => 'multidimensional',
42 );
43
44 # NOTE: If a feature is ever enabled in a non-contiguous range of Perl
45 #       versions, any code below that uses %BundleRanges will have to
46 #       be changed to account.
47
48 # 5.odd implies the next 5.even, but an explicit 5.even can override it.
49
50 # features bundles
51 use constant V5_9_5 => sort qw{say state switch indirect multidimensional};
52 use constant V5_11  => sort ( +V5_9_5, qw{unicode_strings} );
53 use constant V5_15  => sort ( +V5_11, qw{unicode_eval evalbytes current_sub fc} );
54 use constant V5_23  => sort ( +V5_15, qw{postderef_qq} );
55 use constant V5_27  => sort ( +V5_23, qw{bitwise} );
56
57 my %feature_bundle = (
58     all     => [ sort keys %feature ],
59     default => [ qw{indirect multidimensional} ],
60     # using 5.9.5 features bundle
61     "5.9.5" => [ +V5_9_5 ],
62     "5.10"  => [ +V5_9_5 ],
63     # using 5.11 features bundle
64     "5.11"  => [ +V5_11 ],
65     "5.13"  => [ +V5_11 ],
66     # using 5.15 features bundle
67     "5.15"  => [ +V5_15 ],
68     "5.17"  => [ +V5_15 ],
69     "5.19"  => [ +V5_15 ],
70     "5.21"  => [ +V5_15 ],
71     # using 5.23 features bundle
72     "5.23"  => [ +V5_23 ],
73     "5.25"  => [ +V5_23 ],
74     # using 5.27 features bundle
75     "5.27"  => [ +V5_27 ],
76     "5.29"  => [ +V5_27 ],
77     "5.31"  => [ +V5_27 ],
78     "5.33"  => [ +V5_27 ],
79 );
80
81 my @noops = qw( postderef lexical_subs );
82 my @removed = qw( array_base );
83
84
85 ###########################################################################
86 # More data generated from the above
87
88 if (keys %feature > 32) {
89     die "cop_features only has room for 32 features";
90 }
91
92 my %feature_bits;
93 my $mask = 1;
94 for my $feature (sort keys %feature) {
95     $feature_bits{$feature} = $mask;
96     $mask <<= 1;
97 }
98
99 for (keys %feature_bundle) {
100     next unless /^5\.(\d*[13579])\z/;
101     $feature_bundle{"5.".($1+1)} ||= $feature_bundle{$_};
102 }
103
104 my %UniqueBundles; # "say state switch" => 5.10
105 my %Aliases;       #  5.12 => 5.11
106 for( sort keys %feature_bundle ) {
107     my $value = join(' ', sort @{$feature_bundle{$_}});
108     if (exists $UniqueBundles{$value}) {
109         $Aliases{$_} = $UniqueBundles{$value};
110     }
111     else {
112         $UniqueBundles{$value} = $_;
113     }
114 }
115                            # start   end
116 my %BundleRanges; # say => ['5.10', '5.15'] # unique bundles for values
117 for my $bund (
118     sort { $a eq 'default' ? -1 : $b eq 'default' ? 1 : $a cmp $b }
119          values %UniqueBundles
120 ) {
121     next if $bund =~ /[^\d.]/ and $bund ne 'default';
122     for (@{$feature_bundle{$bund}}) {
123         if (@{$BundleRanges{$_} ||= []} == 2) {
124             $BundleRanges{$_}[1] = $bund
125         }
126         else {
127             push @{$BundleRanges{$_}}, $bund;
128         }
129     }
130 }
131
132 my $HintShift;
133 my $HintMask;
134 my $Uni8Bit;
135
136 open "perl.h", "<", "perl.h" or die "$0 cannot open perl.h: $!";
137 while (readline "perl.h") {
138     next unless /#\s*define\s+(HINT_FEATURE_MASK|HINT_UNI_8_BIT)/;
139     my $is_u8b = $1 =~ 8;
140     /(0x[A-Fa-f0-9]+)/ or die "No hex number in:\n\n$_\n ";
141     if ($is_u8b) {
142         $Uni8Bit = $1;
143     }
144     else {
145         my $hex = $HintMask = $1;
146         my $bits = sprintf "%b", oct $1;
147         $bits =~ /^0*1+(0*)\z/
148          or die "Non-contiguous bits in $bits (binary for $hex):\n\n$_\n ";
149         $HintShift = length $1;
150         my $bits_needed =
151             length sprintf "%b", scalar keys %UniqueBundles;
152         $bits =~ /1{$bits_needed}/
153             or die "Not enough bits (need $bits_needed)"
154                  . " in $bits (binary for $hex):\n\n$_\n ";
155     }
156     if ($Uni8Bit && $HintMask) { last }
157 }
158 die "No HINT_FEATURE_MASK defined in perl.h" unless $HintMask;
159 die "No HINT_UNI_8_BIT defined in perl.h"    unless $Uni8Bit;
160
161 close "perl.h";
162
163 my @HintedBundles =
164     ('default', grep !/[^\d.]/, sort values %UniqueBundles);
165
166
167 ###########################################################################
168 # Open files to be generated
169
170 my ($pm, $h) = map {
171     open_new($_, '>', { by => 'regen/feature.pl' });
172 } 'lib/feature.pm', 'feature.h';
173
174
175 ###########################################################################
176 # Generate lib/feature.pm
177
178 while (<DATA>) {
179     last if /^FEATURES$/ ;
180     print $pm $_ ;
181 }
182
183 sub longest {
184     my $long;
185     for(@_) {
186         if (!defined $long or length $long < length) {
187             $long = $_;
188         }
189     }
190     $long;
191 }
192
193 print $pm "our %feature = (\n";
194 my $width = length longest keys %feature;
195 for(sort { length $a <=> length $b || $a cmp $b } keys %feature) {
196     print $pm "    $_" . " "x($width-length)
197             . " => 'feature_$feature{$_}',\n";
198 }
199 print $pm ");\n\n";
200
201 print $pm "our %feature_bundle = (\n";
202 my $bund_width = length longest values %UniqueBundles;
203 for( sort { $UniqueBundles{$a} cmp $UniqueBundles{$b} }
204           keys %UniqueBundles ) {
205     my $bund = $UniqueBundles{$_};
206     print $pm qq'    "$bund"' . " "x($bund_width-length $bund)
207             . qq' => [qw($_)],\n';
208 }
209 print $pm ");\n\n";
210
211 for (sort keys %Aliases) {
212     print $pm
213         qq'\$feature_bundle{"$_"} = \$feature_bundle{"$Aliases{$_}"};\n';
214 };
215
216 print $pm "my \%noops = (\n";
217 print $pm "    $_ => 1,\n", for @noops;
218 print $pm ");\n";
219
220 print $pm "my \%removed = (\n";
221 print $pm "    $_ => 1,\n", for @removed;
222 print $pm ");\n";
223
224 print $pm <<EOPM;
225
226 our \$hint_shift   = $HintShift;
227 our \$hint_mask    = $HintMask;
228 our \@hint_bundles = qw( @HintedBundles );
229
230 # This gets set (for now) in \$^H as well as in %^H,
231 # for runtime speed of the uc/lc/ucfirst/lcfirst functions.
232 # See HINT_UNI_8_BIT in perl.h.
233 our \$hint_uni8bit = $Uni8Bit;
234 EOPM
235
236
237 while (<DATA>) {
238     last if /^PODTURES$/ ;
239     print $pm $_ ;
240 }
241
242 select +(select($pm), $~ = 'PODTURES')[0];
243 format PODTURES =
244   ^<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
245 $::bundle, $::feature
246 .
247
248 for ('default', sort grep /\.\d[02468]/, keys %feature_bundle) {
249     $::bundle = ":$_";
250     $::feature = join ' ', @{$feature_bundle{$_}};
251     write $pm;
252     print $pm "\n";
253 }
254
255 while (<DATA>) {
256     print $pm $_ ;
257 }
258
259 read_only_bottom_close_and_rename($pm);
260
261
262 ###########################################################################
263 # Generate feature.h
264
265 print $h <<EOH;
266
267 #ifndef PERL_FEATURE_H_
268 #define PERL_FEATURE_H_
269
270 #if defined(PERL_CORE) || defined (PERL_EXT)
271
272 #define HINT_FEATURE_SHIFT      $HintShift
273
274 EOH
275
276 for (sort keys %feature_bits) {
277     printf $h "#define FEATURE_%s_BIT%*s %#06x\n", uc($feature{$_}),
278       $width-length($feature{$_}), "", $feature_bits{$_};
279 }
280 print $h "\n";
281
282 my $count;
283 for (@HintedBundles) {
284     (my $key = uc) =~ y/.//d;
285     print $h "#define FEATURE_BUNDLE_$key       ", $count++, "\n";
286 }
287
288 print $h <<'EOH';
289 #define FEATURE_BUNDLE_CUSTOM   (HINT_FEATURE_MASK >> HINT_FEATURE_SHIFT)
290
291 #define CURRENT_HINTS \
292     (PL_curcop == &PL_compiling ? PL_hints : PL_curcop->cop_hints)
293 #define CURRENT_FEATURE_BUNDLE \
294     ((CURRENT_HINTS & HINT_FEATURE_MASK) >> HINT_FEATURE_SHIFT)
295
296 #define FEATURE_IS_ENABLED_MASK(mask)                   \
297   ((CURRENT_HINTS & HINT_LOCALIZE_HH)                \
298     ? (PL_curcop->cop_features & (mask)) : FALSE)
299
300 /* The longest string we pass in.  */
301 EOH
302
303 my $longest_internal_feature_name = longest values %feature;
304 print $h <<EOL;
305 #define MAX_FEATURE_LEN (sizeof("$longest_internal_feature_name")-1)
306
307 EOL
308
309 for (
310     sort { length $a <=> length $b || $a cmp $b } keys %feature
311 ) {
312     my($first,$last) =
313         map { (my $__ = uc) =~ y/.//d; $__ } @{$BundleRanges{$_}};
314     my $name = $feature{$_};
315     my $NAME = uc $name;
316     if ($last && $first eq 'DEFAULT') { #  '>= DEFAULT' warns
317         print $h <<EOI;
318 #define FEATURE_${NAME}_IS_ENABLED \\
319     ( \\
320         CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_$last \\
321      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
322          FEATURE_IS_ENABLED_MASK(FEATURE_${NAME}_BIT)) \\
323     )
324
325 EOI
326     }
327     elsif ($last) {
328         print $h <<EOH3;
329 #define FEATURE_${NAME}_IS_ENABLED \\
330     ( \\
331         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_$first && \\
332          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_$last) \\
333      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
334          FEATURE_IS_ENABLED_MASK(FEATURE_${NAME}_BIT)) \\
335     )
336
337 EOH3
338     }
339     elsif ($first) {
340         print $h <<EOH4;
341 #define FEATURE_${NAME}_IS_ENABLED \\
342     ( \\
343         CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_$first \\
344      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
345          FEATURE_IS_ENABLED_MASK(FEATURE_${NAME}_BIT)) \\
346     )
347
348 EOH4
349     }
350     else {
351         print $h <<EOH5;
352 #define FEATURE_${NAME}_IS_ENABLED \\
353     ( \\
354         CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \\
355          FEATURE_IS_ENABLED_MASK(FEATURE_${NAME}_BIT) \\
356     )
357
358 EOH5
359     }
360 }
361
362 print $h <<EOH;
363
364 #define SAVEFEATUREBITS() SAVEI32(PL_compiling.cop_features)
365
366 #define CLEARFEATUREBITS() (PL_compiling.cop_features = 0)
367
368 #define STOREFEATUREBITSHH(hh) \\
369   (hv_stores((hh), "feature/bits", newSVuv(PL_compiling.cop_features)))
370
371 #define FETCHFEATUREBITSHH(hh)                              \\
372   STMT_START {                                              \\
373       SV **fbsv = hv_fetchs((hh), "feature/bits", FALSE);   \\
374       PL_compiling.cop_features = fbsv ? SvUV(*fbsv) : 0;   \\
375   } STMT_END
376
377 #endif /* PERL_CORE or PERL_EXT */
378
379 #ifdef PERL_IN_OP_C
380 PERL_STATIC_INLINE void
381 S_enable_feature_bundle(pTHX_ SV *ver)
382 {
383     SV *comp_ver = sv_newmortal();
384     PL_hints = (PL_hints &~ HINT_FEATURE_MASK)
385              | (
386 EOH
387
388 for (reverse @HintedBundles[1..$#HintedBundles]) { # skip default
389     my $numver = $_;
390     if ($numver eq '5.10') { $numver = '5.009005' } # special case
391     else                   { $numver =~ s/\./.0/  } # 5.11 => 5.011
392     (my $macrover = $_) =~ y/.//d;
393     print $h <<"    EOK";
394                   (sv_setnv(comp_ver, $numver),
395                    vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
396                         ? FEATURE_BUNDLE_$macrover :
397     EOK
398 }
399
400 print $h <<EOJ;
401                           FEATURE_BUNDLE_DEFAULT
402                ) << HINT_FEATURE_SHIFT;
403     /* special case */
404     assert(PL_curcop == &PL_compiling);
405     if (FEATURE_UNICODE_IS_ENABLED) PL_hints |=  HINT_UNI_8_BIT;
406     else                            PL_hints &= ~HINT_UNI_8_BIT;
407 }
408 #endif /* PERL_IN_OP_C */
409
410 #ifdef PERL_IN_MG_C
411
412 #define magic_sethint_feature(keysv, keypv, keylen, valsv, valbool) \\
413     S_magic_sethint_feature(aTHX_ (keysv), (keypv), (keylen), (valsv), (valbool))
414 PERL_STATIC_INLINE void
415 S_magic_sethint_feature(pTHX_ SV *keysv, const char *keypv, STRLEN keylen,
416                         SV *valsv, bool valbool) {
417     if (keysv)
418       keypv = SvPV_const(keysv, keylen);
419
420     if (memBEGINs(keypv, keylen, "feature_")) {
421         const char *subf = keypv + (sizeof("feature_")-1);
422         U32 mask = 0;
423         switch (*subf) {
424 EOJ
425
426 my %pref;
427 for my $key (sort values %feature) {
428     push @{$pref{substr($key, 0, 1)}}, $key;
429 }
430
431 for my $pref (sort keys %pref) {
432     print $h <<EOS;
433         case '$pref':
434 EOS
435     my $first = 1;
436     for my $subkey (@{$pref{$pref}}) {
437         my $rest = substr($subkey, 1);
438         my $if = $first ? "if" : "else if";
439         print $h <<EOJ;
440             $if (keylen == sizeof("feature_$subkey")-1
441                  && memcmp(subf+1, "$rest", keylen - sizeof("feature_")) == 0) {
442                 mask = FEATURE_\U${subkey}\E_BIT;
443                 break;
444             }
445 EOJ
446
447         $first = 0;
448     }
449     print $h <<EOS;
450             return;
451
452 EOS
453 }
454
455 print $h <<EOJ;
456         default:
457             return;
458         }
459         if (valsv ? SvTRUE(valsv) : valbool)
460             PL_compiling.cop_features |= mask;
461         else
462             PL_compiling.cop_features &= ~mask;
463     }
464 }
465 #endif /* PERL_IN_MG_C */
466
467 #endif /* PERL_FEATURE_H_ */
468 EOJ
469
470 read_only_bottom_close_and_rename($h);
471
472
473 ###########################################################################
474 # Template for feature.pm
475
476 __END__
477 package feature;
478
479 our $VERSION = '1.60';
480
481 FEATURES
482
483 # TODO:
484 # - think about versioned features (use feature switch => 2)
485
486 =head1 NAME
487
488 feature - Perl pragma to enable new features
489
490 =head1 SYNOPSIS
491
492     use feature qw(say switch);
493     given ($foo) {
494         when (1)          { say "\$foo == 1" }
495         when ([2,3])      { say "\$foo == 2 || \$foo == 3" }
496         when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
497         when ($_ > 100)   { say "\$foo > 100" }
498         default           { say "None of the above" }
499     }
500
501     use feature ':5.10'; # loads all features available in perl 5.10
502
503     use v5.10;           # implicitly loads :5.10 feature bundle
504
505 =head1 DESCRIPTION
506
507 It is usually impossible to add new syntax to Perl without breaking
508 some existing programs.  This pragma provides a way to minimize that
509 risk. New syntactic constructs, or new semantic meanings to older
510 constructs, can be enabled by C<use feature 'foo'>, and will be parsed
511 only when the appropriate feature pragma is in scope.  (Nevertheless, the
512 C<CORE::> prefix provides access to all Perl keywords, regardless of this
513 pragma.)
514
515 =head2 Lexical effect
516
517 Like other pragmas (C<use strict>, for example), features have a lexical
518 effect.  C<use feature qw(foo)> will only make the feature "foo" available
519 from that point to the end of the enclosing block.
520
521     {
522         use feature 'say';
523         say "say is available here";
524     }
525     print "But not here.\n";
526
527 =head2 C<no feature>
528
529 Features can also be turned off by using C<no feature "foo">.  This too
530 has lexical effect.
531
532     use feature 'say';
533     say "say is available here";
534     {
535         no feature 'say';
536         print "But not here.\n";
537     }
538     say "Yet it is here.";
539
540 C<no feature> with no features specified will reset to the default group.  To
541 disable I<all> features (an unusual request!) use C<no feature ':all'>.
542
543 =head1 AVAILABLE FEATURES
544
545 =head2 The 'say' feature
546
547 C<use feature 'say'> tells the compiler to enable the Perl 6 style
548 C<say> function.
549
550 See L<perlfunc/say> for details.
551
552 This feature is available starting with Perl 5.10.
553
554 =head2 The 'state' feature
555
556 C<use feature 'state'> tells the compiler to enable C<state>
557 variables.
558
559 See L<perlsub/"Persistent Private Variables"> for details.
560
561 This feature is available starting with Perl 5.10.
562
563 =head2 The 'switch' feature
564
565 B<WARNING>: Because the L<smartmatch operator|perlop/"Smartmatch Operator"> is
566 experimental, Perl will warn when you use this feature, unless you have
567 explicitly disabled the warning:
568
569     no warnings "experimental::smartmatch";
570
571 C<use feature 'switch'> tells the compiler to enable the Perl 6
572 given/when construct.
573
574 See L<perlsyn/"Switch Statements"> for details.
575
576 This feature is available starting with Perl 5.10.
577
578 =head2 The 'unicode_strings' feature
579
580 C<use feature 'unicode_strings'> tells the compiler to use Unicode rules
581 in all string operations executed within its scope (unless they are also
582 within the scope of either C<use locale> or C<use bytes>).  The same applies
583 to all regular expressions compiled within the scope, even if executed outside
584 it.  It does not change the internal representation of strings, but only how
585 they are interpreted.
586
587 C<no feature 'unicode_strings'> tells the compiler to use the traditional
588 Perl rules wherein the native character set rules is used unless it is
589 clear to Perl that Unicode is desired.  This can lead to some surprises
590 when the behavior suddenly changes.  (See
591 L<perlunicode/The "Unicode Bug"> for details.)  For this reason, if you are
592 potentially using Unicode in your program, the
593 C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
594
595 This feature is available starting with Perl 5.12; was almost fully
596 implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>;
597 was extended further in Perl 5.26 to cover L<the range
598 operator|perlop/Range Operators>; and was extended again in Perl 5.28 to
599 cover L<special-cased whitespace splitting|perlfunc/split>.
600
601 =head2 The 'unicode_eval' and 'evalbytes' features
602
603 Together, these two features are intended to replace the legacy string
604 C<eval> function, which behaves problematically in some instances.  They are
605 available starting with Perl 5.16, and are enabled by default by a
606 S<C<use 5.16>> or higher declaration.
607
608 C<unicode_eval> changes the behavior of plain string C<eval> to work more
609 consistently, especially in the Unicode world.  Certain (mis)behaviors
610 couldn't be changed without breaking some things that had come to rely on
611 them, so the feature can be enabled and disabled.  Details are at
612 L<perlfunc/Under the "unicode_eval" feature>.
613
614 C<evalbytes> is like string C<eval>, but operating on a byte stream that is
615 not UTF-8 encoded.  Details are at L<perlfunc/evalbytes EXPR>.  Without a
616 S<C<use feature 'evalbytes'>> nor a S<C<use v5.16>> (or higher) declaration in
617 the current scope, you can still access it by instead writing
618 C<CORE::evalbytes>.
619
620 =head2 The 'current_sub' feature
621
622 This provides the C<__SUB__> token that returns a reference to the current
623 subroutine or C<undef> outside of a subroutine.
624
625 This feature is available starting with Perl 5.16.
626
627 =head2 The 'array_base' feature
628
629 This feature supported the legacy C<$[> variable.  See L<perlvar/$[>.
630 It was on by default but disabled under C<use v5.16> (see
631 L</IMPLICIT LOADING>, below) and unavailable since perl 5.30.
632
633 This feature is available under this name starting with Perl 5.16.  In
634 previous versions, it was simply on all the time, and this pragma knew
635 nothing about it.
636
637 =head2 The 'fc' feature
638
639 C<use feature 'fc'> tells the compiler to enable the C<fc> function,
640 which implements Unicode casefolding.
641
642 See L<perlfunc/fc> for details.
643
644 This feature is available from Perl 5.16 onwards.
645
646 =head2 The 'lexical_subs' feature
647
648 In Perl versions prior to 5.26, this feature enabled
649 declaration of subroutines via C<my sub foo>, C<state sub foo>
650 and C<our sub foo> syntax.  See L<perlsub/Lexical Subroutines> for details.
651
652 This feature is available from Perl 5.18 onwards.  From Perl 5.18 to 5.24,
653 it was classed as experimental, and Perl emitted a warning for its
654 usage, except when explicitly disabled:
655
656   no warnings "experimental::lexical_subs";
657
658 As of Perl 5.26, use of this feature no longer triggers a warning, though
659 the C<experimental::lexical_subs> warning category still exists (for
660 compatibility with code that disables it).  In addition, this syntax is
661 not only no longer experimental, but it is enabled for all Perl code,
662 regardless of what feature declarations are in scope.
663
664 =head2 The 'postderef' and 'postderef_qq' features
665
666 The 'postderef_qq' feature extends the applicability of L<postfix
667 dereference syntax|perlref/Postfix Dereference Syntax> so that postfix array
668 and scalar dereference are available in double-quotish interpolations. For
669 example, it makes the following two statements equivalent:
670
671   my $s = "[@{ $h->{a} }]";
672   my $s = "[$h->{a}->@*]";
673
674 This feature is available from Perl 5.20 onwards. In Perl 5.20 and 5.22, it
675 was classed as experimental, and Perl emitted a warning for its
676 usage, except when explicitly disabled:
677
678   no warnings "experimental::postderef";
679
680 As of Perl 5.24, use of this feature no longer triggers a warning, though
681 the C<experimental::postderef> warning category still exists (for
682 compatibility with code that disables it).
683
684 The 'postderef' feature was used in Perl 5.20 and Perl 5.22 to enable
685 postfix dereference syntax outside double-quotish interpolations. In those
686 versions, using it triggered the C<experimental::postderef> warning in the
687 same way as the 'postderef_qq' feature did. As of Perl 5.24, this syntax is
688 not only no longer experimental, but it is enabled for all Perl code,
689 regardless of what feature declarations are in scope.
690
691 =head2 The 'signatures' feature
692
693 B<WARNING>: This feature is still experimental and the implementation may
694 change in future versions of Perl.  For this reason, Perl will
695 warn when you use the feature, unless you have explicitly disabled the
696 warning:
697
698     no warnings "experimental::signatures";
699
700 This enables unpacking of subroutine arguments into lexical variables
701 by syntax such as
702
703     sub foo ($left, $right) {
704         return $left + $right;
705     }
706
707 See L<perlsub/Signatures> for details.
708
709 This feature is available from Perl 5.20 onwards.
710
711 =head2 The 'refaliasing' feature
712
713 B<WARNING>: This feature is still experimental and the implementation may
714 change in future versions of Perl.  For this reason, Perl will
715 warn when you use the feature, unless you have explicitly disabled the
716 warning:
717
718     no warnings "experimental::refaliasing";
719
720 This enables aliasing via assignment to references:
721
722     \$a = \$b; # $a and $b now point to the same scalar
723     \@a = \@b; #                     to the same array
724     \%a = \%b;
725     \&a = \&b;
726     foreach \%hash (@array_of_hash_refs) {
727         ...
728     }
729
730 See L<perlref/Assigning to References> for details.
731
732 This feature is available from Perl 5.22 onwards.
733
734 =head2 The 'bitwise' feature
735
736 This makes the four standard bitwise operators (C<& | ^ ~>) treat their
737 operands consistently as numbers, and introduces four new dotted operators
738 (C<&. |. ^. ~.>) that treat their operands consistently as strings.  The
739 same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).
740
741 See L<perlop/Bitwise String Operators> for details.
742
743 This feature is available from Perl 5.22 onwards.  Starting in Perl 5.28,
744 C<use v5.28> will enable the feature.  Before 5.28, it was still
745 experimental and would emit a warning in the "experimental::bitwise"
746 category.
747
748 =head2 The 'declared_refs' feature
749
750 B<WARNING>: This feature is still experimental and the implementation may
751 change in future versions of Perl.  For this reason, Perl will
752 warn when you use the feature, unless you have explicitly disabled the
753 warning:
754
755     no warnings "experimental::declared_refs";
756
757 This allows a reference to a variable to be declared with C<my>, C<state>,
758 our C<our>, or localized with C<local>.  It is intended mainly for use in
759 conjunction with the "refaliasing" feature.  See L<perlref/Declaring a
760 Reference to a Variable> for examples.
761
762 This feature is available from Perl 5.26 onwards.
763
764 =head2 The 'isa' feature
765
766 This allows the use of the C<isa> infix operator, which tests whether the
767 scalar given by the left operand is an object of the class given by the
768 right operand. See L<perlop/Class Instance Operator> for more details.
769
770 This feature is available from Perl 5.32 onwards.
771
772 =head2 The 'indirect' feature
773
774 This feature allows the use of L<indirect object
775 syntax|perlobj/Indirect Object Syntax> for method calls, e.g.  C<new
776 Foo 1, 2;>. It is enabled by default, but can be turned off to
777 disallow indirect object syntax.
778
779 This feature is available under this name from Perl 5.32 onwards. In
780 previous versions, it was simply on all the time.  To disallow (or
781 warn on) indirect object syntax on older Perls, see the L<indirect>
782 CPAN module.
783
784 =head2 The 'multidimensional' feature
785
786 This feature enables multidimensional array emulation, a perl 4 (or
787 earlier) feature that was used to emulate multidimensional arrays with
788 hashes.  This works by converting code like C<< $foo{$x, y} >> into
789 C<< $foo{join($;, $x, $y} >>.  It is enabled by default, but can be
790 turned off to disable multidimensional array emulation.
791
792 When this feature is disabled the syntax that is normally replaced
793 will report a compilation error.
794
795 This feature is available under this name from Perl 5.34 onwards. In
796 previous versions, it was simply on all the time.
797
798 You can use the L<multidimensional> module on CPAN to disable
799 multidimensional array emulation for older versions of Perl.
800
801 =head1 FEATURE BUNDLES
802
803 It's possible to load multiple features together, using
804 a I<feature bundle>.  The name of a feature bundle is prefixed with
805 a colon, to distinguish it from an actual feature.
806
807   use feature ":5.10";
808
809 The following feature bundles are available:
810
811   bundle    features included
812   --------- -----------------
813 PODTURES
814 The C<:default> bundle represents the feature set that is enabled before
815 any C<use feature> or C<no feature> declaration.
816
817 Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
818 no effect.  Feature bundles are guaranteed to be the same for all sub-versions.
819
820   use feature ":5.14.0";    # same as ":5.14"
821   use feature ":5.14.1";    # same as ":5.14"
822
823 =head1 IMPLICIT LOADING
824
825 Instead of loading feature bundles by name, it is easier to let Perl do
826 implicit loading of a feature bundle for you.
827
828 There are two ways to load the C<feature> pragma implicitly:
829
830 =over 4
831
832 =item *
833
834 By using the C<-E> switch on the Perl command-line instead of C<-e>.
835 That will enable the feature bundle for that version of Perl in the
836 main compilation unit (that is, the one-liner that follows C<-E>).
837
838 =item *
839
840 By explicitly requiring a minimum Perl version number for your program, with
841 the C<use VERSION> construct.  That is,
842
843     use v5.10.0;
844
845 will do an implicit
846
847     no feature ':all';
848     use feature ':5.10';
849
850 and so on.  Note how the trailing sub-version
851 is automatically stripped from the
852 version.
853
854 But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
855
856     use 5.010;
857
858 with the same effect.
859
860 If the required version is older than Perl 5.10, the ":default" feature
861 bundle is automatically loaded instead.
862
863 Unlike C<use feature ":5.12">, saying C<use v5.12> (or any higher version)
864 also does the equivalent of C<use strict>; see L<perlfunc/use> for details.
865
866 =back
867
868 =cut
869
870 sub import {
871     shift;
872
873     if (!@_) {
874         croak("No features specified");
875     }
876
877     __common(1, @_);
878 }
879
880 sub unimport {
881     shift;
882
883     # A bare C<no feature> should reset to the default bundle
884     if (!@_) {
885         $^H &= ~($hint_uni8bit|$hint_mask);
886         return;
887     }
888
889     __common(0, @_);
890 }
891
892
893 sub __common {
894     my $import = shift;
895     my $bundle_number = $^H & $hint_mask;
896     my $features = $bundle_number != $hint_mask
897       && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
898     if ($features) {
899         # Features are enabled implicitly via bundle hints.
900         # Delete any keys that may be left over from last time.
901         delete @^H{ values(%feature) };
902         $^H |= $hint_mask;
903         for (@$features) {
904             $^H{$feature{$_}} = 1;
905             $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
906         }
907     }
908     while (@_) {
909         my $name = shift;
910         if (substr($name, 0, 1) eq ":") {
911             my $v = substr($name, 1);
912             if (!exists $feature_bundle{$v}) {
913                 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
914                 if (!exists $feature_bundle{$v}) {
915                     unknown_feature_bundle(substr($name, 1));
916                 }
917             }
918             unshift @_, @{$feature_bundle{$v}};
919             next;
920         }
921         if (!exists $feature{$name}) {
922             if (exists $noops{$name}) {
923                 next;
924             }
925             if (!$import && exists $removed{$name}) {
926                 next;
927             }
928             unknown_feature($name);
929         }
930         if ($import) {
931             $^H{$feature{$name}} = 1;
932             $^H |= $hint_uni8bit if $name eq 'unicode_strings';
933         } else {
934             delete $^H{$feature{$name}};
935             $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
936         }
937     }
938 }
939
940 sub unknown_feature {
941     my $feature = shift;
942     croak(sprintf('Feature "%s" is not supported by Perl %vd',
943             $feature, $^V));
944 }
945
946 sub unknown_feature_bundle {
947     my $feature = shift;
948     croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
949             $feature, $^V));
950 }
951
952 sub croak {
953     require Carp;
954     Carp::croak(@_);
955 }
956
957 1;