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