This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
feature.pl: bump version number to 1.67
[perl5.git] / lib / feature.pm
1 # -*- buffer-read-only: t -*-
2 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
3 # This file is built by regen/feature.pl.
4 # Any changes made here will be lost!
5
6 package feature;
7
8 our $VERSION = '1.67';
9
10 our %feature = (
11     fc                   => 'feature_fc',
12     isa                  => 'feature_isa',
13     say                  => 'feature_say',
14     try                  => 'feature_try',
15     state                => 'feature_state',
16     switch               => 'feature_switch',
17     bitwise              => 'feature_bitwise',
18     indirect             => 'feature_indirect',
19     evalbytes            => 'feature_evalbytes',
20     signatures           => 'feature_signatures',
21     current_sub          => 'feature___SUB__',
22     refaliasing          => 'feature_refaliasing',
23     postderef_qq         => 'feature_postderef_qq',
24     unicode_eval         => 'feature_unieval',
25     declared_refs        => 'feature_myref',
26     unicode_strings      => 'feature_unicode',
27     multidimensional     => 'feature_multidimensional',
28     bareword_filehandles => 'feature_bareword_filehandles',
29 );
30
31 our %feature_bundle = (
32     "5.10"    => [qw(bareword_filehandles indirect multidimensional say state switch)],
33     "5.11"    => [qw(bareword_filehandles indirect multidimensional say state switch unicode_strings)],
34     "5.15"    => [qw(bareword_filehandles current_sub evalbytes fc indirect multidimensional say state switch unicode_eval unicode_strings)],
35     "5.23"    => [qw(bareword_filehandles current_sub evalbytes fc indirect multidimensional postderef_qq say state switch unicode_eval unicode_strings)],
36     "5.27"    => [qw(bareword_filehandles bitwise current_sub evalbytes fc indirect multidimensional postderef_qq say state switch unicode_eval unicode_strings)],
37     "5.35"    => [qw(bareword_filehandles bitwise current_sub evalbytes fc indirect multidimensional postderef_qq say state unicode_eval unicode_strings)],
38     "all"     => [qw(bareword_filehandles bitwise current_sub declared_refs evalbytes fc indirect isa multidimensional postderef_qq refaliasing say signatures state switch try unicode_eval unicode_strings)],
39     "default" => [qw(bareword_filehandles indirect multidimensional)],
40 );
41
42 $feature_bundle{"5.12"} = $feature_bundle{"5.11"};
43 $feature_bundle{"5.13"} = $feature_bundle{"5.11"};
44 $feature_bundle{"5.14"} = $feature_bundle{"5.11"};
45 $feature_bundle{"5.16"} = $feature_bundle{"5.15"};
46 $feature_bundle{"5.17"} = $feature_bundle{"5.15"};
47 $feature_bundle{"5.18"} = $feature_bundle{"5.15"};
48 $feature_bundle{"5.19"} = $feature_bundle{"5.15"};
49 $feature_bundle{"5.20"} = $feature_bundle{"5.15"};
50 $feature_bundle{"5.21"} = $feature_bundle{"5.15"};
51 $feature_bundle{"5.22"} = $feature_bundle{"5.15"};
52 $feature_bundle{"5.24"} = $feature_bundle{"5.23"};
53 $feature_bundle{"5.25"} = $feature_bundle{"5.23"};
54 $feature_bundle{"5.26"} = $feature_bundle{"5.23"};
55 $feature_bundle{"5.28"} = $feature_bundle{"5.27"};
56 $feature_bundle{"5.29"} = $feature_bundle{"5.27"};
57 $feature_bundle{"5.30"} = $feature_bundle{"5.27"};
58 $feature_bundle{"5.31"} = $feature_bundle{"5.27"};
59 $feature_bundle{"5.32"} = $feature_bundle{"5.27"};
60 $feature_bundle{"5.33"} = $feature_bundle{"5.27"};
61 $feature_bundle{"5.34"} = $feature_bundle{"5.27"};
62 $feature_bundle{"5.36"} = $feature_bundle{"5.35"};
63 $feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
64 my %noops = (
65     postderef => 1,
66     lexical_subs => 1,
67 );
68 my %removed = (
69     array_base => 1,
70 );
71
72 our $hint_shift   = 26;
73 our $hint_mask    = 0x3c000000;
74 our @hint_bundles = qw( default 5.10 5.11 5.15 5.23 5.27 5.35 );
75
76 # This gets set (for now) in $^H as well as in %^H,
77 # for runtime speed of the uc/lc/ucfirst/lcfirst functions.
78 # See HINT_UNI_8_BIT in perl.h.
79 our $hint_uni8bit = 0x00000800;
80
81 # TODO:
82 # - think about versioned features (use feature switch => 2)
83
84 =head1 NAME
85
86 feature - Perl pragma to enable new features
87
88 =head1 SYNOPSIS
89
90     use feature qw(fc say);
91
92     # Without the "use feature" above, this code would not be able to find
93     # the built-ins "say" or "fc":
94     say "The case-folded version of $x is: " . fc $x;
95
96
97     # set features to match the :5.10 bundle, which may turn off or on
98     # multiple features (see below)
99     use feature ':5.10';
100
101
102     # implicitly loads :5.10 feature bundle
103     use v5.10;
104
105 =head1 DESCRIPTION
106
107 It is usually impossible to add new syntax to Perl without breaking
108 some existing programs.  This pragma provides a way to minimize that
109 risk. New syntactic constructs, or new semantic meanings to older
110 constructs, can be enabled by C<use feature 'foo'>, and will be parsed
111 only when the appropriate feature pragma is in scope.  (Nevertheless, the
112 C<CORE::> prefix provides access to all Perl keywords, regardless of this
113 pragma.)
114
115 =head2 Lexical effect
116
117 Like other pragmas (C<use strict>, for example), features have a lexical
118 effect.  C<use feature qw(foo)> will only make the feature "foo" available
119 from that point to the end of the enclosing block.
120
121     {
122         use feature 'say';
123         say "say is available here";
124     }
125     print "But not here.\n";
126
127 =head2 C<no feature>
128
129 Features can also be turned off by using C<no feature "foo">.  This too
130 has lexical effect.
131
132     use feature 'say';
133     say "say is available here";
134     {
135         no feature 'say';
136         print "But not here.\n";
137     }
138     say "Yet it is here.";
139
140 C<no feature> with no features specified will reset to the default group.  To
141 disable I<all> features (an unusual request!) use C<no feature ':all'>.
142
143 =head1 AVAILABLE FEATURES
144
145 =head2 The 'say' feature
146
147 C<use feature 'say'> tells the compiler to enable the Raku-inspired
148 C<say> function.
149
150 See L<perlfunc/say> for details.
151
152 This feature is available starting with Perl 5.10.
153
154 =head2 The 'state' feature
155
156 C<use feature 'state'> tells the compiler to enable C<state>
157 variables.
158
159 See L<perlsub/"Persistent Private Variables"> for details.
160
161 This feature is available starting with Perl 5.10.
162
163 =head2 The 'switch' feature
164
165 B<WARNING>: This feature is still experimental and the implementation may
166 change or be removed in future versions of Perl.  For this reason, Perl will
167 warn when you use the feature, unless you have explicitly disabled the warning:
168
169     no warnings "experimental::smartmatch";
170
171 C<use feature 'switch'> tells the compiler to enable the Raku
172 given/when construct.
173
174 See L<perlsyn/"Switch Statements"> for details.
175
176 This feature is available starting with Perl 5.10.
177
178 =head2 The 'unicode_strings' feature
179
180 C<use feature 'unicode_strings'> tells the compiler to use Unicode rules
181 in all string operations executed within its scope (unless they are also
182 within the scope of either C<use locale> or C<use bytes>).  The same applies
183 to all regular expressions compiled within the scope, even if executed outside
184 it.  It does not change the internal representation of strings, but only how
185 they are interpreted.
186
187 C<no feature 'unicode_strings'> tells the compiler to use the traditional
188 Perl rules wherein the native character set rules is used unless it is
189 clear to Perl that Unicode is desired.  This can lead to some surprises
190 when the behavior suddenly changes.  (See
191 L<perlunicode/The "Unicode Bug"> for details.)  For this reason, if you are
192 potentially using Unicode in your program, the
193 C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
194
195 This feature is available starting with Perl 5.12; was almost fully
196 implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>;
197 was extended further in Perl 5.26 to cover L<the range
198 operator|perlop/Range Operators>; and was extended again in Perl 5.28 to
199 cover L<special-cased whitespace splitting|perlfunc/split>.
200
201 =head2 The 'unicode_eval' and 'evalbytes' features
202
203 Together, these two features are intended to replace the legacy string
204 C<eval> function, which behaves problematically in some instances.  They are
205 available starting with Perl 5.16, and are enabled by default by a
206 S<C<use 5.16>> or higher declaration.
207
208 C<unicode_eval> changes the behavior of plain string C<eval> to work more
209 consistently, especially in the Unicode world.  Certain (mis)behaviors
210 couldn't be changed without breaking some things that had come to rely on
211 them, so the feature can be enabled and disabled.  Details are at
212 L<perlfunc/Under the "unicode_eval" feature>.
213
214 C<evalbytes> is like string C<eval>, but it treats its argument as a byte
215 string. Details are at L<perlfunc/evalbytes EXPR>.  Without a
216 S<C<use feature 'evalbytes'>> nor a S<C<use v5.16>> (or higher) declaration in
217 the current scope, you can still access it by instead writing
218 C<CORE::evalbytes>.
219
220 =head2 The 'current_sub' feature
221
222 This provides the C<__SUB__> token that returns a reference to the current
223 subroutine or C<undef> outside of a subroutine.
224
225 This feature is available starting with Perl 5.16.
226
227 =head2 The 'array_base' feature
228
229 This feature supported the legacy C<$[> variable.  See L<perlvar/$[>.
230 It was on by default but disabled under C<use v5.16> (see
231 L</IMPLICIT LOADING>, below) and unavailable since perl 5.30.
232
233 This feature is available under this name starting with Perl 5.16.  In
234 previous versions, it was simply on all the time, and this pragma knew
235 nothing about it.
236
237 =head2 The 'fc' feature
238
239 C<use feature 'fc'> tells the compiler to enable the C<fc> function,
240 which implements Unicode casefolding.
241
242 See L<perlfunc/fc> for details.
243
244 This feature is available from Perl 5.16 onwards.
245
246 =head2 The 'lexical_subs' feature
247
248 In Perl versions prior to 5.26, this feature enabled
249 declaration of subroutines via C<my sub foo>, C<state sub foo>
250 and C<our sub foo> syntax.  See L<perlsub/Lexical Subroutines> for details.
251
252 This feature is available from Perl 5.18 onwards.  From Perl 5.18 to 5.24,
253 it was classed as experimental, and Perl emitted a warning for its
254 usage, except when explicitly disabled:
255
256   no warnings "experimental::lexical_subs";
257
258 As of Perl 5.26, use of this feature no longer triggers a warning, though
259 the C<experimental::lexical_subs> warning category still exists (for
260 compatibility with code that disables it).  In addition, this syntax is
261 not only no longer experimental, but it is enabled for all Perl code,
262 regardless of what feature declarations are in scope.
263
264 =head2 The 'postderef' and 'postderef_qq' features
265
266 The 'postderef_qq' feature extends the applicability of L<postfix
267 dereference syntax|perlref/Postfix Dereference Syntax> so that postfix array
268 and scalar dereference are available in double-quotish interpolations. For
269 example, it makes the following two statements equivalent:
270
271   my $s = "[@{ $h->{a} }]";
272   my $s = "[$h->{a}->@*]";
273
274 This feature is available from Perl 5.20 onwards. In Perl 5.20 and 5.22, it
275 was classed as experimental, and Perl emitted a warning for its
276 usage, except when explicitly disabled:
277
278   no warnings "experimental::postderef";
279
280 As of Perl 5.24, use of this feature no longer triggers a warning, though
281 the C<experimental::postderef> warning category still exists (for
282 compatibility with code that disables it).
283
284 The 'postderef' feature was used in Perl 5.20 and Perl 5.22 to enable
285 postfix dereference syntax outside double-quotish interpolations. In those
286 versions, using it triggered the C<experimental::postderef> warning in the
287 same way as the 'postderef_qq' feature did. As of Perl 5.24, this syntax is
288 not only no longer experimental, but it is enabled for all Perl code,
289 regardless of what feature declarations are in scope.
290
291 =head2 The 'signatures' feature
292
293 B<WARNING>: This feature is still experimental and the implementation may
294 change or be removed in future versions of Perl.  For this reason, Perl will
295 warn when you use the feature, unless you have explicitly disabled the warning:
296
297     no warnings "experimental::signatures";
298
299 This enables unpacking of subroutine arguments into lexical variables
300 by syntax such as
301
302     sub foo ($left, $right) {
303         return $left + $right;
304     }
305
306 See L<perlsub/Signatures> for details.
307
308 This feature is available from Perl 5.20 onwards.
309
310 =head2 The 'refaliasing' feature
311
312 B<WARNING>: This feature is still experimental and the implementation may
313 change or be removed in future versions of Perl.  For this reason, Perl will
314 warn when you use the feature, unless you have explicitly disabled the warning:
315
316     no warnings "experimental::refaliasing";
317
318 This enables aliasing via assignment to references:
319
320     \$a = \$b; # $a and $b now point to the same scalar
321     \@a = \@b; #                     to the same array
322     \%a = \%b;
323     \&a = \&b;
324     foreach \%hash (@array_of_hash_refs) {
325         ...
326     }
327
328 See L<perlref/Assigning to References> for details.
329
330 This feature is available from Perl 5.22 onwards.
331
332 =head2 The 'bitwise' feature
333
334 This makes the four standard bitwise operators (C<& | ^ ~>) treat their
335 operands consistently as numbers, and introduces four new dotted operators
336 (C<&. |. ^. ~.>) that treat their operands consistently as strings.  The
337 same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).
338
339 See L<perlop/Bitwise String Operators> for details.
340
341 This feature is available from Perl 5.22 onwards.  Starting in Perl 5.28,
342 C<use v5.28> will enable the feature.  Before 5.28, it was still
343 experimental and would emit a warning in the "experimental::bitwise"
344 category.
345
346 =head2 The 'declared_refs' feature
347
348 B<WARNING>: This feature is still experimental and the implementation may
349 change or be removed in future versions of Perl.  For this reason, Perl will
350 warn when you use the feature, unless you have explicitly disabled the warning:
351
352     no warnings "experimental::declared_refs";
353
354 This allows a reference to a variable to be declared with C<my>, C<state>,
355 our C<our>, or localized with C<local>.  It is intended mainly for use in
356 conjunction with the "refaliasing" feature.  See L<perlref/Declaring a
357 Reference to a Variable> for examples.
358
359 This feature is available from Perl 5.26 onwards.
360
361 =head2 The 'isa' feature
362
363 B<WARNING>: This feature is still experimental and the implementation may
364 change or be removed in future versions of Perl.  For this reason, Perl will
365 warn when you use the feature, unless you have explicitly disabled the warning:
366
367     no warnings "experimental::isa";
368
369 This allows the use of the C<isa> infix operator, which tests whether the
370 scalar given by the left operand is an object of the class given by the
371 right operand. See L<perlop/Class Instance Operator> for more details.
372
373 This feature is available from Perl 5.32 onwards.
374
375 =head2 The 'indirect' feature
376
377 This feature allows the use of L<indirect object
378 syntax|perlobj/Indirect Object Syntax> for method calls, e.g.  C<new
379 Foo 1, 2;>. It is enabled by default, but can be turned off to
380 disallow indirect object syntax.
381
382 This feature is available under this name from Perl 5.32 onwards. In
383 previous versions, it was simply on all the time.  To disallow (or
384 warn on) indirect object syntax on older Perls, see the L<indirect>
385 CPAN module.
386
387 =head2 The 'multidimensional' feature
388
389 This feature enables multidimensional array emulation, a perl 4 (or
390 earlier) feature that was used to emulate multidimensional arrays with
391 hashes.  This works by converting code like C<< $foo{$x, $y} >> into
392 C<< $foo{join($;, $x, $y)} >>.  It is enabled by default, but can be
393 turned off to disable multidimensional array emulation.
394
395 When this feature is disabled the syntax that is normally replaced
396 will report a compilation error.
397
398 This feature is available under this name from Perl 5.34 onwards. In
399 previous versions, it was simply on all the time.
400
401 You can use the L<multidimensional> module on CPAN to disable
402 multidimensional array emulation for older versions of Perl.
403
404 =head2 The 'bareword_filehandles' feature.
405
406 This feature enables bareword filehandles for builtin functions
407 operations, a generally discouraged practice.  It is enabled by
408 default, but can be turned off to disable bareword filehandles, except
409 for the exceptions listed below.
410
411 The perl built-in filehandles C<STDIN>, C<STDOUT>, C<STDERR>, C<DATA>,
412 C<ARGV>, C<ARGVOUT> and the special C<_> are always enabled.
413
414 This feature is enabled under this name from Perl 5.34 onwards.  In
415 previous versions it was simply on all the time.
416
417 You can use the L<bareword::filehandles> module on CPAN to disable
418 bareword filehandles for older versions of perl.
419
420 =head2 The 'try' feature.
421
422 B<WARNING>: This feature is still experimental and the implementation may
423 change or be removed in future versions of Perl.  For this reason, Perl will
424 warn when you use the feature, unless you have explicitly disabled the warning:
425
426     no warnings "experimental::try";
427
428 This feature enables the C<try> and C<catch> syntax, which allows exception
429 handling, where exceptions thrown from the body of the block introduced with
430 C<try> are caught by executing the body of the C<catch> block.
431
432 For more information, see L<perlsyn/"Try Catch Exception Handling">.
433
434 =head1 FEATURE BUNDLES
435
436 It's possible to load multiple features together, using
437 a I<feature bundle>.  The name of a feature bundle is prefixed with
438 a colon, to distinguish it from an actual feature.
439
440   use feature ":5.10";
441
442 The following feature bundles are available:
443
444   bundle    features included
445   --------- -----------------
446   :default  indirect multidimensional
447             bareword_filehandles
448
449   :5.10     bareword_filehandles indirect
450             multidimensional say state switch
451
452   :5.12     bareword_filehandles indirect
453             multidimensional say state switch
454             unicode_strings
455
456   :5.14     bareword_filehandles indirect
457             multidimensional say state switch
458             unicode_strings
459
460   :5.16     bareword_filehandles current_sub evalbytes
461             fc indirect multidimensional say state
462             switch unicode_eval unicode_strings
463
464   :5.18     bareword_filehandles current_sub evalbytes
465             fc indirect multidimensional say state
466             switch unicode_eval unicode_strings
467
468   :5.20     bareword_filehandles current_sub evalbytes
469             fc indirect multidimensional say state
470             switch unicode_eval unicode_strings
471
472   :5.22     bareword_filehandles current_sub evalbytes
473             fc indirect multidimensional say state
474             switch unicode_eval unicode_strings
475
476   :5.24     bareword_filehandles current_sub evalbytes
477             fc indirect multidimensional postderef_qq
478             say state switch unicode_eval
479             unicode_strings
480
481   :5.26     bareword_filehandles current_sub evalbytes
482             fc indirect multidimensional postderef_qq
483             say state switch unicode_eval
484             unicode_strings
485
486   :5.28     bareword_filehandles bitwise current_sub
487             evalbytes fc indirect multidimensional
488             postderef_qq say state switch unicode_eval
489             unicode_strings
490
491   :5.30     bareword_filehandles bitwise current_sub
492             evalbytes fc indirect multidimensional
493             postderef_qq say state switch unicode_eval
494             unicode_strings
495
496   :5.32     bareword_filehandles bitwise current_sub
497             evalbytes fc indirect multidimensional
498             postderef_qq say state switch unicode_eval
499             unicode_strings
500
501   :5.34     bareword_filehandles bitwise current_sub
502             evalbytes fc indirect multidimensional
503             postderef_qq say state switch unicode_eval
504             unicode_strings
505
506   :5.36     bareword_filehandles bitwise current_sub
507             evalbytes fc indirect multidimensional
508             postderef_qq say state unicode_eval
509             unicode_strings
510
511 The C<:default> bundle represents the feature set that is enabled before
512 any C<use feature> or C<no feature> declaration.
513
514 Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
515 no effect.  Feature bundles are guaranteed to be the same for all sub-versions.
516
517   use feature ":5.14.0";    # same as ":5.14"
518   use feature ":5.14.1";    # same as ":5.14"
519
520 =head1 IMPLICIT LOADING
521
522 Instead of loading feature bundles by name, it is easier to let Perl do
523 implicit loading of a feature bundle for you.
524
525 There are two ways to load the C<feature> pragma implicitly:
526
527 =over 4
528
529 =item *
530
531 By using the C<-E> switch on the Perl command-line instead of C<-e>.
532 That will enable the feature bundle for that version of Perl in the
533 main compilation unit (that is, the one-liner that follows C<-E>).
534
535 =item *
536
537 By explicitly requiring a minimum Perl version number for your program, with
538 the C<use VERSION> construct.  That is,
539
540     use v5.10.0;
541
542 will do an implicit
543
544     no feature ':all';
545     use feature ':5.10';
546
547 and so on.  Note how the trailing sub-version
548 is automatically stripped from the
549 version.
550
551 But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
552
553     use 5.010;
554
555 with the same effect.
556
557 If the required version is older than Perl 5.10, the ":default" feature
558 bundle is automatically loaded instead.
559
560 Unlike C<use feature ":5.12">, saying C<use v5.12> (or any higher version)
561 also does the equivalent of C<use strict>; see L<perlfunc/use> for details.
562
563 =back
564
565 =cut
566
567 sub import {
568     shift;
569
570     if (!@_) {
571         croak("No features specified");
572     }
573
574     __common(1, @_);
575 }
576
577 sub unimport {
578     shift;
579
580     # A bare C<no feature> should reset to the default bundle
581     if (!@_) {
582         $^H &= ~($hint_uni8bit|$hint_mask);
583         return;
584     }
585
586     __common(0, @_);
587 }
588
589
590 sub __common {
591     my $import = shift;
592     my $bundle_number = $^H & $hint_mask;
593     my $features = $bundle_number != $hint_mask
594       && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
595     if ($features) {
596         # Features are enabled implicitly via bundle hints.
597         # Delete any keys that may be left over from last time.
598         delete @^H{ values(%feature) };
599         $^H |= $hint_mask;
600         for (@$features) {
601             $^H{$feature{$_}} = 1;
602             $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
603         }
604     }
605     while (@_) {
606         my $name = shift;
607         if (substr($name, 0, 1) eq ":") {
608             my $v = substr($name, 1);
609             if (!exists $feature_bundle{$v}) {
610                 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
611                 if (!exists $feature_bundle{$v}) {
612                     unknown_feature_bundle(substr($name, 1));
613                 }
614             }
615             unshift @_, @{$feature_bundle{$v}};
616             next;
617         }
618         if (!exists $feature{$name}) {
619             if (exists $noops{$name}) {
620                 next;
621             }
622             if (!$import && exists $removed{$name}) {
623                 next;
624             }
625             unknown_feature($name);
626         }
627         if ($import) {
628             $^H{$feature{$name}} = 1;
629             $^H |= $hint_uni8bit if $name eq 'unicode_strings';
630         } else {
631             delete $^H{$feature{$name}};
632             $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
633         }
634     }
635 }
636
637 sub unknown_feature {
638     my $feature = shift;
639     croak(sprintf('Feature "%s" is not supported by Perl %vd',
640             $feature, $^V));
641 }
642
643 sub unknown_feature_bundle {
644     my $feature = shift;
645     croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
646             $feature, $^V));
647 }
648
649 sub croak {
650     require Carp;
651     Carp::croak(@_);
652 }
653
654 1;
655
656 # ex: set ro: