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