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