xref: /openbsd-src/gnu/usr.bin/perl/lib/feature.pm (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1# -*- buffer-read-only: t -*-
2# !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
3# This file is built by regen/feature.pl.
4# Any changes made here will be lost!
5
6package feature;
7
8our $VERSION = '1.52';
9
10our %feature = (
11    fc              => 'feature_fc',
12    say             => 'feature_say',
13    state           => 'feature_state',
14    switch          => 'feature_switch',
15    bitwise         => 'feature_bitwise',
16    evalbytes       => 'feature_evalbytes',
17    array_base      => 'feature_arybase',
18    signatures      => 'feature_signatures',
19    current_sub     => 'feature___SUB__',
20    refaliasing     => 'feature_refaliasing',
21    postderef_qq    => 'feature_postderef_qq',
22    unicode_eval    => 'feature_unieval',
23    declared_refs   => 'feature_myref',
24    unicode_strings => 'feature_unicode',
25);
26
27our %feature_bundle = (
28    "5.10"    => [qw(array_base say state switch)],
29    "5.11"    => [qw(array_base say state switch unicode_strings)],
30    "5.15"    => [qw(current_sub evalbytes fc say state switch unicode_eval unicode_strings)],
31    "5.23"    => [qw(current_sub evalbytes fc postderef_qq say state switch unicode_eval unicode_strings)],
32    "5.27"    => [qw(bitwise current_sub evalbytes fc postderef_qq say state switch unicode_eval unicode_strings)],
33    "all"     => [qw(array_base bitwise current_sub declared_refs evalbytes fc postderef_qq refaliasing say signatures state switch unicode_eval unicode_strings)],
34    "default" => [qw(array_base)],
35);
36
37$feature_bundle{"5.12"} = $feature_bundle{"5.11"};
38$feature_bundle{"5.13"} = $feature_bundle{"5.11"};
39$feature_bundle{"5.14"} = $feature_bundle{"5.11"};
40$feature_bundle{"5.16"} = $feature_bundle{"5.15"};
41$feature_bundle{"5.17"} = $feature_bundle{"5.15"};
42$feature_bundle{"5.18"} = $feature_bundle{"5.15"};
43$feature_bundle{"5.19"} = $feature_bundle{"5.15"};
44$feature_bundle{"5.20"} = $feature_bundle{"5.15"};
45$feature_bundle{"5.21"} = $feature_bundle{"5.15"};
46$feature_bundle{"5.22"} = $feature_bundle{"5.15"};
47$feature_bundle{"5.24"} = $feature_bundle{"5.23"};
48$feature_bundle{"5.25"} = $feature_bundle{"5.23"};
49$feature_bundle{"5.26"} = $feature_bundle{"5.23"};
50$feature_bundle{"5.28"} = $feature_bundle{"5.27"};
51$feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
52my %noops = (
53    postderef => 1,
54    lexical_subs => 1,
55);
56
57our $hint_shift   = 26;
58our $hint_mask    = 0x1c000000;
59our @hint_bundles = qw( default 5.10 5.11 5.15 5.23 5.27 );
60
61# This gets set (for now) in $^H as well as in %^H,
62# for runtime speed of the uc/lc/ucfirst/lcfirst functions.
63# See HINT_UNI_8_BIT in perl.h.
64our $hint_uni8bit = 0x00000800;
65
66# TODO:
67# - think about versioned features (use feature switch => 2)
68
69=head1 NAME
70
71feature - Perl pragma to enable new features
72
73=head1 SYNOPSIS
74
75    use feature qw(say switch);
76    given ($foo) {
77        when (1)          { say "\$foo == 1" }
78        when ([2,3])      { say "\$foo == 2 || \$foo == 3" }
79        when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
80        when ($_ > 100)   { say "\$foo > 100" }
81        default           { say "None of the above" }
82    }
83
84    use feature ':5.10'; # loads all features available in perl 5.10
85
86    use v5.10;           # implicitly loads :5.10 feature bundle
87
88=head1 DESCRIPTION
89
90It is usually impossible to add new syntax to Perl without breaking
91some existing programs.  This pragma provides a way to minimize that
92risk. New syntactic constructs, or new semantic meanings to older
93constructs, can be enabled by C<use feature 'foo'>, and will be parsed
94only when the appropriate feature pragma is in scope.  (Nevertheless, the
95C<CORE::> prefix provides access to all Perl keywords, regardless of this
96pragma.)
97
98=head2 Lexical effect
99
100Like other pragmas (C<use strict>, for example), features have a lexical
101effect.  C<use feature qw(foo)> will only make the feature "foo" available
102from that point to the end of the enclosing block.
103
104    {
105        use feature 'say';
106        say "say is available here";
107    }
108    print "But not here.\n";
109
110=head2 C<no feature>
111
112Features can also be turned off by using C<no feature "foo">.  This too
113has lexical effect.
114
115    use feature 'say';
116    say "say is available here";
117    {
118        no feature 'say';
119        print "But not here.\n";
120    }
121    say "Yet it is here.";
122
123C<no feature> with no features specified will reset to the default group.  To
124disable I<all> features (an unusual request!) use C<no feature ':all'>.
125
126=head1 AVAILABLE FEATURES
127
128=head2 The 'say' feature
129
130C<use feature 'say'> tells the compiler to enable the Perl 6 style
131C<say> function.
132
133See L<perlfunc/say> for details.
134
135This feature is available starting with Perl 5.10.
136
137=head2 The 'state' feature
138
139C<use feature 'state'> tells the compiler to enable C<state>
140variables.
141
142See L<perlsub/"Persistent Private Variables"> for details.
143
144This feature is available starting with Perl 5.10.
145
146=head2 The 'switch' feature
147
148B<WARNING>: Because the L<smartmatch operator|perlop/"Smartmatch Operator"> is
149experimental, Perl will warn when you use this feature, unless you have
150explicitly disabled the warning:
151
152    no warnings "experimental::smartmatch";
153
154C<use feature 'switch'> tells the compiler to enable the Perl 6
155given/when construct.
156
157See L<perlsyn/"Switch Statements"> for details.
158
159This feature is available starting with Perl 5.10.
160
161=head2 The 'unicode_strings' feature
162
163C<use feature 'unicode_strings'> tells the compiler to use Unicode rules
164in all string operations executed within its scope (unless they are also
165within the scope of either C<use locale> or C<use bytes>).  The same applies
166to all regular expressions compiled within the scope, even if executed outside
167it.  It does not change the internal representation of strings, but only how
168they are interpreted.
169
170C<no feature 'unicode_strings'> tells the compiler to use the traditional
171Perl rules wherein the native character set rules is used unless it is
172clear to Perl that Unicode is desired.  This can lead to some surprises
173when the behavior suddenly changes.  (See
174L<perlunicode/The "Unicode Bug"> for details.)  For this reason, if you are
175potentially using Unicode in your program, the
176C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
177
178This feature is available starting with Perl 5.12; was almost fully
179implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>;
180was extended further in Perl 5.26 to cover L<the range
181operator|perlop/Range Operators>; and was extended again in Perl 5.28 to
182cover L<special-cased whitespace splitting|perlfunc/split>.
183
184=head2 The 'unicode_eval' and 'evalbytes' features
185
186Together, these two features are intended to replace the legacy string
187C<eval> function, which behaves problematically in some instances.  They are
188available starting with Perl 5.16, and are enabled by default by a
189S<C<use 5.16>> or higher declaration.
190
191C<unicode_eval> changes the behavior of plain string C<eval> to work more
192consistently, especially in the Unicode world.  Certain (mis)behaviors
193couldn't be changed without breaking some things that had come to rely on
194them, so the feature can be enabled and disabled.  Details are at
195L<perlfunc/Under the "unicode_eval" feature>.
196
197C<evalbytes> is like string C<eval>, but operating on a byte stream that is
198not UTF-8 encoded.  Details are at L<perlfunc/evalbytes EXPR>.  Without a
199S<C<use feature 'evalbytes'>> nor a S<C<use v5.16>> (or higher) declaration in
200the current scope, you can still access it by instead writing
201C<CORE::evalbytes>.
202
203=head2 The 'current_sub' feature
204
205This provides the C<__SUB__> token that returns a reference to the current
206subroutine or C<undef> outside of a subroutine.
207
208This feature is available starting with Perl 5.16.
209
210=head2 The 'array_base' feature
211
212This feature supports the legacy C<$[> variable.  See L<perlvar/$[> and
213L<arybase>.  It is on by default but disabled under C<use v5.16> (see
214L</IMPLICIT LOADING>, below).
215
216This feature is available under this name starting with Perl 5.16.  In
217previous versions, it was simply on all the time, and this pragma knew
218nothing about it.
219
220=head2 The 'fc' feature
221
222C<use feature 'fc'> tells the compiler to enable the C<fc> function,
223which implements Unicode casefolding.
224
225See L<perlfunc/fc> for details.
226
227This feature is available from Perl 5.16 onwards.
228
229=head2 The 'lexical_subs' feature
230
231In Perl versions prior to 5.26, this feature enabled
232declaration of subroutines via C<my sub foo>, C<state sub foo>
233and C<our sub foo> syntax.  See L<perlsub/Lexical Subroutines> for details.
234
235This feature is available from Perl 5.18 onwards.  From Perl 5.18 to 5.24,
236it was classed as experimental, and Perl emitted a warning for its
237usage, except when explicitly disabled:
238
239  no warnings "experimental::lexical_subs";
240
241As of Perl 5.26, use of this feature no longer triggers a warning, though
242the C<experimental::lexical_subs> warning category still exists (for
243compatibility with code that disables it).  In addition, this syntax is
244not only no longer experimental, but it is enabled for all Perl code,
245regardless of what feature declarations are in scope.
246
247=head2 The 'postderef' and 'postderef_qq' features
248
249The 'postderef_qq' feature extends the applicability of L<postfix
250dereference syntax|perlref/Postfix Dereference Syntax> so that postfix array
251and scalar dereference are available in double-quotish interpolations. For
252example, it makes the following two statements equivalent:
253
254  my $s = "[@{ $h->{a} }]";
255  my $s = "[$h->{a}->@*]";
256
257This feature is available from Perl 5.20 onwards. In Perl 5.20 and 5.22, it
258was classed as experimental, and Perl emitted a warning for its
259usage, except when explicitly disabled:
260
261  no warnings "experimental::postderef";
262
263As of Perl 5.24, use of this feature no longer triggers a warning, though
264the C<experimental::postderef> warning category still exists (for
265compatibility with code that disables it).
266
267The 'postderef' feature was used in Perl 5.20 and Perl 5.22 to enable
268postfix dereference syntax outside double-quotish interpolations. In those
269versions, using it triggered the C<experimental::postderef> warning in the
270same way as the 'postderef_qq' feature did. As of Perl 5.24, this syntax is
271not only no longer experimental, but it is enabled for all Perl code,
272regardless of what feature declarations are in scope.
273
274=head2 The 'signatures' feature
275
276B<WARNING>: This feature is still experimental and the implementation may
277change in future versions of Perl.  For this reason, Perl will
278warn when you use the feature, unless you have explicitly disabled the
279warning:
280
281    no warnings "experimental::signatures";
282
283This enables unpacking of subroutine arguments into lexical variables
284by syntax such as
285
286    sub foo ($left, $right) {
287	return $left + $right;
288    }
289
290See L<perlsub/Signatures> for details.
291
292This feature is available from Perl 5.20 onwards.
293
294=head2 The 'refaliasing' feature
295
296B<WARNING>: This feature is still experimental and the implementation may
297change in future versions of Perl.  For this reason, Perl will
298warn when you use the feature, unless you have explicitly disabled the
299warning:
300
301    no warnings "experimental::refaliasing";
302
303This enables aliasing via assignment to references:
304
305    \$a = \$b; # $a and $b now point to the same scalar
306    \@a = \@b; #                     to the same array
307    \%a = \%b;
308    \&a = \&b;
309    foreach \%hash (@array_of_hash_refs) {
310        ...
311    }
312
313See L<perlref/Assigning to References> for details.
314
315This feature is available from Perl 5.22 onwards.
316
317=head2 The 'bitwise' feature
318
319This makes the four standard bitwise operators (C<& | ^ ~>) treat their
320operands consistently as numbers, and introduces four new dotted operators
321(C<&. |. ^. ~.>) that treat their operands consistently as strings.  The
322same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).
323
324See L<perlop/Bitwise String Operators> for details.
325
326This feature is available from Perl 5.22 onwards.  Starting in Perl 5.28,
327C<use v5.28> will enable the feature.  Before 5.28, it was still
328experimental and would emit a warning in the "experimental::bitwise"
329category.
330
331=head2 The 'declared_refs' feature
332
333B<WARNING>: This feature is still experimental and the implementation may
334change in future versions of Perl.  For this reason, Perl will
335warn when you use the feature, unless you have explicitly disabled the
336warning:
337
338    no warnings "experimental::declared_refs";
339
340This allows a reference to a variable to be declared with C<my>, C<state>,
341our C<our>, or localized with C<local>.  It is intended mainly for use in
342conjunction with the "refaliasing" feature.  See L<perlref/Declaring a
343Reference to a Variable> for examples.
344
345This feature is available from Perl 5.26 onwards.
346
347=head1 FEATURE BUNDLES
348
349It's possible to load multiple features together, using
350a I<feature bundle>.  The name of a feature bundle is prefixed with
351a colon, to distinguish it from an actual feature.
352
353  use feature ":5.10";
354
355The following feature bundles are available:
356
357  bundle    features included
358  --------- -----------------
359  :default  array_base
360
361  :5.10     say state switch array_base
362
363  :5.12     say state switch unicode_strings array_base
364
365  :5.14     say state switch unicode_strings array_base
366
367  :5.16     say state switch unicode_strings
368            unicode_eval evalbytes current_sub fc
369
370  :5.18     say state switch unicode_strings
371            unicode_eval evalbytes current_sub fc
372
373  :5.20     say state switch unicode_strings
374            unicode_eval evalbytes current_sub fc
375
376  :5.22     say state switch unicode_strings
377            unicode_eval evalbytes current_sub fc
378
379  :5.24     say state switch unicode_strings
380            unicode_eval evalbytes current_sub fc
381            postderef_qq
382
383  :5.26     say state switch unicode_strings
384            unicode_eval evalbytes current_sub fc
385            postderef_qq
386
387  :5.28     say state switch unicode_strings
388            unicode_eval evalbytes current_sub fc
389            postderef_qq bitwise
390
391The C<:default> bundle represents the feature set that is enabled before
392any C<use feature> or C<no feature> declaration.
393
394Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
395no effect.  Feature bundles are guaranteed to be the same for all sub-versions.
396
397  use feature ":5.14.0";    # same as ":5.14"
398  use feature ":5.14.1";    # same as ":5.14"
399
400=head1 IMPLICIT LOADING
401
402Instead of loading feature bundles by name, it is easier to let Perl do
403implicit loading of a feature bundle for you.
404
405There are two ways to load the C<feature> pragma implicitly:
406
407=over 4
408
409=item *
410
411By using the C<-E> switch on the Perl command-line instead of C<-e>.
412That will enable the feature bundle for that version of Perl in the
413main compilation unit (that is, the one-liner that follows C<-E>).
414
415=item *
416
417By explicitly requiring a minimum Perl version number for your program, with
418the C<use VERSION> construct.  That is,
419
420    use v5.10.0;
421
422will do an implicit
423
424    no feature ':all';
425    use feature ':5.10';
426
427and so on.  Note how the trailing sub-version
428is automatically stripped from the
429version.
430
431But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
432
433    use 5.010;
434
435with the same effect.
436
437If the required version is older than Perl 5.10, the ":default" feature
438bundle is automatically loaded instead.
439
440Unlike C<use feature ":5.12">, saying C<use v5.12> (or any higher version)
441also does the equivalent of C<use strict>; see L<perlfunc/use> for details.
442
443=back
444
445=cut
446
447sub import {
448    shift;
449
450    if (!@_) {
451        croak("No features specified");
452    }
453
454    __common(1, @_);
455}
456
457sub unimport {
458    shift;
459
460    # A bare C<no feature> should reset to the default bundle
461    if (!@_) {
462	$^H &= ~($hint_uni8bit|$hint_mask);
463	return;
464    }
465
466    __common(0, @_);
467}
468
469
470sub __common {
471    my $import = shift;
472    my $bundle_number = $^H & $hint_mask;
473    my $features = $bundle_number != $hint_mask
474	&& $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]};
475    if ($features) {
476	# Features are enabled implicitly via bundle hints.
477	# Delete any keys that may be left over from last time.
478	delete @^H{ values(%feature) };
479	$^H |= $hint_mask;
480	for (@$features) {
481	    $^H{$feature{$_}} = 1;
482	    $^H |= $hint_uni8bit if $_ eq 'unicode_strings';
483	}
484    }
485    while (@_) {
486        my $name = shift;
487        if (substr($name, 0, 1) eq ":") {
488            my $v = substr($name, 1);
489            if (!exists $feature_bundle{$v}) {
490                $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
491                if (!exists $feature_bundle{$v}) {
492                    unknown_feature_bundle(substr($name, 1));
493                }
494            }
495            unshift @_, @{$feature_bundle{$v}};
496            next;
497        }
498        if (!exists $feature{$name}) {
499            if (exists $noops{$name}) {
500                next;
501            }
502            unknown_feature($name);
503        }
504	if ($import) {
505	    $^H{$feature{$name}} = 1;
506	    $^H |= $hint_uni8bit if $name eq 'unicode_strings';
507	} else {
508            delete $^H{$feature{$name}};
509            $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
510        }
511    }
512}
513
514sub unknown_feature {
515    my $feature = shift;
516    croak(sprintf('Feature "%s" is not supported by Perl %vd',
517            $feature, $^V));
518}
519
520sub unknown_feature_bundle {
521    my $feature = shift;
522    croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
523            $feature, $^V));
524}
525
526sub croak {
527    require Carp;
528    Carp::croak(@_);
529}
530
5311;
532
533# ex: set ro:
534