You are viewing the version of this documentation from Perl 5.24.3. View the latest version

CONTENTS

NAME

feature - Perl pragma to enable new features

SYNOPSIS

use feature qw(say switch);
given ($foo) {
    when (1)          { say "\$foo == 1" }
    when ([2,3])      { say "\$foo == 2 || \$foo == 3" }
    when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
    when ($_ > 100)   { say "\$foo > 100" }
    default           { say "None of the above" }
}

use feature ':5.10'; # loads all features available in perl 5.10

use v5.10;           # implicitly loads :5.10 feature bundle

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 use feature 'foo', and will be parsed only when the appropriate feature pragma is in scope. (Nevertheless, the CORE:: prefix provides access to all Perl keywords, regardless of this pragma.)

Lexical effect

Like other pragmas (use strict, for example), features have a lexical effect. 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";

no feature

Features can also be turned off by using 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.";

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

AVAILABLE FEATURES

The 'say' feature

use feature 'say' tells the compiler to enable the Perl 6 style say function.

See "say" in perlfunc for details.

This feature is available starting with Perl 5.10.

The 'state' feature

use feature 'state' tells the compiler to enable state variables.

See "Persistent Private Variables" in perlsub for details.

This feature is available starting with Perl 5.10.

The 'switch' feature

WARNING: Because the smartmatch operator is experimental, Perl will warn when you use this feature, unless you have explicitly disabled the warning:

no warnings "experimental::smartmatch";

use feature 'switch' tells the compiler to enable the Perl 6 given/when construct.

See