perl-5.38.0
unpack TEMPLATE,EXPR
unpack TEMPLATE

unpack does the reverse of pack: it takes a string and expands it out into a list of values. (In scalar context, it returns merely the first value produced.)

unpackpack の逆を 行ないます: 構造体を表わす文字列をとり、 リスト値に展開し、その配列値を返します。 (スカラコンテキストでは、単に最初の値を返します。)

If EXPR is omitted, unpacks the $_ string. See perlpacktut for an introduction to this function.

EXPR が省略されると、$_ の文字列を unpack します。 この関数の説明については perlpacktut を参照してください。

The string is broken into chunks described by the TEMPLATE. Each chunk is converted separately to a value. Typically, either the string is a result of pack, or the characters of the string represent a C structure of some kind.

文字列は TEMPLATE で示された固まりに分割されます。 それぞれの固まりは別々に値に変換されます。 典型的には、文字列は pack の結果あるいはある種の C の構造体の文字列表現の文字列です。

The TEMPLATE has the same format as in the pack function. Here's a subroutine that does substring:

TEMPLATE は、pack 関数と同じフォーマットを使います。 部分文字列を取り出すうサブルーチンの例を示します:

    sub substr {
        my ($what, $where, $howmuch) = @_;
        unpack("x$where a$howmuch", $what);
    }

and then there's

これもそうです。

    sub ordinal { unpack("W",$_[0]); } # same as ord()

In addition to fields allowed in pack, you may prefix a field with a %<number> to indicate that you want a <number>-bit checksum of the items instead of the items themselves. Default is a 16-bit checksum. The checksum is calculated by summing numeric values of expanded values (for string fields the sum of ord($char) is taken; for bit fields the sum of zeroes and ones).

pack で利用可能なフィールドの他に、 フィールドの前に %<数値> というものを付けて、 項目自身の代わりに、その項目の <数値>-ビットのチェックサムを 計算させることができます。 デフォルトは、16-ビットチェックサムです。 チェックサムは展開された値の数値としての値の合計 (文字列フィールドの場合は ord($char) の合計; ビットフィールドの場合は 0 と 1 の合計) が用いられます。

For example, the following computes the same number as the System V sum program:

たとえば、以下のコードは System V の sum プログラムと同じ値を計算します。

    my $checksum = do {
        local $/;  # slurp!
        unpack("%32W*", readline) % 65535;
    };

The following efficiently counts the number of set bits in a bit vector:

以下は、効率的にビットベクターの設定されているビットを 数えるものです。

    my $setbits = unpack("%32b*", $selectmask);

The p and P formats should be used with care. Since Perl has no way of checking whether the value passed to unpack corresponds to a valid memory location, passing a pointer value that's not known to be valid is likely to have disastrous consequences.

pP は注意深く使うべきです。 Perl は unpack に渡された値が有効なメモリ位置を 指しているかどうかを確認する方法がないので、有効かどうかわからない ポインタ値を渡すと悲惨な結果を引き起こすかもしれません。

If there are more pack codes or if the repeat count of a field or a group is larger than what the remainder of the input string allows, the result is not well defined: the repeat count may be decreased, or unpack may produce empty strings or zeros, or it may raise an exception. If the input string is longer than one described by the TEMPLATE, the remainder of that input string is ignored.

多くの pack コードがある場合や、フィールドやグループの繰り返し回数が 入力文字列の残りより大きい場合、結果は未定義です: 繰り返し回数が減らされる場合もありますし、 unpack が空文字列や 0 を 返すこともありますし、例外が発生します。 もし入力文字列が TEMPLATE で表現されているものより大きい場合、 入力文字列の残りは無視されます。

See pack for more examples and notes.

さらなる例と注意に関しては pack を参照してください。