builtin-0.006 > builtin

名前

builtin - Perl pragma to import built-in utility functions

builtin - 組み込みユーティリティ関数をインポートする Perl プラグマ

概要

    use builtin qw(
        true false is_bool
        weaken unweaken is_weak
        blessed refaddr reftype
        created_as_string created_as_number
        ceil floor
        trim
    );

説明

Perl provides several utility functions in the builtin package. These are plain functions, and look and behave just like regular user-defined functions do. They do not provide new syntax or require special parsing. These functions are always present in the interpreter and can be called at any time by their fully-qualified names. By default they are not available as short names, but can be requested for convenience.

Individual named functions can be imported by listing them as import parameters on the use statement for this pragma.

The overall builtin mechanism, as well as every individual function it provides, are currently experimental.

Warning: At present, the entire builtin namespace is experimental. Calling functions in it will trigger warnings of the experimental::builtin category.

Perl は builtin パッケージでいくつかのユーティリティ関数を提供しています。それらは、 ただの関数で、ユーザが定義した通常の関数のように見え、振る舞います。新しい構文を提供したり、 特別なパースを必要としたりしません。これらの関数は常にインタプリタに存在し、完全修飾名で いつでも呼び出すことができます。デフォルトでは、短い名前で利用できませんが、利便性のために要求できます。

個々の名前付き関数は、このプラグマの use 文のインポートパラメータを指定すればインポートできます。

全体的な builtin メカニズム、およびそれが提供する個々の関数は現在 実験的 です。

警告: 現在、builtin 名前空間全体が実験的です。 これらの関数を呼び出せば、experimental::builtinカテゴリの警告が発生します。

Lexical Import

This pragma module creates lexical aliases in the currently-compiling scope to these builtin functions. This is similar to the lexical effect of other pragmas such as strict and feature.

このプラグマモジュールは、現在コンパイル中のスコープにこれらの組み込み関数への レキシカル エイリアスを作成します。 これは、strictfeature のような他のプラグマのようにレキシカルに影響を与えます。

    sub classify
    {
        my $val = shift;

        use builtin 'is_bool';
        return is_bool($val) ? "boolean" : "not a boolean";
    }

    # the is_bool() function is no longer visible here
    # but may still be called by builtin::is_bool()

Because these functions are imported lexically, rather than by package symbols, the user does not need to take any special measures to ensure they don't accidentally appear as object methods from a class.

これらの関数は、パッケージシンボルではなくレキシカルにインポートされるため、 クラスのオブジェクトメソッドとして誤って現れないようにするための特別な措置は必要ありません。

    package An::Object::Class {
        use builtin 'true', 'false';
        ...
    }

    # does not appear as a method
    An::Object::Class->true;

    # Can't locate object method "true" via package "An::Object::Class"
    #   at ...

関数

true

    $val = true;

Returns the boolean truth value. While any scalar value can be tested for truth and most defined, non-empty and non-zero values are considered "true" by perl, this one is special in that "is_bool" considers it to be a distinguished boolean value.

This gives an equivalent value to expressions like !!1 or !0.

真偽値の真を返します。Perlでは、どんなスカラ値も真かどうかテストできますし、定義済みで、空でなく、非ゼロの値は、"真"となりますが、 このtrueは、"is_bool"において、通常の真偽値と区別され、特別な真偽値とみなされます。

これは、!!1!0 のような式と等価です。

false

    $val = false;

Returns the boolean fiction value. While any non-true scalar value is considered "false" by perl, this one is special in that "is_bool" considers it to be a distinguished boolean value.

This gives an equivalent value to expressions like !!0 or !1.

真偽値の偽を返します。Perlでは、真でないスカラ値は、"偽"となりますが、 このfalseは、"is_bool"において、通常の真偽値と区別され、特別な真偽値とみなされます。

これは、!!0!1 のような式と等価です。

is_bool

    $bool = is_bool($val);

Returns true when given a distinguished boolean value, or false if not. A distinguished boolean value is the result of any boolean-returning builtin function (such as true or is_bool itself), boolean-returning operator (such as the eq or == comparison tests or the ! negation operator), or any variable containing one of these results.

This function used to be named isbool. A compatibility alias is provided currently but will be removed in a later version.

特別な真偽値が与えられたときに真を返し、そうでなければ偽を返します。 特別な真偽値は、真偽値を返す組み込み関数(trueis_bool 自身など)や 真偽値を返す演算子(eq== の比較テストや ! の否定演算子など)の結果、 あるいは、これらの結果を含む変数の値です。

この関数は、isbool という名前でした。現在は互換性のためのエイリアスが提供されていますが、 後のバージョンで削除されます。

weaken

    weaken($ref);

Weakens a reference. A weakened reference does not contribute to the reference count of its referent. If only weakened references to a referent remain, it will be disposed of, and all remaining weak references to it will have their value set to undef.

リファレンスを弱くします。弱いリファレンスは、そのリファレンスのリファレンスカウントには寄与しません。 リファレンスへの弱いリファレンスだけが残っている場合、リファレンスは破棄され、残った弱いリファレンスは全てundefに設定されます。

unweaken

    unweaken($ref);

Strengthens a reference, undoing the effects of a previous call to "weaken".

リファレンスを強くします。"weaken" による呼び出しの効果を元に戻します。

is_weak

    $bool = is_weak($ref);

Returns true when given a weakened reference, or false if not a reference or not weak.

This function used to be named isweak. A compatibility alias is provided currently but will be removed in a later version.

弱いリファレンスが与えられた時に真を返し、そうでなければ偽を返します。

この関数は、isweak という名前でした。現在は互換性のためのエイリアスが提供されていますが、 後のバージョンで削除されます。

blessed

    $str = blessed($ref);

Returns the package name for an object reference, or undef for a non-reference or reference that is not an object.

オブジェクトリファレンスであればパッケージ名を返し、 リファレンスでない場合やオブジェクトリファレンスでない場合はundefを返します。

refaddr

    $num = refaddr($ref);

Returns the memory address for a reference, or undef for a non-reference. This value is not likely to be very useful for pure Perl code, but is handy as a means to test for referential identity or uniqueness.

リファレンスのメモリアドレスを返し、リファレンスでない場合はundefを返します。 この値は、純粋なPerlコードではあまり有用ではありませんが、参照の同一性や一意性をテストする手段として便利です。

reftype

    $str = reftype($ref);

Returns the basic container type of the referent of a reference, or undef for a non-reference. This is returned as a string in all-capitals, such as ARRAY for array references, or HASH for hash references.

リファレンスの参照先のデータ型を返し、リファレンスでない場合はundefを返します。 これは、すべて大文字の文字列として返されます。たとえば、配列リファレンスの場合はARRAY、 ハッシュリファレンスの場合はHASHなどです。

created_as_string

    $bool = created_as_string($val);

Returns a boolean representing if the argument value was originally created as a string. It will return true for any scalar expression whose most recent assignment or modification was of a string-like nature - such as assignment from a string literal, or the result of a string operation such as concatenation or regexp. It will return false for references (including any object), numbers, booleans and undef.

It is unlikely that you will want to use this for regular data validation within Perl, as it will not return true for regular numbers that are still perfectly usable as strings, nor for any object reference - especially objects that overload the stringification operator in an attempt to behave more like strings. For example

与えられた値が、元々文字列として作成されたかどうかを真偽値で返します。 最も最近の代入や変更が文字列のような性質であった任意のスカラ式は真を返します。例えば、 文字列リテラルからの代入や、連結や正規表現のような文字列操作の結果などです。 リファレンス(オブジェクトを含む)、数値、真偽値、undefに対しては偽を返します。

Perl内での通常のデータ検証にこの関数を使用することはあまり推奨されません。なぜなら、 文字列として完全に利用できる通常の数値であっても真を返さず、 また、文字列のように振る舞うように文字列化演算子をオーバーロードするオブジェクトを含む、 あらゆるオブジェクトリファレンスに対しても真を返さないからです。例えば、次のようなケースです。

    my $val = URI->new( "https://metacpan.org/" );

    if( created_as_string $val ) { ... }    # this will not execute

created_as_number

    $bool = created_as_number($val);

Returns a boolean representing if the argument value was originally created as a number. It will return true for any scalar expression whose most recent assignment or modification was of a numerical nature - such as assignment from a number literal, or the result of a numerical operation such as addition. It will return false for references (including any object), strings, booleans and undef.

It is unlikely that you will want to use this for regular data validation within Perl, as it will not return true for regular strings of decimal digits that are still perfectly usable as numbers, nor for any object reference - especially objects that overload the numification operator in an attempt to behave more like numbers. For example

与えられた値が、元々数値として作成されたかどうかを真偽値で返します。 最も最近の代入や変更が数値のような性質であった任意のスカラ式は真を返します。例えば、 数値リテラルからの代入や、加算のような数値操作の結果などです。 リファレンス(オブジェクトを含む)、文字列、真偽値、undefに対しては偽を返します。

Perl内での通常のデータ検証にこの関数を使用することはあまり推奨されません。なぜなら、 数値として完全に利用できる10進数の文字列であっても真を返さず、 また、数値のように振る舞うように数値化演算子をオーバーロードするオブジェクトを含む、 あらゆるオブジェクトリファレンスに対しても真を返さないからです。例えば、次のようなケースです。

    my $val = Math::BigInt->new( 123 );

    if( created_as_number $val ) { ... }    # this will not execute

While most Perl code should operate on scalar values without needing to know their creation history, these two functions are intended to be used by data serialisation modules such as JSON encoders or similar situations, where language interoperability concerns require making a distinction between values that are fundamentally stringlike versus numberlike in nature.

ほとんどのPerlコードは、スカラ値の作成履歴を知ることなく操作するべきですが、 これらの二つの関数は、JSONエンコーダなどのデータシリアライゼーションモジュールなどにおいて利用されることを意図しています。 そういった状況では、言語間の相互運用性の観点で、文字列の性質と数値の性質を区別する必要性が生じるからです。

ceil

    $num = ceil($num);

Returns the smallest integer value greater than or equal to the given numerical argument.

与えられた数値引数以上の最小の整数値を返します。

floor

    $num = floor($num);

Returns the largest integer value less than or equal to the given numerical argument.

与えられた数値引数以下の最大の整数値を返します。

indexed

    @ivpairs = indexed(@items)

Returns an even-sized list of number/value pairs, where each pair is formed of a number giving an index in the original list followed by the value at that position in it. I.e. returns a list twice the size of the original, being equal to

元のリストのインデックスを示す数値と、その位置の値から成るペアの数値/値のペアの偶数サイズのリストを返します。 つまり、元のリストの2倍のサイズのリストを返し、次と等価です。

    (0, $items[0], 1, $items[1], 2, $items[2], ...)

Note that unlike the core values function, this function returns copies of its original arguments, not aliases to them. Any modifications of these copies are not reflected in modifications to the original.

コアのvalues関数とは異なり、この関数は元の引数のエリアスではなく、コピーを返すことに注意してください。 これらのコピーにあらゆる変更をしても、元の値には反映されません

    my @x = ...;
    $_++ for indexed @x;  # The @x array remains unaffected

This function is primarily intended to be useful combined with multi-variable foreach loop syntax; as

この関数は、複数変数のforeachループ構文と組み合わせて使用することを主に意図しています。

    foreach my ($index, $value) (indexed LIST) {
        ...
    }

In scalar context this function returns the size of the list that it would otherwise have returned, and provokes a warning in the scalar category.

スカラコンテキストでは、この関数が返すはずのリストのサイズを返し、scalarカテゴリの警告を発生させます。

trim

    $stripped = trim($string);

Returns the input string with whitespace stripped from the beginning and end. trim() will remove these characters:

入力文字列の先頭と末尾から空白を取り除いた文字列を返します。trim()は、次の文字を取り除きます。

" ", an ordinary space.

"\t", a tab.

"\n", a new line (line feed).

"\r", a carriage return.

and all other Unicode characters that are flagged as whitespace. A complete list is in "Whitespace" in perlrecharclass.

そして、空白としてフラグ付けされているすべてのUnicode文字を取り除きます。 完全な一覧は、"Whitespace" in perlrecharclassにあります。

    $var = "  Hello world   ";            # "Hello world"
    $var = "\t\t\tHello world";           # "Hello world"
    $var = "Hello world\n";               # "Hello world"
    $var = "\x{2028}Hello world\x{3000}"; # "Hello world"

trim is equivalent to:

trimは次と等価です。

    $str =~ s/\A\s+|\s+\z//urg;

For Perl versions where this feature is not available look at the String::Util module for a comparable implementation.

このfeatureが利用できないPerlバージョンでは、同等の実装を持つString::Utilモジュールを参照してください。

SEE ALSO

perlop, perlfunc, Scalar::Util