5 # (feature name) => (internal name, used in %^H)
7 switch => 'feature_switch',
11 state => "feature_state",
14 my %feature_bundle = (
15 "5.10.0" => [qw(switch ~~ say err state)],
18 # keep it harcoded until we actually bump the version number to 5.10
19 $feature_bundle{"5.10"} = $feature_bundle{"5.10.0"};
20 #$feature_bundle{"5.10"} = $feature_bundle{sprintf("%vd",$^V)};
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" }
42 It is usually impossible to add new syntax to Perl without breaking
43 some existing programs. This pragma provides a way to minimize that
44 risk. New syntactic constructs can be enabled by C<use feature 'foo'>,
45 and will be parsed only when the appropriate feature pragma is in
50 Like other pragmas (C<use strict>, for example), features have a lexical
51 effect. C<use feature qw(foo)> will only make the feature "foo" available
52 from that point to the end of the enclosing block.
56 say "say is available here";
58 print "But not here.\n";
62 Features can also be turned off by using C<no feature "foo">. This too
66 say "say is available here";
69 print "But not here.\n";
71 say "Yet it is here.";
73 C<no feature> with no features specified will turn off all features.
75 =head2 The 'switch' feature
77 C<use feature 'switch'> tells the compiler to enable the Perl 6
80 See L<perlsyn/"Switch statements"> for details.
82 =head2 The '~~' feature
84 C<use feature '~~'> tells the compiler to enable the Perl 6
85 smart match C<~~> operator.
87 See L<perlsyn/"Smart matching in detail"> for details.
89 =head2 The 'say' feature
91 C<use feature 'say'> tells the compiler to enable the Perl 6
94 See L<perlfunc/say> for details.
96 =head2 the 'err' feature
98 C<use feature 'err'> tells the compiler to enable the C<err>
101 C<err> is a low-precedence variant of the C<//> operator:
102 see C<perlop> for details.
104 =head2 the 'state' feature
106 C<use feature 'state'> tells the compiler to enable C<state>
109 See L<perlsub/"Persistent Private Variables"> for details.
111 =head1 FEATURE BUNDLES
113 It's possible to load a whole slew of features in one go, using
114 a I<feature bundle>. The name of a feature bundle is prefixed with
115 a colon, to distinguish it from an actual feature. At present, the
116 only feature bundles are C<use feature ":5.10"> and C<use feature ":5.10.0">,
117 which both are equivalent to C<use feature qw(switch ~~ say err state)>.
119 In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be
120 equivalent to the latest C<use feature ":5.10.X">.
127 croak("No features specified");
130 my $name = shift(@_);
131 if ($name =~ /^:(.*)/) {
132 if (!exists $feature_bundle{$1}) {
133 unknown_feature_bundle($1);
135 unshift @_, @{$feature_bundle{$1}};
138 if (!exists $feature{$name}) {
139 unknown_feature($name);
141 $^H{$feature{$name}} = 1;
148 # A bare C<no feature> should disable *all* features
150 delete @^H{ values(%feature) };
156 if ($name =~ /^:(.*)/) {
157 if (!exists $feature_bundle{$1}) {
158 unknown_feature_bundle($1);
160 unshift @_, @{$feature_bundle{$1}};
163 if (!exists($feature{$name})) {
164 unknown_feature($name);
167 delete $^H{$feature{$name}};
172 sub unknown_feature {
174 croak(sprintf('Feature "%s" is not supported by Perl %vd',
178 sub unknown_feature_bundle {
180 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',