5 # (feature name) => (internal name, used in %^H)
7 switch => 'feature_switch',
9 state => "feature_state",
12 my %feature_bundle = (
13 "5.10.0" => [qw(switch say state)],
17 $feature_bundle{"5.10"} = $feature_bundle{sprintf("%vd",$^V)};
19 $feature_bundle{"5.9.5"} = $feature_bundle{"5.10.0"};
22 # - think about versioned features (use feature switch => 2)
26 feature - Perl pragma to enable new syntactic features
30 use feature qw(switch say);
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" }
39 use feature ':5.10'; # loads all features available in perl 5.10
43 It is usually impossible to add new syntax to Perl without breaking
44 some existing programs. This pragma provides a way to minimize that
45 risk. New syntactic constructs can be enabled by C<use feature 'foo'>,
46 and will be parsed only when the appropriate feature pragma is in
51 Like other pragmas (C<use strict>, for example), features have a lexical
52 effect. C<use feature qw(foo)> will only make the feature "foo" available
53 from that point to the end of the enclosing block.
57 say "say is available here";
59 print "But not here.\n";
63 Features can also be turned off by using C<no feature "foo">. This too
67 say "say is available here";
70 print "But not here.\n";
72 say "Yet it is here.";
74 C<no feature> with no features specified will turn off all features.
76 =head2 The 'switch' feature
78 C<use feature 'switch'> tells the compiler to enable the Perl 6
81 See L<perlsyn/"Switch statements"> for details.
83 =head2 The 'say' feature
85 C<use feature 'say'> tells the compiler to enable the Perl 6
88 See L<perlfunc/say> for details.
90 =head2 the 'state' feature
92 C<use feature 'state'> tells the compiler to enable C<state>
95 See L<perlsub/"Persistent Private Variables"> for details.
97 =head1 FEATURE BUNDLES
99 It's possible to load a whole slew of features in one go, using
100 a I<feature bundle>. The name of a feature bundle is prefixed with
101 a colon, to distinguish it from an actual feature. At present, the
102 only feature bundles are C<use feature ":5.10"> and C<use feature ":5.10.0">,
103 which both are equivalent to C<use feature qw(switch say state)>.
105 In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be
106 equivalent to the latest C<use feature ":5.10.X">.
108 =head1 IMPLICIT LOADING
110 There are two ways to load the C<feature> pragma implicitly :
116 By using the C<-E> switch on the command-line instead of C<-e>. It enables
117 all available features in the main compilation unit (that is, the one-liner.)
121 By requiring explicitly a minimal Perl version number for your program, with
122 the C<use VERSION> construct, and when the version is higher than or equal to
129 use feature ':5.9.5';
140 croak("No features specified");
143 my $name = shift(@_);
144 if (substr($name, 0, 1) eq ":") {
145 my $v = substr($name, 1);
146 if (!exists $feature_bundle{$v}) {
147 unknown_feature_bundle($v);
149 unshift @_, @{$feature_bundle{$v}};
152 if (!exists $feature{$name}) {
153 unknown_feature($name);
155 $^H{$feature{$name}} = 1;
162 # A bare C<no feature> should disable *all* features
164 delete @^H{ values(%feature) };
170 if (substr($name, 0, 1) eq ":") {
171 my $v = substr($name, 1);
172 if (!exists $feature_bundle{$v}) {
173 unknown_feature_bundle($v);
175 unshift @_, @{$feature_bundle{$v}};
178 if (!exists($feature{$name})) {
179 unknown_feature($name);
182 delete $^H{$feature{$name}};
187 sub unknown_feature {
189 croak(sprintf('Feature "%s" is not supported by Perl %vd',
193 sub unknown_feature_bundle {
195 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',