Commit | Line | Data |
---|---|---|
0d863452 RH |
1 | package feature; |
2 | ||
712d05cf | 3 | our $VERSION = '1.01'; |
0d863452 RH |
4 | |
5 | # (feature name) => (internal name, used in %^H) | |
6 | my %feature = ( | |
7b9ef140 RH |
7 | switch => 'feature_switch', |
8 | "~~" => "feature_~~", | |
9 | say => "feature_say", | |
bc9b29db | 10 | err => "feature_err", |
c0cd9745 | 11 | dor => "feature_err", |
712d05cf | 12 | state => "feature_state", |
bc9b29db RH |
13 | ); |
14 | ||
15 | my %feature_bundle = ( | |
712d05cf | 16 | "5.10" => [qw(switch ~~ say err state)], |
0d863452 RH |
17 | ); |
18 | ||
19 | ||
0d863452 | 20 | # TODO: |
1c321dc6 | 21 | # - think about versioned features (use feature switch => 2) |
0d863452 RH |
22 | |
23 | =head1 NAME | |
24 | ||
25 | feature - Perl pragma to enable new syntactic features | |
26 | ||
27 | =head1 SYNOPSIS | |
28 | ||
bc9b29db | 29 | use feature qw(switch say); |
0d863452 | 30 | given ($foo) { |
bc9b29db RH |
31 | when (1) { say "\$foo == 1" } |
32 | when ([2,3]) { say "\$foo == 2 || \$foo == 3" } | |
33 | when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" } | |
34 | when ($_ > 100) { say "\$foo > 100" } | |
35 | default { say "None of the above" } | |
0d863452 RH |
36 | } |
37 | ||
38 | =head1 DESCRIPTION | |
39 | ||
40 | It is usually impossible to add new syntax to Perl without breaking | |
41 | some existing programs. This pragma provides a way to minimize that | |
42 | risk. New syntactic constructs can be enabled by C<use feature 'foo'>, | |
43 | and will be parsed only when the appropriate feature pragma is in | |
44 | scope. | |
45 | ||
9eb27be9 RGS |
46 | =head2 Lexical effect |
47 | ||
48 | Like other pragmas (C<use strict>, for example), features have a lexical | |
49 | effect. C<use feature qw(foo)> will only make the feature "foo" available | |
50 | from that point to the end of the enclosing block. | |
51 | ||
52 | { | |
53 | use feature 'say'; | |
54 | say "say is available here"; | |
55 | } | |
56 | print "But not here.\n"; | |
57 | ||
0d863452 RH |
58 | =head2 The 'switch' feature |
59 | ||
60 | C<use feature 'switch'> tells the compiler to enable the Perl 6 | |
9eb27be9 | 61 | given/when construct. |
0d863452 RH |
62 | |
63 | See L<perlsyn/"Switch statements"> for details. | |
64 | ||
65 | =head2 The '~~' feature | |
66 | ||
67 | C<use feature '~~'> tells the compiler to enable the Perl 6 | |
9eb27be9 | 68 | smart match C<~~> operator. |
0d863452 RH |
69 | |
70 | See L<perlsyn/"Smart Matching in Detail"> for details. | |
71 | ||
72 | =head2 The 'say' feature | |
73 | ||
74 | C<use feature 'say'> tells the compiler to enable the Perl 6 | |
9eb27be9 | 75 | C<say> function. |
0d863452 RH |
76 | |
77 | See L<perlfunc/say> for details. | |
78 | ||
bc9b29db RH |
79 | =head2 the 'err' feature |
80 | ||
81 | C<use feature 'err'> tells the compiler to enable the C<err> | |
9eb27be9 | 82 | operator. |
bc9b29db RH |
83 | |
84 | C<err> is a low-precedence variant of the C<//> operator: | |
85 | see C<perlop> for details. | |
86 | ||
c0cd9745 RGS |
87 | =head2 the 'dor' feature |
88 | ||
89 | The 'dor' feature is an alias for the 'err' feature. | |
90 | ||
712d05cf RGS |
91 | =head2 the 'state' feature |
92 | ||
93 | C<use feature 'state'> tells the compiler to enable C<state> | |
9eb27be9 | 94 | variables. |
712d05cf | 95 | |
e60bcc8b RGS |
96 | See L<perlsub/"Persistent Private Variables"> for details. |
97 | ||
bc9b29db RH |
98 | =head1 FEATURE BUNDLES |
99 | ||
100 | It's possible to load a whole slew of features in one go, using | |
101 | a I<feature bundle>. The name of a feature bundle is prefixed with | |
102 | a colon, to distinguish it from an actual feature. At present, the | |
103 | only feature bundle is C<use feature ":5.10">, which is equivalent | |
712d05cf | 104 | to C<use feature qw(switch ~~ say err state)>. |
bc9b29db | 105 | |
0d863452 RH |
106 | =cut |
107 | ||
108 | sub import { | |
0d863452 RH |
109 | my $class = shift; |
110 | if (@_ == 0) { | |
0d863452 RH |
111 | croak("No features specified"); |
112 | } | |
113 | while (@_) { | |
114 | my $name = shift(@_); | |
bc9b29db RH |
115 | if ($name =~ /^:(.*)/) { |
116 | if (!exists $feature_bundle{$1}) { | |
b42943c4 | 117 | unknown_feature_bundle($1); |
bc9b29db RH |
118 | } |
119 | unshift @_, @{$feature_bundle{$1}}; | |
120 | next; | |
121 | } | |
0d863452 | 122 | if (!exists $feature{$name}) { |
b42943c4 | 123 | unknown_feature($name); |
0d863452 RH |
124 | } |
125 | $^H{$feature{$name}} = 1; | |
126 | } | |
127 | } | |
128 | ||
129 | sub unimport { | |
130 | my $class = shift; | |
131 | ||
132 | # A bare C<no feature> should disable *all* features | |
bc9b29db RH |
133 | if (!@_) { |
134 | delete @^H{ values(%feature) }; | |
135 | return; | |
136 | } | |
137 | ||
138 | while (@_) { | |
139 | my $name = shift; | |
140 | if ($name =~ /^:(.*)/) { | |
141 | if (!exists $feature_bundle{$1}) { | |
b42943c4 | 142 | unknown_feature_bundle($1); |
bc9b29db RH |
143 | } |
144 | unshift @_, @{$feature_bundle{$1}}; | |
145 | next; | |
146 | } | |
0d863452 | 147 | if (!exists($feature{$name})) { |
b42943c4 | 148 | unknown_feature($name); |
0d863452 RH |
149 | } |
150 | else { | |
151 | delete $^H{$feature{$name}}; | |
152 | } | |
153 | } | |
0d863452 RH |
154 | } |
155 | ||
b42943c4 RGS |
156 | sub unknown_feature { |
157 | my $feature = shift; | |
158 | croak(sprintf('Feature "%s" is not supported by Perl %vd', | |
159 | $feature, $^V)); | |
160 | } | |
161 | ||
162 | sub unknown_feature_bundle { | |
163 | my $feature = shift; | |
164 | croak(sprintf('Feature bundle "%s" is not supported by Perl %vd', | |
165 | $feature, $^V)); | |
166 | } | |
167 | ||
168 | sub croak { | |
169 | require Carp; | |
170 | Carp::croak(@_); | |
171 | } | |
172 | ||
0d863452 | 173 | 1; |