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