1feature(3pm) Perl Programmers Reference Guide feature(3pm)
2
3
4
6 feature - Perl pragma to enable new features
7
9 use feature qw(switch say);
10 given ($foo) {
11 when (1) { say "\$foo == 1" }
12 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
13 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
14 when ($_ > 100) { say "\$foo > 100" }
15 default { say "None of the above" }
16 }
17
18 use feature ':5.10'; # loads all features available in perl 5.10
19
21 It is usually impossible to add new syntax to Perl without breaking
22 some existing programs. This pragma provides a way to minimize that
23 risk. New syntactic constructs, or new semantic meanings to older
24 constructs, can be enabled by "use feature 'foo'", and will be parsed
25 only when the appropriate feature pragma is in scope.
26
27 Lexical effect
28 Like other pragmas ("use strict", for example), features have a lexical
29 effect. "use feature qw(foo)" will only make the feature "foo"
30 available from that point to the end of the enclosing block.
31
32 {
33 use feature 'say';
34 say "say is available here";
35 }
36 print "But not here.\n";
37
38 "no feature"
39 Features can also be turned off by using "no feature "foo"". This too
40 has lexical effect.
41
42 use feature 'say';
43 say "say is available here";
44 {
45 no feature 'say';
46 print "But not here.\n";
47 }
48 say "Yet it is here.";
49
50 "no feature" with no features specified will turn off all features.
51
52 The 'switch' feature
53 "use feature 'switch'" tells the compiler to enable the Perl 6
54 given/when construct.
55
56 See "Switch statements" in perlsyn for details.
57
58 The 'say' feature
59 "use feature 'say'" tells the compiler to enable the Perl 6 "say"
60 function.
61
62 See "say" in perlfunc for details.
63
64 the 'state' feature
65 "use feature 'state'" tells the compiler to enable "state" variables.
66
67 See "Persistent Private Variables" in perlsub for details.
68
69 the 'unicode_strings' feature
70 "use feature 'unicode_strings'" tells the compiler to treat all strings
71 outside of "use locale" and "use bytes" as Unicode. It is available
72 starting with Perl 5.11.3.
73
74 See "The "Unicode Bug"" in perlunicode for details.
75
77 It's possible to load a whole slew of features in one go, using a
78 feature bundle. The name of a feature bundle is prefixed with a colon,
79 to distinguish it from an actual feature. At present, the only feature
80 bundle is "use feature ":5.10"" which is equivalent to "use feature
81 qw(switch say state)".
82
83 Specifying sub-versions such as the 0 in 5.10.0 in feature bundles has
84 no effect: feature bundles are guaranteed to be the same for all sub-
85 versions.
86
88 There are two ways to load the "feature" pragma implicitly :
89
90 · By using the "-E" switch on the command-line instead of "-e". It
91 enables all available features in the main compilation unit (that
92 is, the one-liner.)
93
94 · By requiring explicitly a minimal Perl version number for your
95 program, with the "use VERSION" construct, and when the version is
96 higher than or equal to 5.10.0. That is,
97
98 use 5.10.0;
99
100 will do an implicit
101
102 use feature ':5.10';
103
104 and so on. Note how the trailing sub-version is automatically
105 stripped from the version.
106
107 But to avoid portability warnings (see "use" in perlfunc), you may
108 prefer:
109
110 use 5.010;
111
112 with the same effect.
113
114
115
116perl v5.12.4 2011-06-07 feature(3pm)