1feature(3pm) Perl Programmers Reference Guide feature(3pm)
2
3
4
6 feature - Perl pragma to enable new features
7
9 use feature qw(say switch);
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
20 use v5.10; # implicitly loads :5.10 feature bundle
21
23 It is usually impossible to add new syntax to Perl without breaking
24 some existing programs. This pragma provides a way to minimize that
25 risk. New syntactic constructs, or new semantic meanings to older
26 constructs, can be enabled by "use feature 'foo'", and will be parsed
27 only when the appropriate feature pragma is in scope. (Nevertheless,
28 the "CORE::" prefix provides access to all Perl keywords, regardless of
29 this pragma.)
30
31 Lexical effect
32 Like other pragmas ("use strict", for example), features have a lexical
33 effect. "use feature qw(foo)" will only make the feature "foo"
34 available from that point to the end of the enclosing block.
35
36 {
37 use feature 'say';
38 say "say is available here";
39 }
40 print "But not here.\n";
41
42 "no feature"
43 Features can also be turned off by using "no feature "foo"". This too
44 has lexical effect.
45
46 use feature 'say';
47 say "say is available here";
48 {
49 no feature 'say';
50 print "But not here.\n";
51 }
52 say "Yet it is here.";
53
54 "no feature" with no features specified will reset to the default
55 group. To disable all features (an unusual request!) use "no feature
56 ':all'".
57
59 The 'say' feature
60 "use feature 'say'" tells the compiler to enable the Perl 6 style "say"
61 function.
62
63 See "say" in perlfunc for details.
64
65 This feature is available starting with Perl 5.10.
66
67 The 'state' feature
68 "use feature 'state'" tells the compiler to enable "state" variables.
69
70 See "Persistent Private Variables" in perlsub for details.
71
72 This feature is available starting with Perl 5.10.
73
74 The 'switch' feature
75 "use feature 'switch'" tells the compiler to enable the Perl 6
76 given/when construct.
77
78 See "Switch Statements" in perlsyn for details.
79
80 This feature is available starting with Perl 5.10.
81
82 The 'unicode_strings' feature
83 "use feature 'unicode_strings'" tells the compiler to use Unicode
84 semantics in all string operations executed within its scope (unless
85 they are also within the scope of either "use locale" or "use bytes").
86 The same applies to all regular expressions compiled within the scope,
87 even if executed outside it.
88
89 "no feature 'unicode_strings'" tells the compiler to use the
90 traditional Perl semantics wherein the native character set semantics
91 is used unless it is clear to Perl that Unicode is desired. This can
92 lead to some surprises when the behavior suddenly changes. (See "The
93 "Unicode Bug"" in perlunicode for details.) For this reason, if you
94 are potentially using Unicode in your program, the "use feature
95 'unicode_strings'" subpragma is strongly recommended.
96
97 This feature is available starting with Perl 5.12; was almost fully
98 implemented in Perl 5.14; and extended in Perl 5.16 to cover
99 "quotemeta".
100
101 The 'unicode_eval' and 'evalbytes' features
102 Under the "unicode_eval" feature, Perl's "eval" function, when passed a
103 string, will evaluate it as a string of characters, ignoring any "use
104 utf8" declarations. "use utf8" exists to declare the encoding of the
105 script, which only makes sense for a stream of bytes, not a string of
106 characters. Source filters are forbidden, as they also really only
107 make sense on strings of bytes. Any attempt to activate a source
108 filter will result in an error.
109
110 The "evalbytes" feature enables the "evalbytes" keyword, which
111 evaluates the argument passed to it as a string of bytes. It dies if
112 the string contains any characters outside the 8-bit range. Source
113 filters work within "evalbytes": they apply to the contents of the
114 string being evaluated.
115
116 Together, these two features are intended to replace the historical
117 "eval" function, which has (at least) two bugs in it, that cannot
118 easily be fixed without breaking existing programs:
119
120 · "eval" behaves differently depending on the internal encoding of
121 the string, sometimes treating its argument as a string of bytes,
122 and sometimes as a string of characters.
123
124 · Source filters activated within "eval" leak out into whichever file
125 scope is currently being compiled. To give an example with the
126 CPAN module Semi::Semicolons:
127
128 BEGIN { eval "use Semi::Semicolons; # not filtered here " }
129 # filtered here!
130
131 "evalbytes" fixes that to work the way one would expect:
132
133 use feature "evalbytes";
134 BEGIN { evalbytes "use Semi::Semicolons; # filtered " }
135 # not filtered
136
137 These two features are available starting with Perl 5.16.
138
139 The 'current_sub' feature
140 This provides the "__SUB__" token that returns a reference to the
141 current subroutine or "undef" outside of a subroutine.
142
143 This feature is available starting with Perl 5.16.
144
145 The 'array_base' feature
146 This feature supports the legacy $[ variable. See "$[" in perlvar and
147 arybase. It is on by default but disabled under "use v5.16" (see
148 "IMPLICIT LOADING", below).
149
150 This feature is available under this name starting with Perl 5.16. In
151 previous versions, it was simply on all the time, and this pragma knew
152 nothing about it.
153
154 The 'fc' feature
155 "use feature 'fc'" tells the compiler to enable the "fc" function,
156 which implements Unicode casefolding.
157
158 See "fc" in perlfunc for details.
159
160 This feature is available from Perl 5.16 onwards.
161
163 It's possible to load multiple features together, using a feature
164 bundle. The name of a feature bundle is prefixed with a colon, to
165 distinguish it from an actual feature.
166
167 use feature ":5.10";
168
169 The following feature bundles are available:
170
171 bundle features included
172 --------- -----------------
173 :default array_base
174
175 :5.10 say state switch array_base
176
177 :5.12 say state switch unicode_strings array_base
178
179 :5.14 say state switch unicode_strings array_base
180
181 :5.16 say state switch unicode_strings
182 unicode_eval evalbytes current_sub fc
183
184 The ":default" bundle represents the feature set that is enabled before
185 any "use feature" or "no feature" declaration.
186
187 Specifying sub-versions such as the 0 in 5.14.0 in feature bundles has
188 no effect. Feature bundles are guaranteed to be the same for all sub-
189 versions.
190
191 use feature ":5.14.0"; # same as ":5.14"
192 use feature ":5.14.1"; # same as ":5.14"
193
195 Instead of loading feature bundles by name, it is easier to let Perl do
196 implicit loading of a feature bundle for you.
197
198 There are two ways to load the "feature" pragma implicitly:
199
200 · By using the "-E" switch on the Perl command-line instead of "-e".
201 That will enable the feature bundle for that version of Perl in the
202 main compilation unit (that is, the one-liner that follows "-E").
203
204 · By explicitly requiring a minimum Perl version number for your
205 program, with the "use VERSION" construct. That is,
206
207 use v5.10.0;
208
209 will do an implicit
210
211 no feature ':all';
212 use feature ':5.10';
213
214 and so on. Note how the trailing sub-version is automatically
215 stripped from the version.
216
217 But to avoid portability warnings (see "use" in perlfunc), you may
218 prefer:
219
220 use 5.010;
221
222 with the same effect.
223
224 If the required version is older than Perl 5.10, the ":default"
225 feature bundle is automatically loaded instead.
226
227
228
229perl v5.16.3 2013-03-04 feature(3pm)