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