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