FAQ 4.3 Why isn't my octal data interpreted correctly?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


4.3: Why isn't my octal data interpreted correctly?

    Perl only understands octal and hex numbers as such when they occur as
    literals in your program. Octal literals in perl must start with a
    leading "0" and hexadecimal literals must start with a leading "0x". If
    they are read in from somewhere and assigned, no automatic conversion
    takes place. You must explicitly use oct() or hex() if you want the
    values converted to decimal. oct() interprets hex ("0x350"), octal
    ("0350" or even without the leading "0", like "377") and binary
    ("0b1010") numbers, while hex() only converts hexadecimal ones, with or
    without a leading "0x", like "0x255", "3A", "ff", or "deadbeef". The
    inverse mapping from decimal to octal can be done with either the "%o"
    or "%O" sprintf() formats.

    This problem shows up most often when people try using chmod(), mkdir(),
    umask(), or sysopen(), which by widespread tradition typically take
    permissions in octal.

        chmod(644,  $file); # WRONG
        chmod(0644, $file); # right

    Note the mistake in the first line was specifying the decimal literal
    644, rather than the intended octal literal 0644. The problem can be
    seen with:

        printf("%#o",644); # prints 01204

    Surely you had not intended "chmod(01204, $file);" - did you? If you
    want to use numeric literals as arguments to chmod() et al. then please
    try to express them as octal constants, that is with a leading zero and
    with the following digits restricted to the set 0..7.


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline