1feature(3pm) Perl Programmers Reference Guide feature(3pm)
2
3
4
6 feature - Perl pragma to enable new syntactic 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 can be enabled by "use feature 'foo'",
24 and will be parsed only when the appropriate feature pragma is in
25 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
70 It's possible to load a whole slew of features in one go, using a
71 feature bundle. The name of a feature bundle is prefixed with a colon,
72 to distinguish it from an actual feature. At present, the only feature
73 bundle is "use feature ":5.10"" which is equivalent to "use feature
74 qw(switch say state)".
75
76 Specifying sub-versions such as the 0 in 5.10.0 in feature bundles has
77 no effect: feature bundles are guaranteed to be the same for all sub-
78 versions.
79
81 There are two ways to load the "feature" pragma implicitly :
82
83 · By using the "-E" switch on the command-line instead of "-e". It
84 enables all available features in the main compilation unit (that
85 is, the one-liner.)
86
87 · By requiring explicitly a minimal Perl version number for your
88 program, with the "use VERSION" construct, and when the version is
89 higher than or equal to 5.10.0. That is,
90
91 use 5.10.0;
92
93 will do an implicit
94
95 use feature ':5.10';
96
97 and so on. Note how the trailing sub-version is automatically
98 stripped from the version.
99
100 But to avoid portability warnings (see "use" in perlfunc), you may
101 prefer:
102
103 use 5.010;
104
105 with the same effect.
106
107
108
109perl v5.10.1 2009-02-12 feature(3pm)