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