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
265 The 'isa' feature
266 This allows the use of the "isa" infix operator, which tests whether
267 the scalar given by the left operand is an object of the class given by
268 the right operand. See "Class Instance Operator" in perlop for more
269 details.
270
271 This feature is available from Perl 5.32 onwards.
272
273 The 'indirect' feature
274 This feature allows the use of indirect object syntax for method calls,
275 e.g. "new Foo 1, 2;". It is enabled by default, but can be turned off
276 to disallow indirect object syntax.
277
278 This feature is available under this name from Perl 5.32 onwards. In
279 previous versions, it was simply on all the time. To disallow (or warn
280 on) indirect object syntax on older Perls, see the indirect CPAN
281 module.
282
284 It's possible to load multiple features together, using a feature
285 bundle. The name of a feature bundle is prefixed with a colon, to
286 distinguish it from an actual feature.
287
288 use feature ":5.10";
289
290 The following feature bundles are available:
291
292 bundle features included
293 --------- -----------------
294 :default indirect
295
296 :5.10 say state switch indirect
297
298 :5.12 say state switch unicode_strings indirect
299
300 :5.14 say state switch unicode_strings indirect
301
302 :5.16 say state switch unicode_strings
303 unicode_eval evalbytes current_sub fc
304 indirect
305
306 :5.18 say state switch unicode_strings
307 unicode_eval evalbytes current_sub fc
308 indirect
309
310 :5.20 say state switch unicode_strings
311 unicode_eval evalbytes current_sub fc
312 indirect
313
314 :5.22 say state switch unicode_strings
315 unicode_eval evalbytes current_sub fc
316 indirect
317
318 :5.24 say state switch unicode_strings
319 unicode_eval evalbytes current_sub fc
320 postderef_qq indirect
321
322 :5.26 say state switch unicode_strings
323 unicode_eval evalbytes current_sub fc
324 postderef_qq indirect
325
326 :5.28 say state switch unicode_strings
327 unicode_eval evalbytes current_sub fc
328 postderef_qq bitwise indirect
329
330 :5.30 say state switch unicode_strings
331 unicode_eval evalbytes current_sub fc
332 postderef_qq bitwise indirect
333
334 :5.32 say state switch unicode_strings
335 unicode_eval evalbytes current_sub fc
336 postderef_qq bitwise indirect
337
338 The ":default" bundle represents the feature set that is enabled before
339 any "use feature" or "no feature" declaration.
340
341 Specifying sub-versions such as the 0 in 5.14.0 in feature bundles has
342 no effect. Feature bundles are guaranteed to be the same for all sub-
343 versions.
344
345 use feature ":5.14.0"; # same as ":5.14"
346 use feature ":5.14.1"; # same as ":5.14"
347
349 Instead of loading feature bundles by name, it is easier to let Perl do
350 implicit loading of a feature bundle for you.
351
352 There are two ways to load the "feature" pragma implicitly:
353
354 · By using the "-E" switch on the Perl command-line instead of "-e".
355 That will enable the feature bundle for that version of Perl in the
356 main compilation unit (that is, the one-liner that follows "-E").
357
358 · By explicitly requiring a minimum Perl version number for your
359 program, with the "use VERSION" construct. That is,
360
361 use v5.10.0;
362
363 will do an implicit
364
365 no feature ':all';
366 use feature ':5.10';
367
368 and so on. Note how the trailing sub-version is automatically
369 stripped from the version.
370
371 But to avoid portability warnings (see "use" in perlfunc), you may
372 prefer:
373
374 use 5.010;
375
376 with the same effect.
377
378 If the required version is older than Perl 5.10, the ":default"
379 feature bundle is automatically loaded instead.
380
381 Unlike "use feature ":5.12"", saying "use v5.12" (or any higher
382 version) also does the equivalent of "use strict"; see "use" in
383 perlfunc for details.
384
385
386
387perl v5.32.1 2021-03-31 feature(3pm)