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