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 WARNING: Because the smartmatch operator is experimental, Perl will
76 warn when you use this feature, unless you have explicitly disabled the
77 warning:
78
79 no warnings "experimental::smartmatch";
80
81 "use feature 'switch'" tells the compiler to enable the Perl 6
82 given/when construct.
83
84 See "Switch Statements" in perlsyn for details.
85
86 This feature is available starting with Perl 5.10.
87
88 The 'unicode_strings' feature
89 "use feature 'unicode_strings'" tells the compiler to use Unicode rules
90 in all string operations executed within its scope (unless they are
91 also within the scope of either "use locale" or "use bytes"). The same
92 applies to all regular expressions compiled within the scope, even if
93 executed outside it. It does not change the internal representation of
94 strings, but only how they are interpreted.
95
96 "no feature 'unicode_strings'" tells the compiler to use the
97 traditional Perl rules wherein the native character set rules is used
98 unless it is clear to Perl that Unicode is desired. This can lead to
99 some surprises when the behavior suddenly changes. (See "The "Unicode
100 Bug"" in perlunicode for details.) For this reason, if you are
101 potentially using Unicode in your program, the "use feature
102 'unicode_strings'" subpragma is strongly recommended.
103
104 This feature is available starting with Perl 5.12; was almost fully
105 implemented in Perl 5.14; and extended in Perl 5.16 to cover
106 "quotemeta"; was extended further in Perl 5.26 to cover the range
107 operator; and was extended again in Perl 5.28 to cover special-cased
108 whitespace splitting.
109
110 The 'unicode_eval' and 'evalbytes' features
111 Together, these two features are intended to replace the legacy string
112 "eval" function, which behaves problematically in some instances. They
113 are available starting with Perl 5.16, and are enabled by default by a
114 "use 5.16" or higher declaration.
115
116 "unicode_eval" changes the behavior of plain string "eval" to work more
117 consistently, especially in the Unicode world. Certain (mis)behaviors
118 couldn't be changed without breaking some things that had come to rely
119 on them, so the feature can be enabled and disabled. Details are at
120 "Under the "unicode_eval" feature" in perlfunc.
121
122 "evalbytes" is like string "eval", but operating on a byte stream that
123 is not UTF-8 encoded. Details are at "evalbytes EXPR" in perlfunc.
124 Without a "use feature 'evalbytes'" nor a "use v5.16" (or higher)
125 declaration in the current scope, you can still access it by instead
126 writing "CORE::evalbytes".
127
128 The 'current_sub' feature
129 This provides the "__SUB__" token that returns a reference to the
130 current subroutine or "undef" outside of a subroutine.
131
132 This feature is available starting with Perl 5.16.
133
134 The 'array_base' feature
135 This feature supported the legacy $[ variable. See "$[" in perlvar.
136 It was on by default but disabled under "use v5.16" (see "IMPLICIT
137 LOADING", below) and unavailable since perl 5.30.
138
139 This feature is available under this name starting with Perl 5.16. In
140 previous versions, it was simply on all the time, and this pragma knew
141 nothing about it.
142
143 The 'fc' feature
144 "use feature 'fc'" tells the compiler to enable the "fc" function,
145 which implements Unicode casefolding.
146
147 See "fc" in perlfunc for details.
148
149 This feature is available from Perl 5.16 onwards.
150
151 The 'lexical_subs' feature
152 In Perl versions prior to 5.26, this feature enabled declaration of
153 subroutines via "my sub foo", "state sub foo" and "our sub foo" syntax.
154 See "Lexical Subroutines" in perlsub for details.
155
156 This feature is available from Perl 5.18 onwards. From Perl 5.18 to
157 5.24, it was classed as experimental, and Perl emitted a warning for
158 its usage, except when explicitly disabled:
159
160 no warnings "experimental::lexical_subs";
161
162 As of Perl 5.26, use of this feature no longer triggers a warning,
163 though the "experimental::lexical_subs" warning category still exists
164 (for compatibility with code that disables it). In addition, this
165 syntax is not only no longer experimental, but it is enabled for all
166 Perl code, regardless of what feature declarations are in scope.
167
168 The 'postderef' and 'postderef_qq' features
169 The 'postderef_qq' feature extends the applicability of postfix
170 dereference syntax so that postfix array and scalar dereference are
171 available in double-quotish interpolations. For example, it makes the
172 following two statements equivalent:
173
174 my $s = "[@{ $h->{a} }]";
175 my $s = "[$h->{a}->@*]";
176
177 This feature is available from Perl 5.20 onwards. In Perl 5.20 and
178 5.22, it was classed as experimental, and Perl emitted a warning for
179 its usage, except when explicitly disabled:
180
181 no warnings "experimental::postderef";
182
183 As of Perl 5.24, use of this feature no longer triggers a warning,
184 though the "experimental::postderef" warning category still exists (for
185 compatibility with code that disables it).
186
187 The 'postderef' feature was used in Perl 5.20 and Perl 5.22 to enable
188 postfix dereference syntax outside double-quotish interpolations. In
189 those versions, using it triggered the "experimental::postderef"
190 warning in the same way as the 'postderef_qq' feature did. As of Perl
191 5.24, this syntax is not only no longer experimental, but it is enabled
192 for all Perl code, regardless of what feature declarations are in
193 scope.
194
195 The 'signatures' feature
196 WARNING: This feature is still experimental and the implementation may
197 change in future versions of Perl. For this reason, Perl will warn
198 when you use the feature, unless you have explicitly disabled the
199 warning:
200
201 no warnings "experimental::signatures";
202
203 This enables unpacking of subroutine arguments into lexical variables
204 by syntax such as
205
206 sub foo ($left, $right) {
207 return $left + $right;
208 }
209
210 See "Signatures" in perlsub for details.
211
212 This feature is available from Perl 5.20 onwards.
213
214 The 'refaliasing' feature
215 WARNING: This feature is still experimental and the implementation may
216 change in future versions of Perl. For this reason, Perl will warn
217 when you use the feature, unless you have explicitly disabled the
218 warning:
219
220 no warnings "experimental::refaliasing";
221
222 This enables aliasing via assignment to references:
223
224 \$a = \$b; # $a and $b now point to the same scalar
225 \@a = \@b; # to the same array
226 \%a = \%b;
227 \&a = \&b;
228 foreach \%hash (@array_of_hash_refs) {
229 ...
230 }
231
232 See "Assigning to References" in perlref for details.
233
234 This feature is available from Perl 5.22 onwards.
235
236 The 'bitwise' feature
237 This makes the four standard bitwise operators ("& | ^ ~") treat their
238 operands consistently as numbers, and introduces four new dotted
239 operators ("&. |. ^. ~.") that treat their operands consistently as
240 strings. The same applies to the assignment variants ("&= |= ^= &.=
241 |.= ^.=").
242
243 See "Bitwise String Operators" in perlop for details.
244
245 This feature is available from Perl 5.22 onwards. Starting in Perl
246 5.28, "use v5.28" will enable the feature. Before 5.28, it was still
247 experimental and would emit a warning in the "experimental::bitwise"
248 category.
249
250 The 'declared_refs' feature
251 WARNING: This feature is still experimental and the implementation may
252 change in future versions of Perl. For this reason, Perl will warn
253 when you use the feature, unless you have explicitly disabled the
254 warning:
255
256 no warnings "experimental::declared_refs";
257
258 This allows a reference to a variable to be declared with "my",
259 "state", our "our", or localized with "local". It is intended mainly
260 for use in conjunction with the "refaliasing" feature. See "Declaring
261 a Reference to a Variable" in perlref for examples.
262
263 This feature is available from Perl 5.26 onwards.
264
266 It's possible to load multiple features together, using a feature
267 bundle. The name of a feature bundle is prefixed with a colon, to
268 distinguish it from an actual feature.
269
270 use feature ":5.10";
271
272 The following feature bundles are available:
273
274 bundle features included
275 --------- -----------------
276 :default
277
278 :5.10 say state switch
279
280 :5.12 say state switch unicode_strings
281
282 :5.14 say state switch unicode_strings
283
284 :5.16 say state switch unicode_strings
285 unicode_eval evalbytes current_sub fc
286
287 :5.18 say state switch unicode_strings
288 unicode_eval evalbytes current_sub fc
289
290 :5.20 say state switch unicode_strings
291 unicode_eval evalbytes current_sub fc
292
293 :5.22 say state switch unicode_strings
294 unicode_eval evalbytes current_sub fc
295
296 :5.24 say state switch unicode_strings
297 unicode_eval evalbytes current_sub fc
298 postderef_qq
299
300 :5.26 say state switch unicode_strings
301 unicode_eval evalbytes current_sub fc
302 postderef_qq
303
304 :5.28 say state switch unicode_strings
305 unicode_eval evalbytes current_sub fc
306 postderef_qq bitwise
307
308 :5.30 say state switch unicode_strings
309 unicode_eval evalbytes current_sub fc
310 postderef_qq bitwise
311
312 The ":default" bundle represents the feature set that is enabled before
313 any "use feature" or "no feature" declaration.
314
315 Specifying sub-versions such as the 0 in 5.14.0 in feature bundles has
316 no effect. Feature bundles are guaranteed to be the same for all sub-
317 versions.
318
319 use feature ":5.14.0"; # same as ":5.14"
320 use feature ":5.14.1"; # same as ":5.14"
321
323 Instead of loading feature bundles by name, it is easier to let Perl do
324 implicit loading of a feature bundle for you.
325
326 There are two ways to load the "feature" pragma implicitly:
327
328 · By using the "-E" switch on the Perl command-line instead of "-e".
329 That will enable the feature bundle for that version of Perl in the
330 main compilation unit (that is, the one-liner that follows "-E").
331
332 · By explicitly requiring a minimum Perl version number for your
333 program, with the "use VERSION" construct. That is,
334
335 use v5.10.0;
336
337 will do an implicit
338
339 no feature ':all';
340 use feature ':5.10';
341
342 and so on. Note how the trailing sub-version is automatically
343 stripped from the version.
344
345 But to avoid portability warnings (see "use" in perlfunc), you may
346 prefer:
347
348 use 5.010;
349
350 with the same effect.
351
352 If the required version is older than Perl 5.10, the ":default"
353 feature bundle is automatically loaded instead.
354
355 Unlike "use feature ":5.12"", saying "use v5.12" (or any higher
356 version) also does the equivalent of "use strict"; see "use" in
357 perlfunc for details.
358
359
360
361perl v5.30.2 2020-03-27 feature(3pm)