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 'say' feature
C<use feature 'say'> tells the compiler to enable the Raku-inspired
C<say> function.
See L<perlfunc/say> for details.
This feature is available starting with Perl 5.10.
=head2 The 'state' feature
C<use feature 'state'> tells the compiler to enable C<state>
variables.
See L<perlsub/"Persistent Private Variables"> for details.
This feature is available starting with Perl 5.10.
=head2 The 'smartmatch' feature
C<use feature 'smartmatch'> tells the compiler to enable the
smartmatch operator C<~~>. It is enabled by default, but can be
turned off to disallow the C<~~> operator.
This feature is disabled by default in the 5.42 feature bundle
onwards:
$x ~~ $y; # fine
use v5.42;
$x ~~ $y; # error
This has no effect on the implicit smartmatches done by C<when>.
See L<perlop/"Smartmatch Operator"> for details.
=head2 The 'switch' feature
C<use feature 'switch'> tells the compiler to enable the Raku
given/when construct.
See L<perlsyn/"Switch Statements"> for details.
This feature is available starting with Perl 5.10. It is enabled by
feature bundles 5.10 through 5.34, and disabled from the 5.36 feature
bundle onwards.
=head2 The 'unicode_strings' feature
C<use feature 'unicode_strings'> tells the compiler to use Unicode rules
in all string operations executed within its scope (unless they are also
within the scope of either C<use locale> or C<use bytes>). The same applies
to all regular expressions compiled within the scope, even if executed outside
it. It does not change the internal representation of strings, but only how
they are interpreted.
lib/feature.pm view on Meta::CPAN
:5.28 apostrophe_as_package_separator
bareword_filehandles bitwise current_sub
evalbytes fc indirect multidimensional
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
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.166 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )