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