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