Commit | Line | Data |
0d863452 |
1 | package feature; |
2 | |
8fd870d9 |
3 | our $VERSION = '1.10'; |
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 | ); |
8fd870d9 |
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 | |
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 | |
39 | =head1 DESCRIPTION |
40 | |
41 | It is usually impossible to add new syntax to Perl without breaking |
42 | some existing programs. This pragma provides a way to minimize that |
43 | risk. New syntactic constructs can be enabled by C<use feature 'foo'>, |
44 | and will be parsed only when the appropriate feature pragma is in |
45 | scope. |
46 | |
9eb27be9 |
47 | =head2 Lexical effect |
48 | |
49 | Like other pragmas (C<use strict>, for example), features have a lexical |
5e36ed56 |
50 | effect. C<use feature qw(foo)> will only make the feature "foo" available |
9eb27be9 |
51 | from that point to the end of the enclosing block. |
52 | |
53 | { |
54 | use feature 'say'; |
55 | say "say is available here"; |
56 | } |
57 | print "But not here.\n"; |
58 | |
5e36ed56 |
59 | =head2 C<no feature> |
60 | |
61 | Features can also be turned off by using C<no feature "foo">. This too |
62 | has lexical effect. |
63 | |
64 | use feature 'say'; |
65 | say "say is available here"; |
66 | { |
67 | no feature 'say'; |
68 | print "But not here.\n"; |
69 | } |
70 | say "Yet it is here."; |
71 | |
72 | C<no feature> with no features specified will turn off all features. |
73 | |
0d863452 |
74 | =head2 The 'switch' feature |
75 | |
76 | C<use feature 'switch'> tells the compiler to enable the Perl 6 |
9eb27be9 |
77 | given/when construct. |
0d863452 |
78 | |
79 | See L<perlsyn/"Switch statements"> for details. |
80 | |
0d863452 |
81 | =head2 The 'say' feature |
82 | |
83 | C<use feature 'say'> tells the compiler to enable the Perl 6 |
9eb27be9 |
84 | C<say> function. |
0d863452 |
85 | |
86 | See L<perlfunc/say> for details. |
87 | |
bc9b29db |
88 | =head2 the 'err' feature |
89 | |
90 | C<use feature 'err'> tells the compiler to enable the C<err> |
9eb27be9 |
91 | operator. |
bc9b29db |
92 | |
93 | C<err> is a low-precedence variant of the C<//> operator: |
94 | see C<perlop> 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 | |
bc9b29db |
103 | =head1 FEATURE BUNDLES |
104 | |
105 | It's possible to load a whole slew of features in one go, using |
106 | a I<feature bundle>. The name of a feature bundle is prefixed with |
107 | a colon, to distinguish it from an actual feature. At present, the |
8fd870d9 |
108 | only feature bundles are C<use feature ":5.10"> and C<use feature ":5.10.0">, |
3e7dd34d |
109 | which both are equivalent to C<use feature qw(switch say err state)>. |
8fd870d9 |
110 | |
111 | In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be |
112 | equivalent to the latest C<use feature ":5.10.X">. |
bc9b29db |
113 | |
0d863452 |
114 | =cut |
115 | |
116 | sub import { |
0d863452 |
117 | my $class = shift; |
118 | if (@_ == 0) { |
0d863452 |
119 | croak("No features specified"); |
120 | } |
121 | while (@_) { |
122 | my $name = shift(@_); |
bc9b29db |
123 | if ($name =~ /^:(.*)/) { |
124 | if (!exists $feature_bundle{$1}) { |
b42943c4 |
125 | unknown_feature_bundle($1); |
bc9b29db |
126 | } |
127 | unshift @_, @{$feature_bundle{$1}}; |
128 | next; |
129 | } |
0d863452 |
130 | if (!exists $feature{$name}) { |
b42943c4 |
131 | unknown_feature($name); |
0d863452 |
132 | } |
133 | $^H{$feature{$name}} = 1; |
134 | } |
135 | } |
136 | |
137 | sub unimport { |
138 | my $class = shift; |
139 | |
140 | # A bare C<no feature> should disable *all* features |
bc9b29db |
141 | if (!@_) { |
142 | delete @^H{ values(%feature) }; |
143 | return; |
144 | } |
145 | |
146 | while (@_) { |
147 | my $name = shift; |
148 | if ($name =~ /^:(.*)/) { |
149 | if (!exists $feature_bundle{$1}) { |
b42943c4 |
150 | unknown_feature_bundle($1); |
bc9b29db |
151 | } |
152 | unshift @_, @{$feature_bundle{$1}}; |
153 | next; |
154 | } |
0d863452 |
155 | if (!exists($feature{$name})) { |
b42943c4 |
156 | unknown_feature($name); |
0d863452 |
157 | } |
158 | else { |
159 | delete $^H{$feature{$name}}; |
160 | } |
161 | } |
0d863452 |
162 | } |
163 | |
b42943c4 |
164 | sub unknown_feature { |
165 | my $feature = shift; |
166 | croak(sprintf('Feature "%s" is not supported by Perl %vd', |
167 | $feature, $^V)); |
168 | } |
169 | |
170 | sub unknown_feature_bundle { |
171 | my $feature = shift; |
172 | croak(sprintf('Feature bundle "%s" is not supported by Perl %vd', |
173 | $feature, $^V)); |
174 | } |
175 | |
176 | sub croak { |
177 | require Carp; |
178 | Carp::croak(@_); |
179 | } |
180 | |
0d863452 |
181 | 1; |