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