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)],
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)};
21 $feature_bundle{"5.9.5"} = $feature_bundle{"5.10.0"};
24 # - think about versioned features (use feature switch => 2)
28 feature - Perl pragma to enable new syntactic features
32 use feature qw(switch say);
34 when (1) { say "\$foo == 1" }
35 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
36 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
37 when ($_ > 100) { say "\$foo > 100" }
38 default { say "None of the above" }
41 use feature ':5.10'; # loads all features available in perl 5.10
45 It is usually impossible to add new syntax to Perl without breaking
46 some existing programs. This pragma provides a way to minimize that
47 risk. New syntactic constructs can be enabled by C<use feature 'foo'>,
48 and will be parsed only when the appropriate feature pragma is in
53 Like other pragmas (C<use strict>, for example), features have a lexical
54 effect. C<use feature qw(foo)> will only make the feature "foo" available
55 from that point to the end of the enclosing block.
59 say "say is available here";
61 print "But not here.\n";
65 Features can also be turned off by using C<no feature "foo">. This too
69 say "say is available here";
72 print "But not here.\n";
74 say "Yet it is here.";
76 C<no feature> with no features specified will turn off all features.
78 =head2 The 'switch' feature
80 C<use feature 'switch'> tells the compiler to enable the Perl 6
83 See L<perlsyn/"Switch statements"> for details.
85 =head2 The 'say' feature
87 C<use feature 'say'> tells the compiler to enable the Perl 6
90 See L<perlfunc/say> for details.
92 =head2 the 'err' feature
94 C<use feature 'err'> tells the compiler to enable the C<err>
97 C<err> is a low-precedence variant of the C<//> operator:
98 see C<perlop> for details.
100 =head2 the 'state' feature
102 C<use feature 'state'> tells the compiler to enable C<state>
105 See L<perlsub/"Persistent Private Variables"> for details.
107 =head1 FEATURE BUNDLES
109 It's possible to load a whole slew of features in one go, using
110 a I<feature bundle>. The name of a feature bundle is prefixed with
111 a colon, to distinguish it from an actual feature. At present, the
112 only feature bundles are C<use feature ":5.10"> and C<use feature ":5.10.0">,
113 which both are equivalent to C<use feature qw(switch say err state)>.
115 In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be
116 equivalent to the latest C<use feature ":5.10.X">.
118 =head1 IMPLICIT LOADING
120 There are two ways to load the C<feature> pragma implicitly :
126 By using the C<-E> switch on the command-line instead of C<-e>. It enables
127 all available features in the main compilation unit (that is, the one-liner.)
131 By requiring explicitly a minimal Perl version number for your program, with
132 the C<use VERSION> construct, and when the version is higher than or equal to
139 use feature ':5.9.5';
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',