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