=encoding utf8 =head1 NAME =begin original builtin - Perl pragma to import built-in utility functions =end original builtin - 組み込みユーティリティ関数をインポートする Perl プラグマ =head1 SYNOPSIS use builtin qw( true false is_bool weaken unweaken is_weak blessed refaddr reftype created_as_string created_as_number ceil floor trim ); =head1 DESCRIPTION =begin original Perl provides several utility functions in the C 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 C statement for this pragma. The overall C mechanism, as well as every individual function it provides, are currently B. B: At present, the entire C namespace is experimental. Calling functions in it will trigger warnings of the C category. =end original Perl は C パッケージでいくつかのユーティリティ関数を提供しています。それらは、 ただの関数で、ユーザが定義した通常の関数のように見え、振る舞います。新しい構文を提供したり、 特別なパースを必要としたりしません。これらの関数は常にインタプリタに存在し、完全修飾名で いつでも呼び出すことができます。デフォルトでは、短い名前で利用できませんが、利便性のために要求できます。 個々の名前付き関数は、このプラグマの C 文のインポートパラメータを指定すればインポートできます。 全体的な C メカニズム、およびそれが提供する個々の関数は現在 B<実験的> です。 B<警告>: 現在、C 名前空間全体が実験的です。 これらの関数を呼び出せば、Cカテゴリの警告が発生します。 =head2 Lexical Import =begin original This pragma module creates I aliases in the currently-compiling scope to these builtin functions. This is similar to the lexical effect of other pragmas such as L and L. =end original このプラグマモジュールは、現在コンパイル中のスコープにこれらの組み込み関数への I<レキシカル> エイリアスを作成します。 これは、L や L のような他のプラグマのようにレキシカルに影響を与えます。 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() =begin original 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. =end original これらの関数は、パッケージシンボルではなくレキシカルにインポートされるため、 クラスのオブジェクトメソッドとして誤って現れないようにするための特別な措置は必要ありません。 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 ... =head1 FUNCTIONS =head2 true $val = true; =begin original 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 L considers it to be a distinguished boolean value. This gives an equivalent value to expressions like C or C. =end original 真偽値の真を返します。Perlでは、どんなスカラ値も真かどうかテストできますし、定義済みで、空でなく、非ゼロの値は、"真"となりますが、 このtrueは、Lにおいて、通常の真偽値と区別され、特別な真偽値とみなされます。 これは、C や C のような式と等価です。 =head2 false $val = false; =begin original Returns the boolean fiction value. While any non-true scalar value is considered "false" by perl, this one is special in that L considers it to be a distinguished boolean value. This gives an equivalent value to expressions like C or C. =end original 真偽値の偽を返します。Perlでは、真でないスカラ値は、"偽"となりますが、 このfalseは、Lにおいて、通常の真偽値と区別され、特別な真偽値とみなされます。 これは、C や C のような式と等価です。 =head2 is_bool $bool = is_bool($val); =begin original 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 C or C itself), boolean-returning operator (such as the C or C<==> comparison tests or the C negation operator), or any variable containing one of these results. This function used to be named C. A compatibility alias is provided currently but will be removed in a later version. =end original 特別な真偽値が与えられたときに真を返し、そうでなければ偽を返します。 特別な真偽値は、真偽値を返す組み込み関数(C や C 自身など)や 真偽値を返す演算子(C や C<==> の比較テストや C の否定演算子など)の結果、 あるいは、これらの結果を含む変数の値です。 この関数は、C という名前でした。現在は互換性のためのエイリアスが提供されていますが、 後のバージョンで削除されます。 =head2 weaken weaken($ref); =begin original 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 C. =end original リファレンスを弱くします。弱いリファレンスは、そのリファレンスのリファレンスカウントには寄与しません。 リファレンスへの弱いリファレンスだけが残っている場合、リファレンスは破棄され、残った弱いリファレンスは全てCに設定されます。 =head2 unweaken unweaken($ref); =begin original Strengthens a reference, undoing the effects of a previous call to L. =end original リファレンスを強くします。L による呼び出しの効果を元に戻します。 =head2 is_weak $bool = is_weak($ref); =begin original Returns true when given a weakened reference, or false if not a reference or not weak. This function used to be named C. A compatibility alias is provided currently but will be removed in a later version. =end original 弱いリファレンスが与えられた時に真を返し、そうでなければ偽を返します。 この関数は、C という名前でした。現在は互換性のためのエイリアスが提供されていますが、 後のバージョンで削除されます。 =head2 blessed $str = blessed($ref); =begin original Returns the package name for an object reference, or C for a non-reference or reference that is not an object. =end original オブジェクトリファレンスであればパッケージ名を返し、 リファレンスでない場合やオブジェクトリファレンスでない場合はCを返します。 =head2 refaddr $num = refaddr($ref); =begin original Returns the memory address for a reference, or C 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. =end original リファレンスのメモリアドレスを返し、リファレンスでない場合はCを返します。 この値は、純粋なPerlコードではあまり有用ではありませんが、参照の同一性や一意性をテストする手段として便利です。 =head2 reftype $str = reftype($ref); =begin original Returns the basic container type of the referent of a reference, or C for a non-reference. This is returned as a string in all-capitals, such as C for array references, or C for hash references. =end original リファレンスの参照先のデータ型を返し、リファレンスでない場合はCを返します。 これは、すべて大文字の文字列として返されます。たとえば、配列リファレンスの場合はC、 ハッシュリファレンスの場合はCなどです。 =head2 created_as_string $bool = created_as_string($val); =begin original 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 =end original 与えられた値が、元々文字列として作成されたかどうかを真偽値で返します。 最も最近の代入や変更が文字列のような性質であった任意のスカラ式は真を返します。例えば、 文字列リテラルからの代入や、連結や正規表現のような文字列操作の結果などです。 リファレンス(オブジェクトを含む)、数値、真偽値、undefに対しては偽を返します。 Perl内での通常のデータ検証にこの関数を使用することはあまり推奨されません。なぜなら、 文字列として完全に利用できる通常の数値であっても真を返さず、 また、文字列のように振る舞うように文字列化演算子をオーバーロードするオブジェクトを含む、 あらゆるオブジェクトリファレンスに対しても真を返さないからです。例えば、次のようなケースです。 my $val = URI->new( "https://metacpan.org/" ); if( created_as_string $val ) { ... } # this will not execute =head2 created_as_number $bool = created_as_number($val); =begin original 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 =end original 与えられた値が、元々数値として作成されたかどうかを真偽値で返します。 最も最近の代入や変更が数値のような性質であった任意のスカラ式は真を返します。例えば、 数値リテラルからの代入や、加算のような数値操作の結果などです。 リファレンス(オブジェクトを含む)、文字列、真偽値、undefに対しては偽を返します。 Perl内での通常のデータ検証にこの関数を使用することはあまり推奨されません。なぜなら、 数値として完全に利用できる10進数の文字列であっても真を返さず、 また、数値のように振る舞うように数値化演算子をオーバーロードするオブジェクトを含む、 あらゆるオブジェクトリファレンスに対しても真を返さないからです。例えば、次のようなケースです。 my $val = Math::BigInt->new( 123 ); if( created_as_number $val ) { ... } # this will not execute =begin original 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. =end original ほとんどのPerlコードは、スカラ値の作成履歴を知ることなく操作するべきですが、 これらの二つの関数は、JSONエンコーダなどのデータシリアライゼーションモジュールなどにおいて利用されることを意図しています。 そういった状況では、言語間の相互運用性の観点で、文字列の性質と数値の性質を区別する必要性が生じるからです。 =head2 ceil $num = ceil($num); =begin original Returns the smallest integer value greater than or equal to the given numerical argument. =end original 与えられた数値引数以上の最小の整数値を返します。 =head2 floor $num = floor($num); =begin original Returns the largest integer value less than or equal to the given numerical argument. =end original 与えられた数値引数以下の最大の整数値を返します。 =head2 indexed @ivpairs = indexed(@items) =begin original 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 =end original 元のリストのインデックスを示す数値と、その位置の値から成るペアの数値/値のペアの偶数サイズのリストを返します。 つまり、元のリストの2倍のサイズのリストを返し、次と等価です。 (0, $items[0], 1, $items[1], 2, $items[2], ...) =begin original Note that unlike the core C function, this function returns copies of its original arguments, not aliases to them. Any modifications of these copies are I reflected in modifications to the original. =end original コアのC関数とは異なり、この関数は元の引数のエリアスではなく、コピーを返すことに注意してください。 これらのコピーにあらゆる変更をしても、元の値にはI<反映されません>。 my @x = ...; $_++ for indexed @x; # The @x array remains unaffected =begin original This function is primarily intended to be useful combined with multi-variable C loop syntax; as =end original この関数は、複数変数のCループ構文と組み合わせて使用することを主に意図しています。 foreach my ($index, $value) (indexed LIST) { ... } =begin original In scalar context this function returns the size of the list that it would otherwise have returned, and provokes a warning in the C category. =end original スカラコンテキストでは、この関数が返すはずのリストのサイズを返し、Cカテゴリの警告を発生させます。 =head2 trim $stripped = trim($string); =begin original Returns the input string with whitespace stripped from the beginning and end. trim() will remove these characters: =end original 入力文字列の先頭と末尾から空白を取り除いた文字列を返します。Cは、次の文字を取り除きます。 " ", an ordinary space. "\t", a tab. "\n", a new line (line feed). "\r", a carriage return. =begin original and all other Unicode characters that are flagged as whitespace. A complete list is in L. そして、空白としてフラグ付けされているすべてのUnicode文字を取り除きます。 完全な一覧は、Lにあります。 =end original $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" =begin original C is equivalent to: =end original Cは次と等価です。 $str =~ s/\A\s+|\s+\z//urg; =begin original For Perl versions where this feature is not available look at the L module for a comparable implementation. =end original このfeatureが利用できないPerlバージョンでは、同等の実装を持つLモジュールを参照してください。 =head1 SEE ALSO L, L, L =cut