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