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