perl-5.38.0
oct EXPR
oct

Interprets EXPR as an octal string and returns the corresponding value. An octal string consists of octal digits and, as of Perl 5.33.5, an optional 0o or o prefix. Each octal digit may be preceded by a single underscore, which will be ignored. (If EXPR happens to start off with 0x or x, interprets it as a hex string. If EXPR starts off with 0b or b, it is interpreted as a binary string. Leading whitespace is ignored in all three cases.) The following will handle decimal, binary, octal, and hex in standard Perl notation:

EXPR を 8 進数文字列と解釈して、対応する値を返します。 8 進数文字列は、8 進文字と、Perl 5.33.5 からはオプションの 0o または o 接頭辞からなります。 各 8 進文字の前には一つの下線を置くことができ、これは無視されます。 (EXPR が 0xx で始まるときには、16 進数文字列と解釈します。 EXPR が 0bb で始まるときは、2 進数文字列と解釈します。 どの場合でも、先頭の空白は無視されます。) 以下の例は、標準的な Perl の記法での 10 進数、2 進数、8 進数、16 進数を扱います:

    $val = oct($val) if $val =~ /^0/;

If EXPR is omitted, uses $_. To go the other way (produce a number in octal), use sprintf or printf:

EXPR が省略されると、$_ を使います。 (8 進数を扱う)その他の方法をとしては sprintfprintf があります:

    my $dec_perms = (stat("filename"))[2] & 07777;
    my $oct_perm_str = sprintf "%o", $perms;

The oct function is commonly used when a string such as 644 needs to be converted into a file mode, for example. Although Perl automatically converts strings into numbers as needed, this automatic conversion assumes base 10.

oct 関数は例えば、 644 といった文字列をファイルモードに 変換する時によく使います。 Perl は必要に応じて自動的に文字列を数値に変換しますが、 この自動変換は十進数を仮定します。

Leading white space is ignored without warning, as too are any trailing non-digits, such as a decimal point (oct only handles non-negative integers, not negative integers or floating point).

先頭の空白や、末尾の(小数点のような)非数字は警告なしに無視されます (oct は非負整数のみを扱えます; 負の整数や小数は扱えません)。