This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
untodo the no-longer-failing todo test for rgs' patch
[perl5.git] / lib / feature.pm
CommitLineData
0d863452
RH
1package feature;
2
b7569deb 3our $VERSION = '1.16';
0d863452
RH
4
5# (feature name) => (internal name, used in %^H)
6my %feature = (
1863b879
RGS
7 switch => 'feature_switch',
8 say => "feature_say",
9 state => "feature_state",
10 unicode_strings => "feature_unicode",
bc9b29db
RH
11);
12
1863b879
RGS
13# This gets set (for now) in $^H as well as in %^H,
14# for runtime speed of the uc/lc/ucfirst/lcfirst functions.
b0f41c9d 15# See HINT_UNI_8_BIT in perl.h.
1863b879
RGS
16our $hint_uni8bit = 0x00000800;
17
13a7998c
RGS
18# NB. the latest bundle must be loaded by the -E switch (see toke.c)
19
bc9b29db 20my %feature_bundle = (
82cfb3a2 21 "5.10" => [qw(switch say state)],
1863b879 22 "5.11" => [qw(switch say state unicode_strings)],
b7569deb 23 "5.12" => [qw(switch say state unicode_strings)],
0d863452 24);
d052521a 25
82cfb3a2
S
26# special case
27$feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
7dfde25d 28
0d863452 29# TODO:
1c321dc6 30# - think about versioned features (use feature switch => 2)
0d863452
RH
31
32=head1 NAME
33
e1b711da 34feature - Perl pragma to enable new features
0d863452
RH
35
36=head1 SYNOPSIS
37
bc9b29db 38 use feature qw(switch say);
0d863452 39 given ($foo) {
bc9b29db
RH
40 when (1) { say "\$foo == 1" }
41 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
42 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
43 when ($_ > 100) { say "\$foo > 100" }
44 default { say "None of the above" }
0d863452
RH
45 }
46
ec488c7f
RGS
47 use feature ':5.10'; # loads all features available in perl 5.10
48
0d863452
RH
49=head1 DESCRIPTION
50
51It is usually impossible to add new syntax to Perl without breaking
52some existing programs. This pragma provides a way to minimize that
1863b879
RGS
53risk. New syntactic constructs, or new semantic meanings to older
54constructs, can be enabled by C<use feature 'foo'>, and will be parsed
55only when the appropriate feature pragma is in scope.
0d863452 56
9eb27be9
RGS
57=head2 Lexical effect
58
59Like other pragmas (C<use strict>, for example), features have a lexical
5e36ed56 60effect. C<use feature qw(foo)> will only make the feature "foo" available
9eb27be9
RGS
61from that point to the end of the enclosing block.
62
63 {
64 use feature 'say';
65 say "say is available here";
66 }
67 print "But not here.\n";
68
5e36ed56
RGS
69=head2 C<no feature>
70
71Features can also be turned off by using C<no feature "foo">. This too
72has lexical effect.
73
74 use feature 'say';
75 say "say is available here";
76 {
77 no feature 'say';
78 print "But not here.\n";
79 }
80 say "Yet it is here.";
81
82C<no feature> with no features specified will turn off all features.
83
0d863452
RH
84=head2 The 'switch' feature
85
86C<use feature 'switch'> tells the compiler to enable the Perl 6
9eb27be9 87given/when construct.
0d863452
RH
88
89See L<perlsyn/"Switch statements"> for details.
90
0d863452
RH
91=head2 The 'say' feature
92
93C<use feature 'say'> tells the compiler to enable the Perl 6
9eb27be9 94C<say> function.
0d863452
RH
95
96See L<perlfunc/say> for details.
97
712d05cf
RGS
98=head2 the 'state' feature
99
100C<use feature 'state'> tells the compiler to enable C<state>
9eb27be9 101variables.
712d05cf 102
e60bcc8b
RGS
103See L<perlsub/"Persistent Private Variables"> for details.
104
1863b879
RGS
105=head2 the 'unicode_strings' feature
106
107C<use feature 'unicode_strings'> tells the compiler to treat
e1b711da
KW
108all strings outside of C<use locale> and C<use bytes> as Unicode. It is
109available starting with Perl 5.11.3.
1863b879 110
e1b711da 111See L<perlunicode/The "Unicode Bug"> for details.
1863b879 112
bc9b29db
RH
113=head1 FEATURE BUNDLES
114
115It's possible to load a whole slew of features in one go, using
116a I<feature bundle>. The name of a feature bundle is prefixed with
117a colon, to distinguish it from an actual feature. At present, the
82cfb3a2
S
118only feature bundle is C<use feature ":5.10"> which is equivalent
119to C<use feature qw(switch say state)>.
8fd870d9 120
82cfb3a2
S
121Specifying sub-versions such as the C<0> in C<5.10.0> in feature bundles has
122no effect: feature bundles are guaranteed to be the same for all sub-versions.
bc9b29db 123
7dfde25d
RGS
124=head1 IMPLICIT LOADING
125
126There are two ways to load the C<feature> pragma implicitly :
127
128=over 4
129
130=item *
131
132By using the C<-E> switch on the command-line instead of C<-e>. It enables
133all available features in the main compilation unit (that is, the one-liner.)
134
135=item *
136
137By requiring explicitly a minimal Perl version number for your program, with
138the C<use VERSION> construct, and when the version is higher than or equal to
8d115822 1395.10.0. That is,
7dfde25d 140
8d115822 141 use 5.10.0;
7dfde25d
RGS
142
143will do an implicit
144
82cfb3a2 145 use feature ':5.10';
7dfde25d 146
82cfb3a2
S
147and so on. Note how the trailing sub-version is automatically stripped from the
148version.
7dfde25d 149
8d115822
RB
150But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
151
152 use 5.010;
153
154with the same effect.
155
7dfde25d
RGS
156=back
157
0d863452
RH
158=cut
159
160sub import {
0d863452
RH
161 my $class = shift;
162 if (@_ == 0) {
0d863452
RH
163 croak("No features specified");
164 }
165 while (@_) {
166 my $name = shift(@_);
89c3975a
RGS
167 if (substr($name, 0, 1) eq ":") {
168 my $v = substr($name, 1);
7be54ea7 169 if (!exists $feature_bundle{$v}) {
82cfb3a2
S
170 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
171 if (!exists $feature_bundle{$v}) {
172 unknown_feature_bundle(substr($name, 1));
173 }
bc9b29db 174 }
7be54ea7 175 unshift @_, @{$feature_bundle{$v}};
bc9b29db
RH
176 next;
177 }
0d863452 178 if (!exists $feature{$name}) {
b42943c4 179 unknown_feature($name);
0d863452
RH
180 }
181 $^H{$feature{$name}} = 1;
1863b879 182 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
0d863452
RH
183 }
184}
185
186sub unimport {
187 my $class = shift;
188
189 # A bare C<no feature> should disable *all* features
bc9b29db
RH
190 if (!@_) {
191 delete @^H{ values(%feature) };
1863b879 192 $^H &= ~ $hint_uni8bit;
bc9b29db
RH
193 return;
194 }
195
196 while (@_) {
197 my $name = shift;
89c3975a
RGS
198 if (substr($name, 0, 1) eq ":") {
199 my $v = substr($name, 1);
7be54ea7 200 if (!exists $feature_bundle{$v}) {
82cfb3a2
S
201 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
202 if (!exists $feature_bundle{$v}) {
203 unknown_feature_bundle(substr($name, 1));
204 }
bc9b29db 205 }
7be54ea7 206 unshift @_, @{$feature_bundle{$v}};
bc9b29db
RH
207 next;
208 }
0d863452 209 if (!exists($feature{$name})) {
b42943c4 210 unknown_feature($name);
0d863452
RH
211 }
212 else {
213 delete $^H{$feature{$name}};
1863b879 214 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
0d863452
RH
215 }
216 }
0d863452
RH
217}
218
b42943c4
RGS
219sub unknown_feature {
220 my $feature = shift;
221 croak(sprintf('Feature "%s" is not supported by Perl %vd',
222 $feature, $^V));
223}
224
225sub unknown_feature_bundle {
226 my $feature = shift;
227 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
228 $feature, $^V));
229}
230
231sub croak {
232 require Carp;
233 Carp::croak(@_);
234}
235
0d863452 2361;