fde2e6ac54abae4064f1eaf166b1c7c20401ec60
[p5sagit/p5-mst-13.2.git] / lib / feature.pm
1 package feature;
2
3 our $VERSION = '1.01';
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     dor    => "feature_err",
12     state  => "feature_state",
13 );
14
15 my %feature_bundle = (
16     "5.10" => [qw(switch ~~ say err state)],
17 );
18
19
20 # TODO:
21 # - think about versioned features (use feature switch => 2)
22
23 =head1 NAME
24
25 feature - Perl pragma to enable new syntactic features
26
27 =head1 SYNOPSIS
28
29     use feature qw(switch say);
30     given ($foo) {
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" }
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
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
58 =head2 C<no feature>
59
60 Features can also be turned off by using C<no feature "foo">. This too
61 has lexical effect.
62
63     use feature 'say';
64     say "say is available here";
65     {
66         no feature 'say';
67         print "But not here.\n";
68     }
69     say "Yet it is here.";
70
71 C<no feature> with no features specified will turn off all features.
72
73 =head2 The 'switch' feature
74
75 C<use feature 'switch'> tells the compiler to enable the Perl 6
76 given/when construct.
77
78 See L<perlsyn/"Switch statements"> for details.
79
80 =head2 The '~~' feature
81
82 C<use feature '~~'> tells the compiler to enable the Perl 6
83 smart match C<~~> operator.
84
85 See L<perlsyn/"Smart Matching in Detail"> for details.
86
87 =head2 The 'say' feature
88
89 C<use feature 'say'> tells the compiler to enable the Perl 6
90 C<say> function.
91
92 See L<perlfunc/say> for details.
93
94 =head2 the 'err' feature
95
96 C<use feature 'err'> tells the compiler to enable the C<err>
97 operator.
98
99 C<err> is a low-precedence variant of the C<//> operator:
100 see C<perlop> for details.
101
102 =head2 the 'dor' feature
103
104 The 'dor' feature is an alias for the 'err' feature.
105
106 =head2 the 'state' feature
107
108 C<use feature 'state'> tells the compiler to enable C<state>
109 variables.
110
111 See L<perlsub/"Persistent Private Variables"> for details.
112
113 =head1 FEATURE BUNDLES
114
115 It's possible to load a whole slew of features in one go, using
116 a I<feature bundle>. The name of a feature bundle is prefixed with
117 a colon, to distinguish it from an actual feature. At present, the
118 only feature bundle is C<use feature ":5.10">, which is equivalent
119 to C<use feature qw(switch ~~ say err state)>.
120
121 =cut
122
123 sub import {
124     my $class = shift;
125     if (@_ == 0) {
126         croak("No features specified");
127     }
128     while (@_) {
129         my $name = shift(@_);
130         if ($name =~ /^:(.*)/) {
131             if (!exists $feature_bundle{$1}) {
132                 unknown_feature_bundle($1);
133             }
134             unshift @_, @{$feature_bundle{$1}};
135             next;
136         }
137         if (!exists $feature{$name}) {
138             unknown_feature($name);
139         }
140         $^H{$feature{$name}} = 1;
141     }
142 }
143
144 sub unimport {
145     my $class = shift;
146
147     # A bare C<no feature> should disable *all* features
148     if (!@_) {
149         delete @^H{ values(%feature) };
150         return;
151     }
152
153     while (@_) {
154         my $name = shift;
155         if ($name =~ /^:(.*)/) {
156             if (!exists $feature_bundle{$1}) {
157                 unknown_feature_bundle($1);
158             }
159             unshift @_, @{$feature_bundle{$1}};
160             next;
161         }
162         if (!exists($feature{$name})) {
163             unknown_feature($name);
164         }
165         else {
166             delete $^H{$feature{$name}};
167         }
168     }
169 }
170
171 sub unknown_feature {
172     my $feature = shift;
173     croak(sprintf('Feature "%s" is not supported by Perl %vd',
174             $feature, $^V));
175 }
176
177 sub unknown_feature_bundle {
178     my $feature = shift;
179     croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
180             $feature, $^V));
181 }
182
183 sub croak {
184     require Carp;
185     Carp::croak(@_);
186 }
187
188 1;