Commit | Line | Data |
---|---|---|
69bcf1d3 FC |
1 | # -*- buffer-read-only: t -*- |
2 | # !!!!!!! DO NOT EDIT THIS FILE !!!!!!! | |
3 | # This file is built by regen/feature.pl. | |
4 | # Any changes made here will be lost! | |
5 | ||
0d863452 RH |
6 | package feature; |
7 | ||
2a0fcf58 | 8 | our $VERSION = '1.34'; |
0d863452 | 9 | |
0bb01b05 | 10 | our %feature = ( |
2a4315f8 | 11 | fc => 'feature_fc', |
47e9d84a EB |
12 | say => 'feature_say', |
13 | state => 'feature_state', | |
1863b879 | 14 | switch => 'feature_switch', |
7d789282 | 15 | evalbytes => 'feature_evalbytes', |
b9a36c95 | 16 | postderef => 'feature_postderef', |
3fff3427 | 17 | array_base => 'feature_arybase', |
84ed0108 | 18 | current_sub => 'feature___SUB__', |
ebd25686 | 19 | lexical_subs => 'feature_lexsubs', |
158becca | 20 | postderef_qq => 'feature_postderef_qq', |
cda6b701 | 21 | unicode_eval => 'feature_unieval', |
47e9d84a | 22 | unicode_strings => 'feature_unicode', |
bc9b29db RH |
23 | ); |
24 | ||
0d2bd2aa | 25 | our %feature_bundle = ( |
69bcf1d3 FC |
26 | "5.10" => [qw(array_base say state switch)], |
27 | "5.11" => [qw(array_base say state switch unicode_strings)], | |
2a4315f8 | 28 | "5.15" => [qw(current_sub evalbytes fc say state switch unicode_eval unicode_strings)], |
158becca | 29 | "all" => [qw(array_base current_sub evalbytes fc lexical_subs postderef postderef_qq say state switch unicode_eval unicode_strings)], |
69bcf1d3 | 30 | "default" => [qw(array_base)], |
0d863452 | 31 | ); |
d052521a | 32 | |
88da30d7 FC |
33 | $feature_bundle{"5.12"} = $feature_bundle{"5.11"}; |
34 | $feature_bundle{"5.13"} = $feature_bundle{"5.11"}; | |
35 | $feature_bundle{"5.14"} = $feature_bundle{"5.11"}; | |
36 | $feature_bundle{"5.16"} = $feature_bundle{"5.15"}; | |
d6402ebe | 37 | $feature_bundle{"5.17"} = $feature_bundle{"5.15"}; |
52fc5c56 | 38 | $feature_bundle{"5.18"} = $feature_bundle{"5.15"}; |
d09258e7 RS |
39 | $feature_bundle{"5.19"} = $feature_bundle{"5.15"}; |
40 | $feature_bundle{"5.20"} = $feature_bundle{"5.15"}; | |
88da30d7 | 41 | $feature_bundle{"5.9.5"} = $feature_bundle{"5.10"}; |
69bcf1d3 | 42 | |
0bb01b05 FC |
43 | our $hint_shift = 26; |
44 | our $hint_mask = 0x1c000000; | |
45 | our @hint_bundles = qw( default 5.10 5.11 5.15 ); | |
ada44f8c | 46 | |
69bcf1d3 FC |
47 | # This gets set (for now) in $^H as well as in %^H, |
48 | # for runtime speed of the uc/lc/ucfirst/lcfirst functions. | |
49 | # See HINT_UNI_8_BIT in perl.h. | |
50 | our $hint_uni8bit = 0x00000800; | |
7dfde25d | 51 | |
0d863452 | 52 | # TODO: |
1c321dc6 | 53 | # - think about versioned features (use feature switch => 2) |
0d863452 RH |
54 | |
55 | =head1 NAME | |
56 | ||
e1b711da | 57 | feature - Perl pragma to enable new features |
0d863452 RH |
58 | |
59 | =head1 SYNOPSIS | |
60 | ||
47e9d84a | 61 | use feature qw(say switch); |
0d863452 | 62 | given ($foo) { |
0b25e784 DG |
63 | when (1) { say "\$foo == 1" } |
64 | when ([2,3]) { say "\$foo == 2 || \$foo == 3" } | |
65 | when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" } | |
66 | when ($_ > 100) { say "\$foo > 100" } | |
67 | default { say "None of the above" } | |
0d863452 RH |
68 | } |
69 | ||
ec488c7f RGS |
70 | use feature ':5.10'; # loads all features available in perl 5.10 |
71 | ||
0b25e784 DG |
72 | use v5.10; # implicitly loads :5.10 feature bundle |
73 | ||
0d863452 RH |
74 | =head1 DESCRIPTION |
75 | ||
76 | It is usually impossible to add new syntax to Perl without breaking | |
b22bbcf0 | 77 | some existing programs. This pragma provides a way to minimize that |
1863b879 RGS |
78 | risk. New syntactic constructs, or new semantic meanings to older |
79 | constructs, can be enabled by C<use feature 'foo'>, and will be parsed | |
b22bbcf0 | 80 | only when the appropriate feature pragma is in scope. (Nevertheless, the |
4a904372 FC |
81 | C<CORE::> prefix provides access to all Perl keywords, regardless of this |
82 | pragma.) | |
0d863452 | 83 | |
9eb27be9 RGS |
84 | =head2 Lexical effect |
85 | ||
86 | Like other pragmas (C<use strict>, for example), features have a lexical | |
301381dc | 87 | effect. C<use feature qw(foo)> will only make the feature "foo" available |
9eb27be9 RGS |
88 | from that point to the end of the enclosing block. |
89 | ||
90 | { | |
91 | use feature 'say'; | |
92 | say "say is available here"; | |
93 | } | |
94 | print "But not here.\n"; | |
95 | ||
5e36ed56 RGS |
96 | =head2 C<no feature> |
97 | ||
b22bbcf0 | 98 | Features can also be turned off by using C<no feature "foo">. This too |
5e36ed56 RGS |
99 | has lexical effect. |
100 | ||
101 | use feature 'say'; | |
102 | say "say is available here"; | |
103 | { | |
104 | no feature 'say'; | |
105 | print "But not here.\n"; | |
106 | } | |
107 | say "Yet it is here."; | |
108 | ||
39ec54a5 RS |
109 | C<no feature> with no features specified will reset to the default group. To |
110 | disable I<all> features (an unusual request!) use C<no feature ':all'>. | |
5e36ed56 | 111 | |
0b25e784 DG |
112 | =head1 AVAILABLE FEATURES |
113 | ||
0d863452 RH |
114 | =head2 The 'say' feature |
115 | ||
0b25e784 | 116 | C<use feature 'say'> tells the compiler to enable the Perl 6 style |
9eb27be9 | 117 | C<say> function. |
0d863452 RH |
118 | |
119 | See L<perlfunc/say> for details. | |
120 | ||
0b25e784 DG |
121 | This feature is available starting with Perl 5.10. |
122 | ||
123 | =head2 The 'state' feature | |
712d05cf RGS |
124 | |
125 | C<use feature 'state'> tells the compiler to enable C<state> | |
9eb27be9 | 126 | variables. |
712d05cf | 127 | |
e60bcc8b RGS |
128 | See L<perlsub/"Persistent Private Variables"> for details. |
129 | ||
0b25e784 DG |
130 | This feature is available starting with Perl 5.10. |
131 | ||
47e9d84a EB |
132 | =head2 The 'switch' feature |
133 | ||
134 | C<use feature 'switch'> tells the compiler to enable the Perl 6 | |
135 | given/when construct. | |
136 | ||
48238296 | 137 | See L<perlsyn/"Switch Statements"> for details. |
47e9d84a | 138 | |
0b25e784 DG |
139 | This feature is available starting with Perl 5.10. |
140 | ||
141 | =head2 The 'unicode_strings' feature | |
1863b879 | 142 | |
20db7501 KW |
143 | C<use feature 'unicode_strings'> tells the compiler to use Unicode semantics |
144 | in all string operations executed within its scope (unless they are also | |
145 | within the scope of either C<use locale> or C<use bytes>). The same applies | |
146 | to all regular expressions compiled within the scope, even if executed outside | |
2269d15c KW |
147 | it. It does not change the internal representation of strings, but only how |
148 | they are interpreted. | |
20db7501 KW |
149 | |
150 | C<no feature 'unicode_strings'> tells the compiler to use the traditional | |
151 | Perl semantics wherein the native character set semantics is used unless it is | |
152 | clear to Perl that Unicode is desired. This can lead to some surprises | |
153 | when the behavior suddenly changes. (See | |
154 | L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are | |
155 | potentially using Unicode in your program, the | |
156 | C<use feature 'unicode_strings'> subpragma is B<strongly> recommended. | |
157 | ||
2e2b2571 KW |
158 | This feature is available starting with Perl 5.12; was almost fully |
159 | implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>. | |
1863b879 | 160 | |
0b25e784 | 161 | =head2 The 'unicode_eval' and 'evalbytes' features |
7289c5e6 FC |
162 | |
163 | Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a | |
164 | string, will evaluate it as a string of characters, ignoring any | |
165 | C<use utf8> declarations. C<use utf8> exists to declare the encoding of | |
166 | the script, which only makes sense for a stream of bytes, not a string of | |
167 | characters. Source filters are forbidden, as they also really only make | |
168 | sense on strings of bytes. Any attempt to activate a source filter will | |
169 | result in an error. | |
170 | ||
171 | The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates | |
172 | the argument passed to it as a string of bytes. It dies if the string | |
173 | contains any characters outside the 8-bit range. Source filters work | |
174 | within C<evalbytes>: they apply to the contents of the string being | |
175 | evaluated. | |
176 | ||
177 | Together, these two features are intended to replace the historical C<eval> | |
178 | function, which has (at least) two bugs in it, that cannot easily be fixed | |
179 | without breaking existing programs: | |
180 | ||
181 | =over | |
182 | ||
183 | =item * | |
184 | ||
185 | C<eval> behaves differently depending on the internal encoding of the | |
186 | string, sometimes treating its argument as a string of bytes, and sometimes | |
187 | as a string of characters. | |
188 | ||
189 | =item * | |
190 | ||
191 | Source filters activated within C<eval> leak out into whichever I<file> | |
192 | scope is currently being compiled. To give an example with the CPAN module | |
193 | L<Semi::Semicolons>: | |
194 | ||
195 | BEGIN { eval "use Semi::Semicolons; # not filtered here " } | |
196 | # filtered here! | |
197 | ||
198 | C<evalbytes> fixes that to work the way one would expect: | |
199 | ||
200 | use feature "evalbytes"; | |
201 | BEGIN { evalbytes "use Semi::Semicolons; # filtered " } | |
202 | # not filtered | |
203 | ||
204 | =back | |
205 | ||
206 | These two features are available starting with Perl 5.16. | |
207 | ||
84ed0108 FC |
208 | =head2 The 'current_sub' feature |
209 | ||
210 | This provides the C<__SUB__> token that returns a reference to the current | |
211 | subroutine or C<undef> outside of a subroutine. | |
212 | ||
213 | This feature is available starting with Perl 5.16. | |
214 | ||
01868d00 FC |
215 | =head2 The 'array_base' feature |
216 | ||
217 | This feature supports the legacy C<$[> variable. See L<perlvar/$[> and | |
218 | L<arybase>. It is on by default but disabled under C<use v5.16> (see | |
219 | L</IMPLICIT LOADING>, below). | |
220 | ||
221 | This feature is available under this name starting with Perl 5.16. In | |
222 | previous versions, it was simply on all the time, and this pragma knew | |
223 | nothing about it. | |
224 | ||
2a4315f8 BF |
225 | =head2 The 'fc' feature |
226 | ||
227 | C<use feature 'fc'> tells the compiler to enable the C<fc> function, | |
228 | which implements Unicode casefolding. | |
229 | ||
230 | See L<perlfunc/fc> for details. | |
231 | ||
232 | This feature is available from Perl 5.16 onwards. | |
233 | ||
ca40957e FC |
234 | =head2 The 'lexical_subs' feature |
235 | ||
236 | B<WARNING>: This feature is still experimental and the implementation may | |
64fbf0dd FC |
237 | change in future versions of Perl. For this reason, Perl will |
238 | warn when you use the feature, unless you have explicitly disabled the | |
ca40957e FC |
239 | warning: |
240 | ||
f1d34ca8 | 241 | no warnings "experimental::lexical_subs"; |
ca40957e FC |
242 | |
243 | This enables declaration of subroutines via C<my sub foo>, C<state sub foo> | |
244 | and C<our sub foo> syntax. See L<perlsub/Lexical Subroutines> for details. | |
245 | ||
246 | This feature is available from Perl 5.18 onwards. | |
247 | ||
bc9b29db RH |
248 | =head1 FEATURE BUNDLES |
249 | ||
0b25e784 | 250 | It's possible to load multiple features together, using |
b22bbcf0 | 251 | a I<feature bundle>. The name of a feature bundle is prefixed with |
0b25e784 DG |
252 | a colon, to distinguish it from an actual feature. |
253 | ||
254 | use feature ":5.10"; | |
255 | ||
256 | The following feature bundles are available: | |
257 | ||
258 | bundle features included | |
259 | --------- ----------------- | |
01868d00 | 260 | :default array_base |
0b25e784 | 261 | |
01868d00 | 262 | :5.10 say state switch array_base |
0b25e784 | 263 | |
01868d00 | 264 | :5.12 say state switch unicode_strings array_base |
0b25e784 | 265 | |
01868d00 | 266 | :5.14 say state switch unicode_strings array_base |
0b25e784 DG |
267 | |
268 | :5.16 say state switch unicode_strings | |
2a4315f8 | 269 | unicode_eval evalbytes current_sub fc |
0b25e784 | 270 | |
52fc5c56 FC |
271 | :5.18 say state switch unicode_strings |
272 | unicode_eval evalbytes current_sub fc | |
273 | ||
d09258e7 RS |
274 | :5.20 say state switch unicode_strings |
275 | unicode_eval evalbytes current_sub fc | |
276 | ||
01868d00 FC |
277 | The C<:default> bundle represents the feature set that is enabled before |
278 | any C<use feature> or C<no feature> declaration. | |
a3a91442 JV |
279 | |
280 | Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has | |
b22bbcf0 | 281 | no effect. Feature bundles are guaranteed to be the same for all sub-versions. |
bc9b29db | 282 | |
0b25e784 DG |
283 | use feature ":5.14.0"; # same as ":5.14" |
284 | use feature ":5.14.1"; # same as ":5.14" | |
a3a91442 | 285 | |
7dfde25d RGS |
286 | =head1 IMPLICIT LOADING |
287 | ||
0b25e784 DG |
288 | Instead of loading feature bundles by name, it is easier to let Perl do |
289 | implicit loading of a feature bundle for you. | |
290 | ||
291 | There are two ways to load the C<feature> pragma implicitly: | |
7dfde25d RGS |
292 | |
293 | =over 4 | |
294 | ||
295 | =item * | |
296 | ||
0b25e784 DG |
297 | By using the C<-E> switch on the Perl command-line instead of C<-e>. |
298 | That will enable the feature bundle for that version of Perl in the | |
299 | main compilation unit (that is, the one-liner that follows C<-E>). | |
7dfde25d RGS |
300 | |
301 | =item * | |
302 | ||
0b25e784 | 303 | By explicitly requiring a minimum Perl version number for your program, with |
b22bbcf0 | 304 | the C<use VERSION> construct. That is, |
7dfde25d | 305 | |
0b25e784 | 306 | use v5.10.0; |
7dfde25d RGS |
307 | |
308 | will do an implicit | |
309 | ||
39ec54a5 | 310 | no feature ':all'; |
82cfb3a2 | 311 | use feature ':5.10'; |
7dfde25d | 312 | |
b22bbcf0 FC |
313 | and so on. Note how the trailing sub-version |
314 | is automatically stripped from the | |
82cfb3a2 | 315 | version. |
7dfde25d | 316 | |
8d115822 RB |
317 | But to avoid portability warnings (see L<perlfunc/use>), you may prefer: |
318 | ||
319 | use 5.010; | |
320 | ||
321 | with the same effect. | |
322 | ||
0b25e784 | 323 | If the required version is older than Perl 5.10, the ":default" feature |
01868d00 | 324 | bundle is automatically loaded instead. |
70397346 | 325 | |
7dfde25d RGS |
326 | =back |
327 | ||
0d863452 RH |
328 | =cut |
329 | ||
330 | sub import { | |
0d863452 | 331 | my $class = shift; |
36143a0c NC |
332 | |
333 | if (!@_) { | |
0b25e784 | 334 | croak("No features specified"); |
0d863452 | 335 | } |
36143a0c | 336 | |
d3757264 | 337 | __common(1, @_); |
0d863452 RH |
338 | } |
339 | ||
340 | sub unimport { | |
341 | my $class = shift; | |
342 | ||
39ec54a5 | 343 | # A bare C<no feature> should reset to the default bundle |
bc9b29db | 344 | if (!@_) { |
39ec54a5 RS |
345 | $^H &= ~($hint_uni8bit|$hint_mask); |
346 | return; | |
bc9b29db RH |
347 | } |
348 | ||
d3757264 NC |
349 | __common(0, @_); |
350 | } | |
351 | ||
352 | ||
353 | sub __common { | |
354 | my $import = shift; | |
0c8d5017 NC |
355 | my $bundle_number = $^H & $hint_mask; |
356 | my $features = $bundle_number != $hint_mask | |
357 | && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]}; | |
358 | if ($features) { | |
da5b5421 | 359 | # Features are enabled implicitly via bundle hints. |
d9ee6ccb NC |
360 | # Delete any keys that may be left over from last time. |
361 | delete @^H{ values(%feature) }; | |
362 | $^H |= $hint_mask; | |
363 | for (@$features) { | |
364 | $^H{$feature{$_}} = 1; | |
365 | $^H |= $hint_uni8bit if $_ eq 'unicode_strings'; | |
366 | } | |
da5b5421 | 367 | } |
bc9b29db | 368 | while (@_) { |
0b25e784 DG |
369 | my $name = shift; |
370 | if (substr($name, 0, 1) eq ":") { | |
371 | my $v = substr($name, 1); | |
372 | if (!exists $feature_bundle{$v}) { | |
373 | $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/; | |
374 | if (!exists $feature_bundle{$v}) { | |
375 | unknown_feature_bundle(substr($name, 1)); | |
376 | } | |
377 | } | |
378 | unshift @_, @{$feature_bundle{$v}}; | |
379 | next; | |
380 | } | |
36143a0c | 381 | if (!exists $feature{$name}) { |
0b25e784 DG |
382 | unknown_feature($name); |
383 | } | |
d3757264 NC |
384 | if ($import) { |
385 | $^H{$feature{$name}} = 1; | |
386 | $^H |= $hint_uni8bit if $name eq 'unicode_strings'; | |
387 | } else { | |
0b25e784 | 388 | delete $^H{$feature{$name}}; |
1863b879 | 389 | $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings'; |
0b25e784 | 390 | } |
0d863452 | 391 | } |
0d863452 RH |
392 | } |
393 | ||
b42943c4 RGS |
394 | sub unknown_feature { |
395 | my $feature = shift; | |
396 | croak(sprintf('Feature "%s" is not supported by Perl %vd', | |
0b25e784 | 397 | $feature, $^V)); |
b42943c4 RGS |
398 | } |
399 | ||
400 | sub unknown_feature_bundle { | |
401 | my $feature = shift; | |
402 | croak(sprintf('Feature bundle "%s" is not supported by Perl %vd', | |
0b25e784 | 403 | $feature, $^V)); |
b42943c4 RGS |
404 | } |
405 | ||
406 | sub croak { | |
407 | require Carp; | |
408 | Carp::croak(@_); | |
409 | } | |
410 | ||
0d863452 | 411 | 1; |
69bcf1d3 FC |
412 | |
413 | # ex: set ro: |