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