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