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