This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add 5.16 feature bundle
[perl5.git] / lib / feature.pm
CommitLineData
0d863452
RH
1package feature;
2
cc7b5b23 3our $VERSION = '1.23';
0d863452
RH
4
5# (feature name) => (internal name, used in %^H)
6my %feature = (
47e9d84a
EB
7 say => 'feature_say',
8 state => 'feature_state',
1863b879 9 switch => 'feature_switch',
7d789282 10 evalbytes => 'feature_evalbytes',
cda6b701 11 unicode_eval => 'feature_unieval',
47e9d84a 12 unicode_strings => 'feature_unicode',
bc9b29db
RH
13);
14
1863b879
RGS
15# This gets set (for now) in $^H as well as in %^H,
16# for runtime speed of the uc/lc/ucfirst/lcfirst functions.
b0f41c9d 17# See HINT_UNI_8_BIT in perl.h.
1863b879
RGS
18our $hint_uni8bit = 0x00000800;
19
13a7998c
RGS
20# NB. the latest bundle must be loaded by the -E switch (see toke.c)
21
bc9b29db 22my %feature_bundle = (
47e9d84a
EB
23 "5.10" => [qw(say state switch)],
24 "5.11" => [qw(say state switch unicode_strings)],
25 "5.12" => [qw(say state switch unicode_strings)],
26 "5.13" => [qw(say state switch unicode_strings)],
27 "5.14" => [qw(say state switch unicode_strings)],
7d789282 28 "5.15" => [qw(say state switch unicode_strings unicode_eval
167f59c1
FC
29 evalbytes)],
30 "5.16" => [qw(say state switch unicode_strings unicode_eval
7d789282 31 evalbytes)],
0d863452 32);
d052521a 33
82cfb3a2
S
34# special case
35$feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
7dfde25d 36
0d863452 37# TODO:
1c321dc6 38# - think about versioned features (use feature switch => 2)
0d863452
RH
39
40=head1 NAME
41
e1b711da 42feature - Perl pragma to enable new features
0d863452
RH
43
44=head1 SYNOPSIS
45
47e9d84a 46 use feature qw(say switch);
0d863452 47 given ($foo) {
bc9b29db
RH
48 when (1) { say "\$foo == 1" }
49 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
50 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
51 when ($_ > 100) { say "\$foo > 100" }
52 default { say "None of the above" }
0d863452
RH
53 }
54
ec488c7f
RGS
55 use feature ':5.10'; # loads all features available in perl 5.10
56
0d863452
RH
57=head1 DESCRIPTION
58
59It is usually impossible to add new syntax to Perl without breaking
60some existing programs. This pragma provides a way to minimize that
1863b879
RGS
61risk. New syntactic constructs, or new semantic meanings to older
62constructs, can be enabled by C<use feature 'foo'>, and will be parsed
4a904372
FC
63only when the appropriate feature pragma is in scope. (Nevertheless, the
64C<CORE::> prefix provides access to all Perl keywords, regardless of this
65pragma.)
0d863452 66
9eb27be9
RGS
67=head2 Lexical effect
68
69Like other pragmas (C<use strict>, for example), features have a lexical
5e36ed56 70effect. C<use feature qw(foo)> will only make the feature "foo" available
9eb27be9
RGS
71from that point to the end of the enclosing block.
72
73 {
74 use feature 'say';
75 say "say is available here";
76 }
77 print "But not here.\n";
78
5e36ed56
RGS
79=head2 C<no feature>
80
81Features can also be turned off by using C<no feature "foo">. This too
82has lexical effect.
83
84 use feature 'say';
85 say "say is available here";
86 {
87 no feature 'say';
88 print "But not here.\n";
89 }
90 say "Yet it is here.";
91
92C<no feature> with no features specified will turn off all features.
93
0d863452
RH
94=head2 The 'say' feature
95
96C<use feature 'say'> tells the compiler to enable the Perl 6
9eb27be9 97C<say> function.
0d863452
RH
98
99See L<perlfunc/say> for details.
100
712d05cf
RGS
101=head2 the 'state' feature
102
103C<use feature 'state'> tells the compiler to enable C<state>
9eb27be9 104variables.
712d05cf 105
e60bcc8b
RGS
106See L<perlsub/"Persistent Private Variables"> for details.
107
47e9d84a
EB
108=head2 The 'switch' feature
109
110C<use feature 'switch'> tells the compiler to enable the Perl 6
111given/when construct.
112
113See L<perlsyn/"Switch statements"> for details.
114
1863b879
RGS
115=head2 the 'unicode_strings' feature
116
20db7501
KW
117C<use feature 'unicode_strings'> tells the compiler to use Unicode semantics
118in all string operations executed within its scope (unless they are also
119within the scope of either C<use locale> or C<use bytes>). The same applies
120to all regular expressions compiled within the scope, even if executed outside
121it.
122
123C<no feature 'unicode_strings'> tells the compiler to use the traditional
124Perl semantics wherein the native character set semantics is used unless it is
125clear to Perl that Unicode is desired. This can lead to some surprises
126when the behavior suddenly changes. (See
127L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are
128potentially using Unicode in your program, the
129C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
130
131This subpragma is available starting with Perl 5.11.3, but was not fully
132implemented until 5.13.8.
1863b879 133
7289c5e6
FC
134=head2 the 'unicode_eval' and 'evalbytes' features
135
136Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a
137string, will evaluate it as a string of characters, ignoring any
138C<use utf8> declarations. C<use utf8> exists to declare the encoding of
139the script, which only makes sense for a stream of bytes, not a string of
140characters. Source filters are forbidden, as they also really only make
141sense on strings of bytes. Any attempt to activate a source filter will
142result in an error.
143
144The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates
145the argument passed to it as a string of bytes. It dies if the string
146contains any characters outside the 8-bit range. Source filters work
147within C<evalbytes>: they apply to the contents of the string being
148evaluated.
149
150Together, these two features are intended to replace the historical C<eval>
151function, which has (at least) two bugs in it, that cannot easily be fixed
152without breaking existing programs:
153
154=over
155
156=item *
157
158C<eval> behaves differently depending on the internal encoding of the
159string, sometimes treating its argument as a string of bytes, and sometimes
160as a string of characters.
161
162=item *
163
164Source filters activated within C<eval> leak out into whichever I<file>
165scope is currently being compiled. To give an example with the CPAN module
166L<Semi::Semicolons>:
167
168 BEGIN { eval "use Semi::Semicolons; # not filtered here " }
169 # filtered here!
170
171C<evalbytes> fixes that to work the way one would expect:
172
173 use feature "evalbytes";
174 BEGIN { evalbytes "use Semi::Semicolons; # filtered " }
175 # not filtered
176
177=back
178
179These two features are available starting with Perl 5.16.
180
bc9b29db
RH
181=head1 FEATURE BUNDLES
182
183It's possible to load a whole slew of features in one go, using
184a I<feature bundle>. The name of a feature bundle is prefixed with
185a colon, to distinguish it from an actual feature. At present, the
a3a91442
JV
186only feature bundles correspond to Perl releases, e.g. C<use feature
187":5.10"> which is equivalent to C<use feature qw(switch say state)>.
8fd870d9 188
a3a91442
JV
189By convention, the feature bundle for any given Perl release includes
190the features of previous releases, down to and including 5.10, the
191first official release to provide this facility. Since Perl 5.12
192only provides one new feature, C<unicode_strings>, and Perl 5.14
193provides none, C<use feature ":5.14"> is equivalent to C<use feature
194qw(switch say state unicode_strings)>.
195
196Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
82cfb3a2 197no effect: feature bundles are guaranteed to be the same for all sub-versions.
bc9b29db 198
a3a91442
JV
199Note that instead of using release-based feature bundles it is usually
200better, and shorter, to use implicit loading as described below.
201
7dfde25d
RGS
202=head1 IMPLICIT LOADING
203
204There are two ways to load the C<feature> pragma implicitly :
205
206=over 4
207
208=item *
209
210By using the C<-E> switch on the command-line instead of C<-e>. It enables
211all available features in the main compilation unit (that is, the one-liner.)
212
213=item *
214
215By requiring explicitly a minimal Perl version number for your program, with
216the C<use VERSION> construct, and when the version is higher than or equal to
8d115822 2175.10.0. That is,
7dfde25d 218
8d115822 219 use 5.10.0;
7dfde25d
RGS
220
221will do an implicit
222
82cfb3a2 223 use feature ':5.10';
7dfde25d 224
82cfb3a2
S
225and so on. Note how the trailing sub-version is automatically stripped from the
226version.
7dfde25d 227
8d115822
RB
228But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
229
230 use 5.010;
231
232with the same effect.
233
7dfde25d
RGS
234=back
235
0d863452
RH
236=cut
237
238sub import {
0d863452
RH
239 my $class = shift;
240 if (@_ == 0) {
0d863452
RH
241 croak("No features specified");
242 }
243 while (@_) {
244 my $name = shift(@_);
89c3975a
RGS
245 if (substr($name, 0, 1) eq ":") {
246 my $v = substr($name, 1);
7be54ea7 247 if (!exists $feature_bundle{$v}) {
82cfb3a2
S
248 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
249 if (!exists $feature_bundle{$v}) {
250 unknown_feature_bundle(substr($name, 1));
251 }
bc9b29db 252 }
7be54ea7 253 unshift @_, @{$feature_bundle{$v}};
bc9b29db
RH
254 next;
255 }
0d863452 256 if (!exists $feature{$name}) {
b42943c4 257 unknown_feature($name);
0d863452
RH
258 }
259 $^H{$feature{$name}} = 1;
1863b879 260 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
0d863452
RH
261 }
262}
263
264sub unimport {
265 my $class = shift;
266
267 # A bare C<no feature> should disable *all* features
bc9b29db
RH
268 if (!@_) {
269 delete @^H{ values(%feature) };
1863b879 270 $^H &= ~ $hint_uni8bit;
bc9b29db
RH
271 return;
272 }
273
274 while (@_) {
275 my $name = shift;
89c3975a
RGS
276 if (substr($name, 0, 1) eq ":") {
277 my $v = substr($name, 1);
7be54ea7 278 if (!exists $feature_bundle{$v}) {
82cfb3a2
S
279 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
280 if (!exists $feature_bundle{$v}) {
281 unknown_feature_bundle(substr($name, 1));
282 }
bc9b29db 283 }
7be54ea7 284 unshift @_, @{$feature_bundle{$v}};
bc9b29db
RH
285 next;
286 }
0d863452 287 if (!exists($feature{$name})) {
b42943c4 288 unknown_feature($name);
0d863452
RH
289 }
290 else {
291 delete $^H{$feature{$name}};
1863b879 292 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
0d863452
RH
293 }
294 }
0d863452
RH
295}
296
b42943c4
RGS
297sub unknown_feature {
298 my $feature = shift;
299 croak(sprintf('Feature "%s" is not supported by Perl %vd',
300 $feature, $^V));
301}
302
303sub unknown_feature_bundle {
304 my $feature = shift;
305 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
306 $feature, $^V));
307}
308
309sub croak {
310 require Carp;
311 Carp::croak(@_);
312}
313
0d863452 3141;