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