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