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