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