perl

 view release on metacpan or  search on metacpan

lib/feature.pm  view on Meta::CPAN


=encoding utf8

=head1 NAME

feature - Perl pragma to enable new features

=head1 SYNOPSIS

    use feature qw(fc say);

    # Without the "use feature" above, this code would not be able to find
    # the built-ins "say" or "fc":
    say "The case-folded version of $x is: " . fc $x;


    # set features to match the :5.36 bundle, which may turn off or on
    # multiple features (see "FEATURE BUNDLES" below)
    use feature ':5.36';


    # implicitly loads :5.36 feature bundle
    use v5.36;

=head1 DESCRIPTION

It is usually impossible to add new syntax to Perl without breaking
some existing programs.  This pragma provides a way to minimize that
risk. New syntactic constructs, or new semantic meanings to older
constructs, can be enabled by C<use feature 'foo'>, and will be parsed
only when the appropriate feature pragma is in scope.  (Nevertheless, the
C<CORE::> prefix provides access to all Perl keywords, regardless of this
pragma.)

=head2 Lexical effect

Like other pragmas (C<use strict>, for example), features have a lexical
effect.  C<use feature qw(foo)> will only make the feature "foo" available
from that point to the end of the enclosing block.

    {
        use feature 'say';
        say "say is available here";
    }
    print "But not here.\n";

=head2 C<no feature>

Features can also be turned off by using C<no feature "foo">.  This too
has lexical effect.

    use feature 'say';
    say "say is available here";
    {
        no feature 'say';
        print "But not here.\n";
    }
    say "Yet it is here.";

C<no feature> with no features specified will reset to the default group.  To
disable I<all> features (an unusual request!) use C<no feature ':all'>.

=head1 AVAILABLE FEATURES

Read L</"FEATURE BUNDLES"> for the feature cheat sheet summary.

=head2 The 'apostrophe_as_package_separator' feature

This feature enables use C<'> (apostrophe) as an alternative to using
C<::> as a separate in package and other global names.

This is enabled by default, but disabled from the 5.42 feature bundle
onwards.  In previous versions it was enabled all the time.

This only disables C<'> in symbols in your source code, the internal
conversion from C<'> to C<::>, including for symbolic references, is
always enabled.

=head2 The 'array_base' feature

This feature supported the legacy C<$[> variable.  See L<perlvar/$[>.
It was on by default but disabled under C<use v5.16> (see
L</IMPLICIT LOADING>, below) and unavailable since perl 5.30.

This feature is available under this name starting with Perl 5.16.  In
previous versions, it was simply on all the time, and this pragma knew
nothing about it.

=head2 The 'bareword_filehandles' feature

This feature enables bareword filehandles for builtin functions
operations, a generally discouraged practice.  It is enabled by
default, but can be turned off to disable bareword filehandles, except
for the exceptions listed below.

The perl built-in filehandles C<STDIN>, C<STDOUT>, C<STDERR>, C<DATA>,
C<ARGV>, C<ARGVOUT> and the special C<_> are always enabled.

This feature is available under this name from Perl 5.34 onwards.  In
previous versions it was simply on all the time.  It is disabled from
the 5.38 feature bundle onwards.

You can use the L<bareword::filehandles> module on CPAN to disable
bareword filehandles for older versions of perl.

=head2 The 'bitwise' feature

This makes the four standard bitwise operators (C<& | ^ ~>) treat their
operands consistently as numbers, and introduces four new dotted operators
(C<&. |. ^. ~.>) that treat their operands consistently as strings.  The
same applies to the assignment variants (C<&= |= ^= &.= |.= ^.=>).

See L<perlop/Bitwise String Operators> for details.

This feature is available from Perl 5.22 onwards.  Starting in Perl 5.28,
C<use v5.28> will enable the feature.  Before 5.28, it was still
experimental and would emit a warning in the "experimental::bitwise"
category.

=head2 The 'class' feature

lib/feature.pm  view on Meta::CPAN

            postderef_qq say smartmatch state switch
            unicode_eval unicode_strings

  :5.30     apostrophe_as_package_separator
            bareword_filehandles bitwise current_sub
            evalbytes fc indirect multidimensional
            postderef_qq say smartmatch state switch
            unicode_eval unicode_strings

  :5.32     apostrophe_as_package_separator
            bareword_filehandles bitwise current_sub
            evalbytes fc indirect multidimensional
            postderef_qq say smartmatch state switch
            unicode_eval unicode_strings

  :5.34     apostrophe_as_package_separator
            bareword_filehandles bitwise current_sub
            evalbytes fc indirect multidimensional
            postderef_qq say smartmatch state switch
            unicode_eval unicode_strings

  :5.36     apostrophe_as_package_separator
            bareword_filehandles bitwise current_sub
            evalbytes fc isa postderef_qq say signatures
            smartmatch state unicode_eval
            unicode_strings

  :5.38     apostrophe_as_package_separator bitwise
            current_sub evalbytes fc isa module_true
            postderef_qq say signatures smartmatch state
            unicode_eval unicode_strings

  :5.40     apostrophe_as_package_separator bitwise
            current_sub evalbytes fc isa module_true
            postderef_qq say signatures smartmatch state
            try unicode_eval unicode_strings

  :5.42     bitwise current_sub evalbytes fc isa
            module_true postderef_qq say signatures
            state try unicode_eval unicode_strings

  :5.44     bitwise current_sub evalbytes fc isa
            module_true postderef_qq say signatures
            state try unicode_eval unicode_strings

The C<:default> bundle represents the feature set that is enabled before
any C<use feature> or C<no feature> declaration.

Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
no effect.  Feature bundles are guaranteed to be the same for all sub-versions.

  use feature ":5.14.0";    # same as ":5.14"
  use feature ":5.14.1";    # same as ":5.14"

You can also do:

  use feature ":all";

or

  no feature ":all";

but the first may enable features in a later version of Perl that
change the meaning of your code, and the second may disable mechanisms
that are part of Perl's current behavior that have been turned into
features, just as C<indirect> and C<bareword_filehandles> were.

=head1 IMPLICIT LOADING

Instead of loading feature bundles by name, it is easier to let Perl do
implicit loading of a feature bundle for you.

There are two ways to load the C<feature> pragma implicitly:

=over 4

=item *

By using the C<-E> switch on the Perl command-line instead of C<-e>.
That will enable the feature bundle for that version of Perl in the
main compilation unit (that is, the one-liner that follows C<-E>).

=item *

By explicitly requiring a minimum Perl version number for your program, with
the C<use VERSION> construct.  That is,

    use v5.36.0;

will do an implicit

    no feature ':all';
    use feature ':5.36';

and so on.  Note how the trailing sub-version
is automatically stripped from the
version.

But to avoid portability warnings (see L<perlfunc/use>), you may prefer:

    use 5.036;

with the same effect.

If the required version is older than Perl 5.10, the ":default" feature
bundle is automatically loaded instead.

Unlike C<use feature ":5.12">, saying C<use v5.12> (or any higher version)
also does the equivalent of C<use strict>; see L<perlfunc/use> for details.

=back

=head1 CHECKING FEATURES

C<feature> provides some simple APIs to check which features are enabled.

These functions cannot be imported and must be called by their fully
qualified names.  If you don't otherwise need to set a feature you will
need to ensure C<feature> is loaded with:

  use feature ();

=over

=item feature_enabled($feature)

=item feature_enabled($feature, $depth)

  package MyStandardEnforcer;
  use feature ();
  use Carp "croak";
  sub import {
    croak "disable indirect!" if feature::feature_enabled("indirect");
  }

Test whether a named feature is enabled at a given level in the call
stack, returning a true value if it is.  C<$depth> defaults to 1,
which checks the scope that called the scope calling
feature::feature_enabled().

croaks for an unknown feature name.

=item features_enabled()

=item features_enabled($depth)

  package ReportEnabledFeatures;
  use feature "say";
  sub import {
    say STDERR join " ", feature::features_enabled();
  }



( run in 1.000 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )