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