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)};
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 'err' feature
92 C<use feature 'err'> tells the compiler to enable the C<err>
95 C<err> is a low-precedence variant of the C<//> operator:
96 see C<perlop> for details.
98 =head2 the 'state' feature
100 C<use feature 'state'> tells the compiler to enable C<state>
103 See L<perlsub/"Persistent Private Variables"> for details.
105 =head1 FEATURE BUNDLES
107 It's possible to load a whole slew of features in one go, using
108 a I<feature bundle>. The name of a feature bundle is prefixed with
109 a colon, to distinguish it from an actual feature. At present, the
110 only feature bundles are C<use feature ":5.10"> and C<use feature ":5.10.0">,
111 which both are equivalent to C<use feature qw(switch say err state)>.
113 In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be
114 equivalent to the latest C<use feature ":5.10.X">.
121 croak("No features specified");
124 my $name = shift(@_);
125 if ($name =~ /^:(.*)/) {
126 if (!exists $feature_bundle{$1}) {
127 unknown_feature_bundle($1);
129 unshift @_, @{$feature_bundle{$1}};
132 if (!exists $feature{$name}) {
133 unknown_feature($name);
135 $^H{$feature{$name}} = 1;
142 # A bare C<no feature> should disable *all* features
144 delete @^H{ values(%feature) };
150 if ($name =~ /^:(.*)/) {
151 if (!exists $feature_bundle{$1}) {
152 unknown_feature_bundle($1);
154 unshift @_, @{$feature_bundle{$1}};
157 if (!exists($feature{$name})) {
158 unknown_feature($name);
161 delete $^H{$feature{$name}};
166 sub unknown_feature {
168 croak(sprintf('Feature "%s" is not supported by Perl %vd',
172 sub unknown_feature_bundle {
174 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',