perl-5.42.0
pack TEMPLATE,LIST

Takes a LIST of values and converts it into a string using the rules given by the TEMPLATE. The resulting string is the concatenation of the converted values. Typically, each converted value looks like its machine-level representation. For example, on 32-bit machines an integer may be represented by a sequence of 4 bytes, which will in Perl be presented as a string that's 4 characters long.

LIST の値を TEMPLATE で与えられたルールを用いて文字列に変換します。 結果の文字列は変換した値を連結したものです。 典型的には、それぞれの変換された値はマシンレベルの表現のように見えます。 例えば、32-bit マシンでは、整数は 4 バイトで表現されるので、 Perl では 4 文字の文字列で表現されます。

See perlpacktut for an introduction to this function.

この関数の説明については perlpacktut を参照してください。

The TEMPLATE is a sequence of characters that give the order and type of values, as follows:

TEMPLATE は、以下のような値の型と順番を指定する文字を並べたものです:

    a  A string with arbitrary binary data, will be null padded.
    A  A text (ASCII) string, will be space padded.
    Z  A null-terminated (ASCIZ) string, will be null padded.
    a  任意のバイナリデータを含む文字列、ヌル文字で埋める。
    A  テキスト (ASCII) 文字列、スペース文字で埋める。
    Z  ヌル文字終端 (ASCIZ) 文字列、ヌル文字で埋める。
    b  A bit string (ascending bit order inside each byte,
       like vec()).
    B  A bit string (descending bit order inside each byte).
    h  A hex string (low nybble first).
    H  A hex string (high nybble first).
    b  ビット列 (バイトごとに昇ビット順、vec() と同じ)。
    B  ビット列 (バイトごとに降ビット順)。
    h  16 進数文字列 (低位ニブルが先)。
    H  16 進数文字列 (高位ニブルが先)。
    c  A signed char (8-bit) value.
    C  An unsigned char (octet) value.
    W  An unsigned char value (can be greater than 255).
    c  signed char (8 ビット) 値。
    C  unsigned char (オクテット) 値。
    W  unsigned char 値 (255 より大きいかもしれません)。
    s  A signed short (16-bit) value.
    S  An unsigned short value.
    s  signed short (16 ビット) 値。
    S  unsigned short 値。
    l  A signed long (32-bit) value.
    L  An unsigned long value.
    l  signed long (32 ビット) 値。
    L  unsigned long 値。
    q  A signed quad (64-bit) value.
    Q  An unsigned quad value.
         (Quads are available only if your system supports 64-bit
          integer values _and_ if Perl has been compiled to support
          those.  Raises an exception otherwise.)
    q  符号付き 64 ビット整数。
    Q  符号なし 64 ビット整数。
      (64 ビット整数は、システムが 64 ビット整数に対応していて、かつ Perl が
       64 ビット整数対応としてコンパイルされている場合にのみ使用可能です。
           それ以外の場合は例外が発生します。)
    i  A signed integer value.
    I  An unsigned integer value.
         (This 'integer' is _at_least_ 32 bits wide.  Its exact
          size depends on what a local C compiler calls 'int'.)
    i  signed int 値。
    I  unsigned int 値。
       (ここでの 'integer' は 「最低」 32 ビット幅です。正確なサイズは
        ローカルの C コンパイラの 'int' のサイズに依存します。)
    n  An unsigned short (16-bit) in "network" (big-endian) order.
    N  An unsigned long (32-bit) in "network" (big-endian) order.
    v  An unsigned short (16-bit) in "VAX" (little-endian) order.
    V  An unsigned long (32-bit) in "VAX" (little-endian) order.
    n  "network" 順序 (ビッグエンディアン) の unsigned short (16 ビット)。
    N  "network" 順序 (ビッグエンディアン) の unsigned long (32 ビット)。
    v  "VAX" 順序 (リトルエンディアン) の unsigned short (16 ビット)。
    V  "VAX" 順序 (リトルエンディアン) の unsigned long (32 ビット)。
    j  A Perl internal signed integer value (IV).
    J  A Perl internal unsigned integer value (UV).
    j  Perl 内部符号付き整数 (IV)。
    J  Perl 内部符号なし整数 (UV)。
    f  A single-precision float in native format.
    d  A double-precision float in native format.
    f  機種依存の単精度浮動小数点数。
    d  機種依存の倍精度浮動小数点数。
    F  A Perl internal floating-point value (NV) in native format
    D  A float of long-double precision in native format.
         (Long doubles are available only if your system supports
          long double values. Raises an exception otherwise.
          Note that there are different long double formats.)
    F  ネイティブフォーマットの Perl 内部浮動小数点数 (NV)
    D  ネイティブフォーマットの長い倍精度浮動小数点数(long double)。
      (long double は、システムが long double に対応している場合にのみ
          使用可能です。それ以外の場合は例外が発生します。
          long double 型式の場合は異なることに注意してください。)
    p  A pointer to a null-terminated string.
    P  A pointer to a structure (fixed-length string).
    p  ヌル文字で終端する文字列へのポインタ。
    P  構造体 (固定長文字列) へのポインタ。
    u  A uuencoded string.
    U  A Unicode character number.  Encodes to a character in char-
       acter mode and UTF-8 (or UTF-EBCDIC in EBCDIC platforms) in
       byte mode.  Also on EBCDIC platforms, the character number will
       be the native EBCDIC value for character numbers below 256.
       This allows most programs using this feature to not have to
       care which type of platform they are running on.
    u  uuencode 文字列。
    U  Unicode 文字番号。文字モードでは文字に、バイトモードなら UTF-8 に
       (EBCDIC システムでは UTF-EBCDIC に)エンコードされます。
       また EBCDIC プラットフォームでは、文字番号が 256 より下の場合は
       EBCDIC にネイティブな値です。
       これにより、この機能を使うほとんどのプログラムで、どの種類の
       プラットフォームで実行されるかを気にする必要がなくなります。
    w  A BER compressed integer (not an ASN.1 BER, see perlpacktut
       for details).  Its bytes represent an unsigned integer in
       base 128, most significant digit first, with as few digits
       as possible.  Bit eight (the high bit) is set on each byte
       except the last.
    w  A BER 圧縮変数(ASN.1 BER ではありません; 詳細については perlpacktut を
       参照してください)。このバイト列はできるだけ少ない桁数で表現された
       128 を基とした符号なし整数で、最上位ビットから順に並びます。
       最後のバイト以外の各バイトのビット 8 (上位ビット) がセットされます。
    x  A null byte (a.k.a. ASCII NUL, "\000", chr(0))
    X  Back up a byte.
    @  Null-fill or truncate to absolute position, counted from the
       start of the innermost ()-group.
    .  Null-fill or truncate to absolute position specified by
       the value.
    (  Start of a ()-group.
    x  ヌル文字 (つまり ASCII NUL, "\000", chr(0))
    X  1 文字後退。
    @  一番内側の () の組の開始位置から数えて、絶対位置までヌル文字で
       埋めるか切り詰める。
    .  値で指定した絶対位置までヌル文字で埋めるか切り詰める。
    (  () の組の開始。

One or more modifiers below may optionally follow certain letters in the TEMPLATE (the second column lists letters for which the modifier is valid):

以下に示す一つまたは複数の修飾子を、TEMPLATE の文字のいくつかにオプションで 付けることができます(表の 2 列目は、その修飾子が有効な文字です):

    !   sSlLiI     Forces native (short, long, int) sizes instead
                   of fixed (16-/32-bit) sizes.
    !   sSlLiI     固定の(16/32 ビット)サイズではなく、ネイティブな
                   (short, long, int)サイズを強制する。
    !   xX         Make x and X act as alignment commands.
    !   xX         x と X をアライメントコマンドとして振舞わせる。
    !   nNvV       Treat integers as signed instead of unsigned.
    !   nNvV       整数を符号なしではなく符号付きとして扱わせる。
    !   @.         Specify position as byte offset in the internal
                   representation of the packed string.  Efficient
                   but dangerous.
    !   @.         pack された内部表現のバイトオフセットとして位置を指定する。
                   効率的ですが危険です。
    >   sSiIlLqQ   Force big-endian byte-order on the type.
        jJfFdDpP   (The "big end" touches the construct.)
    >   sSiIlLqQ   これらの型のバイト順をビッグエンディアンに強制します。
        jJfFdDpP   (「大きい端」が構造に触れています。)
    <   sSiIlLqQ   Force little-endian byte-order on the type.
        jJfFdDpP   (The "little end" touches the construct.)
    <   sSiIlLqQ   これらの型のバイト順をリトルエンディアンに強制します。
        jJfFdDpP   (「小さい端」が構造に触れています。)

The > and < modifiers can also be used on () groups to force a particular byte-order on all components in that group, including all its subgroups.

>< の修飾子は ()-グループでも使えます; この場合はそのグループと全ての副グループ内の全ての要素を特定のバイト順に 強制します。

The following rules apply:

以下の条件が適用されます:

Examples:

例:

    $foo = pack("WWWW",65,66,67,68);
    # foo eq "ABCD"
    $foo = pack("W4",65,66,67,68);
    # same thing
    $foo = pack("W4",0x24b6,0x24b7,0x24b8,0x24b9);
    # same thing with Unicode circled letters.
    $foo = pack("U4",0x24b6,0x24b7,0x24b8,0x24b9);
    # same thing with Unicode circled letters.  You don't get the
    # UTF-8 bytes because the U at the start of the format caused
    # a switch to U0-mode, so the UTF-8 bytes get joined into
    # characters
    $foo = pack("C0U4",0x24b6,0x24b7,0x24b8,0x24b9);
    # foo eq "\xe2\x92\xb6\xe2\x92\xb7\xe2\x92\xb8\xe2\x92\xb9"
    # This is the UTF-8 encoding of the string in the
    # previous example

    $foo = pack("ccxxcc",65,66,67,68);
    # foo eq "AB\0\0CD"

    # NOTE: The examples above featuring "W" and "c" are true
    # only on ASCII and ASCII-derived systems such as ISO Latin 1
    # and UTF-8.  On EBCDIC systems, the first example would be
    #      $foo = pack("WWWW",193,194,195,196);

    $foo = pack("s2",1,2);
    # "\001\000\002\000" on little-endian
    # "\000\001\000\002" on big-endian

    $foo = pack("a4","abcd","x","y","z");
    # "abcd"

    $foo = pack("aaaa","abcd","x","y","z");
    # "axyz"

    $foo = pack("a14","abcdefg");
    # "abcdefg\0\0\0\0\0\0\0"

    $foo = pack("i9pl", gmtime);
    # a real struct tm (on my system anyway)

    $utmp_template = "Z8 Z8 Z16 L";
    $utmp = pack($utmp_template, @utmp1);
    # a struct utmp (BSDish)

    @utmp2 = unpack($utmp_template, $utmp);
    # "@utmp1" eq "@utmp2"

    sub bintodec {
        unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
    }

    $foo = pack('sx2l', 12, 34);
    # short 12, two zero bytes padding, long 34
    $bar = pack('s@4l', 12, 34);
    # short 12, zero fill to position 4, long 34
    # $foo eq $bar
    $baz = pack('s.l', 12, 4, 34);
    # short 12, zero fill to position 4, long 34

    $foo = pack('nN', 42, 4711);
    # pack big-endian 16- and 32-bit unsigned integers
    $foo = pack('S>L>', 42, 4711);
    # exactly the same
    $foo = pack('s<l<', -42, 4711);
    # pack little-endian 16- and 32-bit signed integers
    $foo = pack('(sl)<', -42, 4711);
    # exactly the same

The same template may generally also be used in unpack.

一般的には、同じテンプレートが unpack でも 使用できます。