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