This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge branch to not require mathoms special cases into blead
[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 {
14 require 'regen/regen_lib.pl';
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',
b9a36c95 30 postderef => 'postderef',
3fff3427 31 array_base => 'arybase',
67bdaa9e 32 current_sub => '__SUB__',
baabe3fb 33 refaliasing => 'refaliasing',
ebd25686 34 lexical_subs => 'lexsubs',
158becca 35 postderef_qq => 'postderef_qq',
67bdaa9e
FC
36 unicode_eval => 'unieval',
37 unicode_strings => 'unicode',
2a4315f8 38 fc => 'fc',
30d9c59b 39 signatures => 'signatures',
69bcf1d3
FC
40);
41
40e4d872
FC
42# NOTE: If a feature is ever enabled in a non-contiguous range of Perl
43# versions, any code below that uses %BundleRanges will have to
44# be changed to account.
45
52fc5c56 46# 5.odd implies the next 5.even, but an explicit 5.even can override it.
69bcf1d3 47my %feature_bundle = (
39ec54a5 48 all => [ keys %feature ],
3fff3427 49 default => [qw(array_base)],
69bcf1d3
FC
50 "5.9.5" => [qw(say state switch array_base)],
51 "5.10" => [qw(say state switch array_base)],
52 "5.11" => [qw(say state switch unicode_strings array_base)],
69bcf1d3 53 "5.13" => [qw(say state switch unicode_strings array_base)],
69bcf1d3 54 "5.15" => [qw(say state switch unicode_strings unicode_eval
2a4315f8 55 evalbytes current_sub fc)],
d6402ebe
RS
56 "5.17" => [qw(say state switch unicode_strings unicode_eval
57 evalbytes current_sub fc)],
d09258e7
RS
58 "5.19" => [qw(say state switch unicode_strings unicode_eval
59 evalbytes current_sub fc)],
b530a4ea
RS
60 "5.21" => [qw(say state switch unicode_strings unicode_eval
61 evalbytes current_sub fc)],
6031f0d2 62 "5.23" => [qw(say state switch unicode_strings unicode_eval
1c2511e0 63 evalbytes current_sub fc postderef_qq)],
2c5484a6
RS
64 "5.25" => [qw(say state switch unicode_strings unicode_eval
65 evalbytes current_sub fc postderef_qq)],
69bcf1d3
FC
66);
67
64fbf0dd 68# not actually used currently
ebd25686
FC
69my @experimental = qw( 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
FC
111
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
64fbf0dd
FC
192#print $pm "my \%experimental = (\n";
193#print $pm " $_ => 1,\n", for @experimental;
194#print $pm ");\n";
ebd25686 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
2c5484a6 372our $VERSION = '1.43';
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
KW
488This feature is available starting with Perl 5.12; was almost fully
489implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>.
69bcf1d3
FC
490
491=head2 The 'unicode_eval' and 'evalbytes' features
492
493Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a
494string, will evaluate it as a string of characters, ignoring any
495C<use utf8> declarations. C<use utf8> exists to declare the encoding of
496the script, which only makes sense for a stream of bytes, not a string of
497characters. Source filters are forbidden, as they also really only make
498sense on strings of bytes. Any attempt to activate a source filter will
499result in an error.
500
501The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates
502the argument passed to it as a string of bytes. It dies if the string
503contains any characters outside the 8-bit range. Source filters work
504within C<evalbytes>: they apply to the contents of the string being
505evaluated.
506
507Together, these two features are intended to replace the historical C<eval>
508function, which has (at least) two bugs in it, that cannot easily be fixed
509without breaking existing programs:
510
511=over
512
513=item *
514
515C<eval> behaves differently depending on the internal encoding of the
516string, sometimes treating its argument as a string of bytes, and sometimes
517as a string of characters.
518
519=item *
520
521Source filters activated within C<eval> leak out into whichever I<file>
522scope is currently being compiled. To give an example with the CPAN module
523L<Semi::Semicolons>:
524
525 BEGIN { eval "use Semi::Semicolons; # not filtered here " }
526 # filtered here!
527
528C<evalbytes> fixes that to work the way one would expect:
529
530 use feature "evalbytes";
531 BEGIN { evalbytes "use Semi::Semicolons; # filtered " }
532 # not filtered
533
534=back
535
536These two features are available starting with Perl 5.16.
537
538=head2 The 'current_sub' feature
539
540This provides the C<__SUB__> token that returns a reference to the current
541subroutine or C<undef> outside of a subroutine.
542
543This feature is available starting with Perl 5.16.
544
545=head2 The 'array_base' feature
546
547This feature supports the legacy C<$[> variable. See L<perlvar/$[> and
548L<arybase>. It is on by default but disabled under C<use v5.16> (see
549L</IMPLICIT LOADING>, below).
550
551This feature is available under this name starting with Perl 5.16. In
552previous versions, it was simply on all the time, and this pragma knew
553nothing about it.
554
2a4315f8
BF
555=head2 The 'fc' feature
556
557C<use feature 'fc'> tells the compiler to enable the C<fc> function,
558which implements Unicode casefolding.
559
560See L<perlfunc/fc> for details.
561
562This feature is available from Perl 5.16 onwards.
563
ca40957e
FC
564=head2 The 'lexical_subs' feature
565
566B<WARNING>: This feature is still experimental and the implementation may
64fbf0dd
FC
567change in future versions of Perl. For this reason, Perl will
568warn when you use the feature, unless you have explicitly disabled the
ca40957e
FC
569warning:
570
f1d34ca8 571 no warnings "experimental::lexical_subs";
ca40957e
FC
572
573This enables declaration of subroutines via C<my sub foo>, C<state sub foo>
574and C<our sub foo> syntax. See L<perlsub/Lexical Subroutines> for details.
575
576This feature is available from Perl 5.18 onwards.
577
f86d720e
RS
578=head2 The 'postderef' and 'postderef_qq' features
579
1c2511e0
AC
580The 'postderef_qq' feature extends the applicability of L<postfix
581dereference syntax|perlref/Postfix Dereference Syntax> so that postfix array
582and scalar dereference are available in double-quotish interpolations. For
583example, it makes the following two statements equivalent:
f86d720e 584
1c2511e0
AC
585 my $s = "[@{ $h->{a} }]";
586 my $s = "[$h->{a}->@*]";
f86d720e 587
1c2511e0
AC
588This feature is available from Perl 5.20 onwards. In Perl 5.20 and 5.22, it
589was classed as experimental, and Perl emitted a warning for its
2ad792cd
AC
590usage, except when explicitly disabled:
591
592 no warnings "experimental::postderef";
593
1c2511e0 594As of Perl 5.24, use of this feature no longer triggers a warning, though
2ad792cd
AC
595the C<experimental::postderef> warning category still exists (for
596compatibility with code that disables it).
f86d720e 597
1c2511e0
AC
598The 'postderef' feature was used in Perl 5.20 and Perl 5.22 to enable
599postfix dereference syntax outside double-quotish interpolations. In those
600versions, using it triggered the C<experimental::postderef> warning in the
601same way as the 'postderef_qq' feature did. As of Perl 5.24, this syntax is
602not only no longer experimental, but it is enabled for all Perl code,
603regardless of what feature declarations are in scope.
604
30d9c59b
Z
605=head2 The 'signatures' feature
606
607B<WARNING>: This feature is still experimental and the implementation may
608change in future versions of Perl. For this reason, Perl will
609warn when you use the feature, unless you have explicitly disabled the
610warning:
611
612 no warnings "experimental::signatures";
613
614This enables unpacking of subroutine arguments into lexical variables
615by syntax such as
616
617 sub foo ($left, $right) {
618 return $left + $right;
619 }
620
621See L<perlsub/Signatures> for details.
622
623This feature is available from Perl 5.20 onwards.
624
baabe3fb 625=head2 The 'refaliasing' feature
82848c10
FC
626
627B<WARNING>: This feature is still experimental and the implementation may
628change in future versions of Perl. For this reason, Perl will
629warn when you use the feature, unless you have explicitly disabled the
630warning:
631
baabe3fb 632 no warnings "experimental::refaliasing";
82848c10
FC
633
634This enables aliasing via assignment to references:
635
636 \$a = \$b; # $a and $b now point to the same scalar
637 \@a = \@b; # to the same array
638 \%a = \%b;
639 \&a = \&b;
640 foreach \%hash (@array_of_hash_refs) {
641 ...
642 }
643
644See L<perlref/Assigning to References> for details.
645
646This feature is available from Perl 5.22 onwards.
647
70ea8edf
FC
648=head2 The 'bitwise' feature
649
650B<WARNING>: This feature is still experimental and the implementation may
651change in future versions of Perl. For this reason, Perl will
652warn when you use the feature, unless you have explicitly disabled the
653warning:
654
655 no warnings "experimental::bitwise";
656
657This makes the four standard bitwise operators (C<& | ^ ~>) treat their
658operands consistently as numbers, and introduces four new dotted operators
659(C<&. |. ^. ~.>) that treat their operands consistently as strings. The
660same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).
661
662See L<perlop/Bitwise String Operators> for details.
663
664This feature is available from Perl 5.22 onwards.
665
69bcf1d3
FC
666=head1 FEATURE BUNDLES
667
668It's possible to load multiple features together, using
669a I<feature bundle>. The name of a feature bundle is prefixed with
670a colon, to distinguish it from an actual feature.
671
672 use feature ":5.10";
673
674The following feature bundles are available:
675
676 bundle features included
677 --------- -----------------
2b3fe414 678PODTURES
69bcf1d3
FC
679The C<:default> bundle represents the feature set that is enabled before
680any C<use feature> or C<no feature> declaration.
681
682Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
683no effect. Feature bundles are guaranteed to be the same for all sub-versions.
684
685 use feature ":5.14.0"; # same as ":5.14"
686 use feature ":5.14.1"; # same as ":5.14"
687
688=head1 IMPLICIT LOADING
689
690Instead of loading feature bundles by name, it is easier to let Perl do
691implicit loading of a feature bundle for you.
692
693There are two ways to load the C<feature> pragma implicitly:
694
695=over 4
696
697=item *
698
699By using the C<-E> switch on the Perl command-line instead of C<-e>.
700That will enable the feature bundle for that version of Perl in the
701main compilation unit (that is, the one-liner that follows C<-E>).
702
703=item *
704
705By explicitly requiring a minimum Perl version number for your program, with
706the C<use VERSION> construct. That is,
707
708 use v5.10.0;
709
710will do an implicit
711
39ec54a5 712 no feature ':all';
69bcf1d3
FC
713 use feature ':5.10';
714
715and so on. Note how the trailing sub-version
716is automatically stripped from the
717version.
718
719But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
720
721 use 5.010;
722
723with the same effect.
724
725If the required version is older than Perl 5.10, the ":default" feature
726bundle is automatically loaded instead.
727
728=back
729
730=cut
731
732sub import {
22055af9 733 shift;
36143a0c
NC
734
735 if (!@_) {
69bcf1d3
FC
736 croak("No features specified");
737 }
36143a0c 738
d3757264 739 __common(1, @_);
69bcf1d3
FC
740}
741
742sub unimport {
22055af9 743 shift;
69bcf1d3 744
39ec54a5 745 # A bare C<no feature> should reset to the default bundle
69bcf1d3 746 if (!@_) {
39ec54a5
RS
747 $^H &= ~($hint_uni8bit|$hint_mask);
748 return;
69bcf1d3
FC
749 }
750
d3757264
NC
751 __common(0, @_);
752}
753
754
755sub __common {
756 my $import = shift;
0c8d5017
NC
757 my $bundle_number = $^H & $hint_mask;
758 my $features = $bundle_number != $hint_mask
759 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
760 if ($features) {
da5b5421 761 # Features are enabled implicitly via bundle hints.
d9ee6ccb
NC
762 # Delete any keys that may be left over from last time.
763 delete @^H{ values(%feature) };
764 $^H |= $hint_mask;
765 for (@$features) {
766 $^H{$feature{$_}} = 1;
767 $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
768 }
da5b5421 769 }
69bcf1d3
FC
770 while (@_) {
771 my $name = shift;
772 if (substr($name, 0, 1) eq ":") {
773 my $v = substr($name, 1);
774 if (!exists $feature_bundle{$v}) {
775 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
776 if (!exists $feature_bundle{$v}) {
777 unknown_feature_bundle(substr($name, 1));
778 }
779 }
780 unshift @_, @{$feature_bundle{$v}};
781 next;
782 }
36143a0c 783 if (!exists $feature{$name}) {
69bcf1d3 784 unknown_feature($name);
69bcf1d3 785 }
d3757264
NC
786 if ($import) {
787 $^H{$feature{$name}} = 1;
788 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
789 } else {
69bcf1d3
FC
790 delete $^H{$feature{$name}};
791 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
792 }
793 }
794}
795
796sub unknown_feature {
797 my $feature = shift;
798 croak(sprintf('Feature "%s" is not supported by Perl %vd',
799 $feature, $^V));
800}
801
802sub unknown_feature_bundle {
803 my $feature = shift;
804 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
805 $feature, $^V));
806}
807
808sub croak {
809 require Carp;
810 Carp::croak(@_);
811}
812
8131;