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