5 # (feature name) => (internal name, used in %^H)
7 switch => 'feature_switch',
9 state => "feature_state",
12 # NB. the latest bundle must be loaded by the -E switch (see toke.c)
14 my %feature_bundle = (
15 "5.10.0" => [qw(switch say state)],
16 "5.11.0" => [qw(switch say state)],
20 $feature_bundle{"5.11"} = $feature_bundle{sprintf("%vd",$^V)};
22 $feature_bundle{"5.10"} = $feature_bundle{"5.10.0"};
23 $feature_bundle{"5.9.5"} = $feature_bundle{"5.10.0"};
26 # - think about versioned features (use feature switch => 2)
30 feature - Perl pragma to enable new syntactic features
34 use feature qw(switch say);
36 when (1) { say "\$foo == 1" }
37 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
38 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
39 when ($_ > 100) { say "\$foo > 100" }
40 default { say "None of the above" }
43 use feature ':5.10'; # loads all features available in perl 5.10
47 It is usually impossible to add new syntax to Perl without breaking
48 some existing programs. This pragma provides a way to minimize that
49 risk. New syntactic constructs can be enabled by C<use feature 'foo'>,
50 and will be parsed only when the appropriate feature pragma is in
55 Like other pragmas (C<use strict>, for example), features have a lexical
56 effect. C<use feature qw(foo)> will only make the feature "foo" available
57 from that point to the end of the enclosing block.
61 say "say is available here";
63 print "But not here.\n";
67 Features can also be turned off by using C<no feature "foo">. This too
71 say "say is available here";
74 print "But not here.\n";
76 say "Yet it is here.";
78 C<no feature> with no features specified will turn off all features.
80 =head2 The 'switch' feature
82 C<use feature 'switch'> tells the compiler to enable the Perl 6
85 See L<perlsyn/"Switch statements"> for details.
87 =head2 The 'say' feature
89 C<use feature 'say'> tells the compiler to enable the Perl 6
92 See L<perlfunc/say> for details.
94 =head2 the 'state' feature
96 C<use feature 'state'> tells the compiler to enable C<state>
99 See L<perlsub/"Persistent Private Variables"> for details.
101 =head1 FEATURE BUNDLES
103 It's possible to load a whole slew of features in one go, using
104 a I<feature bundle>. The name of a feature bundle is prefixed with
105 a colon, to distinguish it from an actual feature. At present, the
106 only feature bundles are C<use feature ":5.10"> and C<use feature ":5.10.0">,
107 which both are equivalent to C<use feature qw(switch say state)>.
109 In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be
110 equivalent to the latest C<use feature ":5.10.X">.
112 =head1 IMPLICIT LOADING
114 There are two ways to load the C<feature> pragma implicitly :
120 By using the C<-E> switch on the command-line instead of C<-e>. It enables
121 all available features in the main compilation unit (that is, the one-liner.)
125 By requiring explicitly a minimal Perl version number for your program, with
126 the C<use VERSION> construct, and when the version is higher than or equal to
133 use feature ':5.10.0';
137 But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
141 with the same effect.
150 croak("No features specified");
153 my $name = shift(@_);
154 if (substr($name, 0, 1) eq ":") {
155 my $v = substr($name, 1);
156 if (!exists $feature_bundle{$v}) {
157 unknown_feature_bundle($v);
159 unshift @_, @{$feature_bundle{$v}};
162 if (!exists $feature{$name}) {
163 unknown_feature($name);
165 $^H{$feature{$name}} = 1;
172 # A bare C<no feature> should disable *all* features
174 delete @^H{ values(%feature) };
180 if (substr($name, 0, 1) eq ":") {
181 my $v = substr($name, 1);
182 if (!exists $feature_bundle{$v}) {
183 unknown_feature_bundle($v);
185 unshift @_, @{$feature_bundle{$v}};
188 if (!exists($feature{$name})) {
189 unknown_feature($name);
192 delete $^H{$feature{$name}};
197 sub unknown_feature {
199 croak(sprintf('Feature "%s" is not supported by Perl %vd',
203 sub unknown_feature_bundle {
205 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',