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