5 # (feature name) => (internal name, used in %^H)
7 switch => 'feature_switch',
10 state => "feature_state",
13 my %feature_bundle = (
14 "5.10.0" => [qw(switch say err state)],
18 $feature_bundle{"5.10"} = $feature_bundle{sprintf("%vd",$^V)};
20 $feature_bundle{"5.9.5"} = $feature_bundle{"5.10.0"};
23 # - think about versioned features (use feature switch => 2)
27 feature - Perl pragma to enable new syntactic features
31 use feature qw(switch say);
33 when (1) { say "\$foo == 1" }
34 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
35 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
36 when ($_ > 100) { say "\$foo > 100" }
37 default { say "None of the above" }
40 use feature ':5.10'; # loads all features available in perl 5.10
44 It is usually impossible to add new syntax to Perl without breaking
45 some existing programs. This pragma provides a way to minimize that
46 risk. New syntactic constructs can be enabled by C<use feature 'foo'>,
47 and will be parsed only when the appropriate feature pragma is in
52 Like other pragmas (C<use strict>, for example), features have a lexical
53 effect. C<use feature qw(foo)> will only make the feature "foo" available
54 from that point to the end of the enclosing block.
58 say "say is available here";
60 print "But not here.\n";
64 Features can also be turned off by using C<no feature "foo">. This too
68 say "say is available here";
71 print "But not here.\n";
73 say "Yet it is here.";
75 C<no feature> with no features specified will turn off all features.
77 =head2 The 'switch' feature
79 C<use feature 'switch'> tells the compiler to enable the Perl 6
82 See L<perlsyn/"Switch statements"> for details.
84 =head2 The 'say' feature
86 C<use feature 'say'> tells the compiler to enable the Perl 6
89 See L<perlfunc/say> for details.
91 =head2 the 'err' feature
93 C<use feature 'err'> tells the compiler to enable the C<err>
96 C<err> is a low-precedence variant of the C<//> operator:
97 see C<perlop> for details.
99 =head2 the 'state' feature
101 C<use feature 'state'> tells the compiler to enable C<state>
104 See L<perlsub/"Persistent Private Variables"> for details.
106 =head1 FEATURE BUNDLES
108 It's possible to load a whole slew of features in one go, using
109 a I<feature bundle>. The name of a feature bundle is prefixed with
110 a colon, to distinguish it from an actual feature. At present, the
111 only feature bundles are C<use feature ":5.10"> and C<use feature ":5.10.0">,
112 which both are equivalent to C<use feature qw(switch say err state)>.
114 In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be
115 equivalent to the latest C<use feature ":5.10.X">.
117 =head1 IMPLICIT LOADING
119 There are two ways to load the C<feature> pragma implicitly :
125 By using the C<-E> switch on the command-line instead of C<-e>. It enables
126 all available features in the main compilation unit (that is, the one-liner.)
130 By requiring explicitly a minimal Perl version number for your program, with
131 the C<use VERSION> construct, and when the version is higher than or equal to
138 use feature ':5.9.5';
149 croak("No features specified");
152 my $name = shift(@_);
153 if (substr($name, 0, 1) eq ":") {
154 my $v = substr($name, 1);
155 if (!exists $feature_bundle{$v}) {
156 unknown_feature_bundle($v);
158 unshift @_, @{$feature_bundle{$v}};
161 if (!exists $feature{$name}) {
162 unknown_feature($name);
164 $^H{$feature{$name}} = 1;
171 # A bare C<no feature> should disable *all* features
173 delete @^H{ values(%feature) };
179 if (substr($name, 0, 1) eq ":") {
180 my $v = substr($name, 1);
181 if (!exists $feature_bundle{$v}) {
182 unknown_feature_bundle($v);
184 unshift @_, @{$feature_bundle{$v}};
187 if (!exists($feature{$name})) {
188 unknown_feature($name);
191 delete $^H{$feature{$name}};
196 sub unknown_feature {
198 croak(sprintf('Feature "%s" is not supported by Perl %vd',
202 sub unknown_feature_bundle {
204 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',