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