feature-1.40 > feature

名前

feature - Perl pragma to enable new features

feature - 新しい機能を有効にするプラグマ

概要

    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

説明

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.)

既に存在しているプログラムを壊すことなく、Perl に新しい文法を追加することは、 普通は不可能です。 このプラグマは、リスクを最小化する方法を提供します。 新しい文法構造や、古い構造の新しい意味は、use feature 'foo' で有効化され、 適切な feature プラグマがスコープ内にある場合にのみパースされます。 (それでも、このプラグマに関わらず、CORE:: 接頭辞は全ての Perl キーワードへのアクセスを提供します。)

レキシカルな効果

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 strict のような) その他のプラグマと同様、feature は レキシカルな効果を持ちます。 use feature qw(foo) は、この地点からブロックの終わりまでの間だけ、 "foo" 機能を利用可能にします。

    {
        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.

機能は no feature "foo" を使うことで無効にすることも出来ます。 これもまたレキシカルな効果を持ちます。

    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'.

no feature と、機能を指定せずに使うと、デフォルトグループにリセットします。 全ての 機能を無効にする(普通でない要求!)には、no feature ':all' を 使ってください。

利用可能な機能

'say' 機能

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

use feature 'say' は、コンパイラに Perl 6 形式の say 関数を 有効にするように伝えます。

See "say" in perlfunc for details.

詳しくは "say" in perlfunc を参照してください。

This feature is available starting with Perl 5.10.

この機能は Perl 5.10 から利用可能です。

'state' 機能

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

use feature 'state' は、コンパイラに state 変数を有効にするように 伝えます。

詳しくは "Persistent Private Variables" in perlsub を参照してください。

This feature is available starting with Perl 5.10.

この機能は Perl 5.10 から利用可能です。

'switch' 機能

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

WARNING: スマートマッチング演算子 は 実験的なので、この機能を使うと、明示的に無効にしない限り警告が発生します:

    no warnings "experimental::smartmatch";

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

use feature 'switch' は、コンパイラに Perl 6 given/when 構文を 有効にするように伝えます。

詳しくは "Switch Statements" in perlsyn を参照してください。

This feature is available starting with Perl 5.10.

この機能は Perl 5.10 から利用可能です。

'unicode_strings' 機能

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 use locale or 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.

use feature 'unicode_strings' は、(use localeuse bytes の スコープないでない限り) そのスコープ内で実行される全ての文字列操作に Unicode の規則を使うようにコンパイラに伝えます。 これは文字列の内部表現は変更しません; それをどう解釈するかだけです。

no feature 'unicode_strings' tells the compiler to use the traditional Perl rules wherein the native character set rules is used unless it is clear to Perl that Unicode is desired. This can lead to some surprises when the behavior suddenly changes. (See "The "Unicode Bug"" in perlunicode for details.) For this reason, if you are potentially using Unicode in your program, the use feature 'unicode_strings' subpragma is strongly recommended.

no feature 'unicode_strings' は、Unicode が求められているのが Perl にとって明らかでない限り、ネイティブな文字集合規則が使われるところで 伝統的な Perl の規則を使うようにコンパイラに伝えます。 これは、振る舞いが突然変更されたときに驚きを引き起こすかもしれません。 (詳しくは "The "Unicode Bug"" in perlunicode を参照してください。) この理由により、もしプログラムで Unicode を扱う可能性があるなら、 use feature 'unicode_strings' 副プラグマを 強く 勧めます。

This feature is available starting with Perl 5.12; was almost fully implemented in Perl 5.14; and extended in Perl 5.16 to cover quotemeta.

この機能は Perl 5.12 から利用可能になりました; Perl 5.14 でほぼ完全に 実装されました; Perl 5.16 で quotemeta に対応するように拡張されました。

'unicode_eval' と 'evalbytes' 機能

Under the unicode_eval feature, Perl's eval function, when passed a string, will evaluate it as a string of characters, ignoring any use utf8 declarations. use utf8 exists to declare the encoding of the script, which only makes sense for a stream of bytes, not a string of characters. Source filters are forbidden, as they also really only make sense on strings of bytes. Any attempt to activate a source filter will result in an error.

unicode_eval 機能の基では、Perl の eval 関数に文字列が渡されると、 文字の文字列として評価し、use utf8 宣言を無視します。 use utf8 はスクリプトのエンコーディングを宣言するために存在し、 バイトの並びにのみ意味があり、文字の文字列では意味がありません。 ソースフィルタは禁止されます; これらもバイトの文字列に対してのみ 意味があるからです。 ソースフィルタを有効にしようとするあらゆる試みはエラーとなります。

The evalbytes feature enables the evalbytes keyword, which evaluates the argument passed to it as a string of bytes. It dies if the string contains any characters outside the 8-bit range. Source filters work within evalbytes: they apply to the contents of the string being evaluated.

evalbytes 機能は evalbytes キーワードを有効にします; これは引数として渡されたものをバイトの文字列として評価します。 文字列に 8 ビットの範囲の外側の文字が含まれていると die します。 ソースフィルタは evalbytes の中では動作します: これらは 評価される文字列の中身に対して適用されます。

Together, these two features are intended to replace the historical eval function, which has (at least) two bugs in it, that cannot easily be fixed without breaking existing programs:

これら二つの機能は共に、歴史的な eval 関数を置き換えることを 目的としています; これには(少なくとも)二つのバグがあり、既存のプログラムを 壊すことなく簡単に修正することができません:

  • eval behaves differently depending on the internal encoding of the string, sometimes treating its argument as a string of bytes, and sometimes as a string of characters.

    eval は文字列音内部エンコーディングに依存して異なる振る舞いを行い、 時には引数をバイトの文字列として扱い、時には文字の文字列として扱います。

  • Source filters activated within eval leak out into whichever file scope is currently being compiled. To give an example with the CPAN module Semi::Semicolons:

    eval の中で有効にされたソースフィルタは、どの file スコープが コンパイルされているかについてリークします。 CPAN モジュールである Semi::Semicolons を例にします:

        BEGIN { eval "use Semi::Semicolons;  # not filtered here " }
        # filtered here!

    evalbytes fixes that to work the way one would expect:

    evalbytes は、想定通りに動作するように修正します:

        use feature "evalbytes";
        BEGIN { evalbytes "use Semi::Semicolons;  # filtered " }
        # not filtered

These two features are available starting with Perl 5.16.

これら二つの機能は Perl 5.16 から利用可能です。

'current_sub' 機能

This provides the __SUB__ token that returns a reference to the current subroutine or undef outside of a subroutine.

これは __SUB__ トークンを提供します; これは現在のサブルーチンへの リファレンスか、サブルーチンの外側では undef を返します。

This feature is available starting with Perl 5.16.

この機能は Perl 5.16 から利用可能です。

'array_base' 機能

This feature supports the legacy $[ variable. See "$[" in perlvar and arybase. It is on by default but disabled under use v5.16 (see "IMPLICIT LOADING", below).

この機能はレガシーな $[ 変数に対応します。 "$[" in perlvararybase を参照してください。 これはデフォルトではオンですが use v5.16 (後述の "IMPLICIT LOADING" 参照) の基では無効になります。

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.

この機能は Perl 5.16 からこの名前で利用可能です。 以前のバージョンでは、単に常時適用されていて、このプラグマはこれについて 何も知りませんでした。

'fc' 機能

use feature 'fc' tells the compiler to enable the fc function, which implements Unicode casefolding.

use feature 'fc' は、Unicode 畳み込みを実装した fc 関数を 有効にするようにコンパイラに伝えます。

See "fc" in perlfunc for details.

詳しくは "fc" in perlfunc を参照してください。

This feature is available from Perl 5.16 onwards.

この機能は Perl 5.16 から利用可能です。

'lexical_subs' 機能

WARNING: This feature is still experimental and the implementation may change in future versions of Perl. For this reason, Perl will warn when you use the feature, unless you have explicitly disabled the warning:

警告: この機能はまだ実験的で、実装は将来のバージョンの Perl で 変わるかもしれません。 このため、この機能を使うと、明示的に無効にしない限り警告が発生します:

    no warnings "experimental::lexical_subs";

This enables declaration of subroutines via my sub foo, state sub foo and our sub foo syntax. See "Lexical Subroutines" in perlsub for details.

これは、my sub foo, state sub foo, our sub foo 文法による サブルーチンの定義を有効にします。 詳しくは "Lexical Subroutines" in perlsub を参照してください。

This feature is available from Perl 5.18 onwards.

この機能は Perl 5.18 から利用可能です。

The 'postderef' and 'postderef_qq' features

WARNING: This feature is still experimental and the implementation may change in future versions of Perl. For this reason, Perl will warn when you use the feature, unless you have explicitly disabled the warning:

警告: この機能はまだ実験的で、実装は将来のバージョンの Perl で 変わるかもしれません。 このため、この機能を使うと、明示的に無効にしない限り警告が発生します:

  no warnings "experimental::postderef";

The 'postderef' feature allows the use of postfix dereference syntax. For example, it will make the following two statements equivalent:

'postderef' 機能は、後置デリファレンス文法 を使えるようにします。 例えば、以下の二つの文は等価になります:

  my @x = @{ $h->{a} };
  my @x = $h->{a}->@*;

The 'postderef_qq' feature extends this, for array and scalar dereference, to working inside of double-quotish interpolations.

'postderef_qq' 機能はこれを、ダブルクォート風変数展開の内側で動作するように、 配列とスカラのデリファレンスに拡張します。

This feature is available from Perl 5.20 onwards.

この機能は Perl 5.20 から利用可能です。

'signatures' 機能

WARNING: This feature is still experimental and the implementation may change in future versions of Perl. For this reason, Perl will warn when you use the feature, unless you have explicitly disabled the warning:

警告: この機能はまだ実験的で、実装は将来のバージョンの Perl で 変わるかもしれません。 このため、この機能を使うと、明示的に無効にしない限り警告が発生します:

    no warnings "experimental::signatures";

This enables unpacking of subroutine arguments into lexical variables by syntax such as

これは、次のような文法によってサブルーチンの引数をレキシカル変数に 展開できるようにします:

    sub foo ($left, $right) {
        return $left + $right;
    }

See "Signatures" in perlsub for details.

詳しくは "Signatures" in perlsub を参照してください。

This feature is available from Perl 5.20 onwards.

この機能は Perl 5.20 から利用可能です。

The 'refaliasing' feature

WARNING: This feature is still experimental and the implementation may change in future versions of Perl. For this reason, Perl will warn when you use the feature, unless you have explicitly disabled the warning:

警告: この機能はまだ実験的で、実装は将来のバージョンの Perl で 変わるかもしれません。 このため、この機能を使うと、明示的に無効にしない限り警告が発生します:

    no warnings "experimental::refaliasing";

This enables aliasing via assignment to references:

これはリファレンスへの代入による別名化を有効にします:

    \$a = \$b; # $a and $b now point to the same scalar
    \@a = \@b; #                     to the same array
    \%a = \%b;
    \&a = \&b;
    foreach \%hash (@array_of_hash_refs) {
        ...
    }

詳しくは "Assigning to References" in perlref を参照してください。

This feature is available from Perl 5.22 onwards.

この機能は Perl 5.22 から利用可能です。

The 'bitwise' feature

WARNING: This feature is still experimental and the implementation may change in future versions of Perl. For this reason, Perl will warn when you use the feature, unless you have explicitly disabled the warning:

警告: この機能はまだ実験的で、実装は将来のバージョンの Perl で 変わるかもしれません。 このため、この機能を使うと、明示的に無効にしない限り警告が発生します:

    no warnings "experimental::bitwise";

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

これは四つの標準ビット単位演算子 (& | ^ ~) がそのオペランドを 数値として一貫して扱うようになり、 オペランドを一貫して文字列として扱う新しいドット付き演算子 (&. |. ^. ~.) を導入します。 同じものは代入の亜種 (&= |= ^= &.= |.= ^.=) にも適用されます。

詳しくは "Bitwise String Operators" in perlop を参照してください。

This feature is available from Perl 5.22 onwards.

この機能は Perl 5.22 から利用可能です。

機能の束

It's possible to load multiple features together, using a feature bundle. The name of a feature bundle is prefixed with a colon, to distinguish it from an actual feature.

複数の機能のまとめて読み込むためには、機能の束 (feature bundle) が 使えます。 機能の束の名前には、実際の機能と区別するためにコロンが前置されます。

  use feature ":5.10";

The following feature bundles are available:

以下の機能の束が利用可能です:

  bundle    features included
  --------- -----------------
  :default  array_base

  :5.10     say state switch array_base

  :5.12     say state switch unicode_strings array_base

  :5.14     say state switch unicode_strings array_base

  :5.16     say state switch unicode_strings
            unicode_eval evalbytes current_sub fc

  :5.18     say state switch unicode_strings
            unicode_eval evalbytes current_sub fc

  :5.20     say state switch unicode_strings
            unicode_eval evalbytes current_sub fc

  :5.22     say state switch unicode_strings
            unicode_eval evalbytes current_sub fc

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

:default 束は、use featureno feature 宣言が有効になる前の 機能集合を表現しています。

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

機能の束での 5.14.00 のような副バージョンを指定しても効果は ありません。 機能の束は全ての副バージョンに関して同じ事が保証されています。

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

暗黙の読み込み

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

機能の束を名前で読み込むより、Perl に機能の束を暗黙に読み込ませるように した方が簡単です。

There are two ways to load the feature pragma implicitly:

feature プラグマを暗黙に読み込むには二つの方法があります:

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

    Perl のコマンドラインで -e オプションの代わりに -E オプションを 使用した場合。 これにより、main コンパイル単位(つまり、-E に引き続く 1 行野郎)で そのバージョンの Perl の機能の束が有効になります。

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

    use VERSION 構文を使ってプログラムが必要とする最低限の Perl バージョン 番号を明示的に指定した場合。 つまり、以下のようにすると:

        use v5.10.0;

    will do an implicit

    暗黙のうちに以下のように:

        no feature ':all';
        use feature ':5.10';

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

    なるということです。 末尾の副バージョンは自動的にバージョンから取り除かれるようになったことに 注意してください。

    But to avoid portability warnings (see "use" in perlfunc), you may prefer:

    しかし移植性の警告("use" in perlfunc を参照してください)を避けるために、 以下のようにするのを好むかもしれません:

        use 5.010;

    with the same effect.

    これでも同じ効果が得られます。

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

    要求したバージョンが Perl 5.10 より前の場合、代わりに機能の束 ":default" が 自動的に読み込まれます。