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