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