oct 
oct EXPR
octThis function interprets EXPR as an
octal string and returns the equivalent decimal value. If
EXPR happens to start with “0x”, it is interpreted as a hexadecimal string
instead. If EXPR starts off with “0b”, it is interpreted as a string of binary
digits. The following will properly convert to whole numbers input
strings in decimal, binary, octal, and hex bases using standard Perl
notation:
$val = oct $val if $val =~ /^0/;
For the inverse function, use sprintf with an appropriate format:
$dec_perms = (stat("filename"))[2] & 07777;
$oct_perm_str = sprintf "%o", $dec_perms;The oct function is commonly
used when a data 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.
Leading whitespace is ignored without warning, as are any trailing
nondigits, such as a decimal point (oct only handles nonnegative integers, not
negative integers or floating point).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access