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