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