5 # (feature name) => (internal name, used in %^H)
7 switch => 'feature_switch',
9 state => "feature_state",
10 unicode_strings => "feature_unicode",
13 # This gets set (for now) in $^H as well as in %^H,
14 # for runtime speed of the uc/lc/ucfirst/lcfirst functions.
15 # See HINT_UNI_8_BIT in perl.h.
16 our $hint_uni8bit = 0x00000800;
18 # NB. the latest bundle must be loaded by the -E switch (see toke.c)
20 my %feature_bundle = (
21 "5.10" => [qw(switch say state)],
22 "5.11" => [qw(switch say state unicode_strings)],
23 "5.12" => [qw(switch say state unicode_strings)],
27 $feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
30 # - think about versioned features (use feature switch => 2)
34 feature - Perl pragma to enable new features
38 use feature qw(switch say);
40 when (1) { say "\$foo == 1" }
41 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
42 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
43 when ($_ > 100) { say "\$foo > 100" }
44 default { say "None of the above" }
47 use feature ':5.10'; # loads all features available in perl 5.10
51 It is usually impossible to add new syntax to Perl without breaking
52 some existing programs. This pragma provides a way to minimize that
53 risk. New syntactic constructs, or new semantic meanings to older
54 constructs, can be enabled by C<use feature 'foo'>, and will be parsed
55 only when the appropriate feature pragma is in scope.
59 Like other pragmas (C<use strict>, for example), features have a lexical
60 effect. C<use feature qw(foo)> will only make the feature "foo" available
61 from that point to the end of the enclosing block.
65 say "say is available here";
67 print "But not here.\n";
71 Features can also be turned off by using C<no feature "foo">. This too
75 say "say is available here";
78 print "But not here.\n";
80 say "Yet it is here.";
82 C<no feature> with no features specified will turn off all features.
84 =head2 The 'switch' feature
86 C<use feature 'switch'> tells the compiler to enable the Perl 6
89 See L<perlsyn/"Switch statements"> for details.
91 =head2 The 'say' feature
93 C<use feature 'say'> tells the compiler to enable the Perl 6
96 See L<perlfunc/say> 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 =head2 the 'unicode_strings' feature
107 C<use feature 'unicode_strings'> tells the compiler to treat
108 all strings outside of C<use locale> and C<use bytes> as Unicode. It is
109 available starting with Perl 5.11.3.
111 See L<perlunicode/The "Unicode Bug"> for details.
113 =head1 FEATURE BUNDLES
115 It's possible to load a whole slew of features in one go, using
116 a I<feature bundle>. The name of a feature bundle is prefixed with
117 a colon, to distinguish it from an actual feature. At present, the
118 only feature bundle is C<use feature ":5.10"> which is equivalent
119 to C<use feature qw(switch say state)>.
121 Specifying sub-versions such as the C<0> in C<5.10.0> in feature bundles has
122 no effect: feature bundles are guaranteed to be the same for all sub-versions.
124 =head1 IMPLICIT LOADING
126 There are two ways to load the C<feature> pragma implicitly :
132 By using the C<-E> switch on the command-line instead of C<-e>. It enables
133 all available features in the main compilation unit (that is, the one-liner.)
137 By requiring explicitly a minimal Perl version number for your program, with
138 the C<use VERSION> construct, and when the version is higher than or equal to
147 and so on. Note how the trailing sub-version is automatically stripped from the
150 But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
154 with the same effect.
163 croak("No features specified");
166 my $name = shift(@_);
167 if (substr($name, 0, 1) eq ":") {
168 my $v = substr($name, 1);
169 if (!exists $feature_bundle{$v}) {
170 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
171 if (!exists $feature_bundle{$v}) {
172 unknown_feature_bundle(substr($name, 1));
175 unshift @_, @{$feature_bundle{$v}};
178 if (!exists $feature{$name}) {
179 unknown_feature($name);
181 $^H{$feature{$name}} = 1;
182 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
189 # A bare C<no feature> should disable *all* features
191 delete @^H{ values(%feature) };
192 $^H &= ~ $hint_uni8bit;
198 if (substr($name, 0, 1) eq ":") {
199 my $v = substr($name, 1);
200 if (!exists $feature_bundle{$v}) {
201 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
202 if (!exists $feature_bundle{$v}) {
203 unknown_feature_bundle(substr($name, 1));
206 unshift @_, @{$feature_bundle{$v}};
209 if (!exists($feature{$name})) {
210 unknown_feature($name);
213 delete $^H{$feature{$name}};
214 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
219 sub unknown_feature {
221 croak(sprintf('Feature "%s" is not supported by Perl %vd',
225 sub unknown_feature_bundle {
227 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',