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