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