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