5 # (feature name) => (internal name, used in %^H)
7 switch => 'feature_switch',
12 state => "feature_state",
15 my %feature_bundle = (
16 "5.10" => [qw(switch ~~ say err state)],
21 # - think about versioned features (use feature switch => 2)
25 feature - Perl pragma to enable new syntactic features
29 use feature qw(switch say);
31 when (1) { say "\$foo == 1" }
32 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
33 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
34 when ($_ > 100) { say "\$foo > 100" }
35 default { say "None of the above" }
40 It is usually impossible to add new syntax to Perl without breaking
41 some existing programs. This pragma provides a way to minimize that
42 risk. New syntactic constructs can be enabled by C<use feature 'foo'>,
43 and will be parsed only when the appropriate feature pragma is in
48 Like other pragmas (C<use strict>, for example), features have a lexical
49 effect. C<use feature qw(foo)> will only make the feature "foo" available
50 from that point to the end of the enclosing block.
54 say "say is available here";
56 print "But not here.\n";
58 =head2 The 'switch' feature
60 C<use feature 'switch'> tells the compiler to enable the Perl 6
63 See L<perlsyn/"Switch statements"> for details.
65 =head2 The '~~' feature
67 C<use feature '~~'> tells the compiler to enable the Perl 6
68 smart match C<~~> operator.
70 See L<perlsyn/"Smart Matching in Detail"> for details.
72 =head2 The 'say' feature
74 C<use feature 'say'> tells the compiler to enable the Perl 6
77 See L<perlfunc/say> for details.
79 =head2 the 'err' feature
81 C<use feature 'err'> tells the compiler to enable the C<err>
84 C<err> is a low-precedence variant of the C<//> operator:
85 see C<perlop> for details.
87 =head2 the 'dor' feature
89 The 'dor' feature is an alias for the 'err' feature.
91 =head2 the 'state' feature
93 C<use feature 'state'> tells the compiler to enable C<state>
96 See L<perlsub/"Persistent Private Variables"> for details.
98 =head1 FEATURE BUNDLES
100 It's possible to load a whole slew of features in one go, using
101 a I<feature bundle>. The name of a feature bundle is prefixed with
102 a colon, to distinguish it from an actual feature. At present, the
103 only feature bundle is C<use feature ":5.10">, which is equivalent
104 to C<use feature qw(switch ~~ say err state)>.
111 croak("No features specified");
114 my $name = shift(@_);
115 if ($name =~ /^:(.*)/) {
116 if (!exists $feature_bundle{$1}) {
117 unknown_feature_bundle($1);
119 unshift @_, @{$feature_bundle{$1}};
122 if (!exists $feature{$name}) {
123 unknown_feature($name);
125 $^H{$feature{$name}} = 1;
132 # A bare C<no feature> should disable *all* features
134 delete @^H{ values(%feature) };
140 if ($name =~ /^:(.*)/) {
141 if (!exists $feature_bundle{$1}) {
142 unknown_feature_bundle($1);
144 unshift @_, @{$feature_bundle{$1}};
147 if (!exists($feature{$name})) {
148 unknown_feature($name);
151 delete $^H{$feature{$name}};
156 sub unknown_feature {
158 croak(sprintf('Feature "%s" is not supported by Perl %vd',
162 sub unknown_feature_bundle {
164 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',