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