From: PerlFAQ Server on
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at .


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

(contributed by brian d foy)

You're probably trying to convert a string to a number, which Perl only
converts as a decimal number. When Perl converts a string to a number,
it ignores leading spaces and zeroes, then assumes the rest of the
digits are in base 10:

my $string = '0644';

print $string + 0; # prints 644

print $string + 44; # prints 688, certainly not octal!

This problem usually involves one of the Perl built-ins that has the
same name a Unix command that uses octal numbers as arguments on the
command line. In this example, "chmod" on the command line knows that
its first argument is octal because that's what it does:

%prompt> chmod 644 file

If you want to use the same literal digits (644) in Perl, you have to
tell Perl to treat them as octal numbers either by prefixing the digits
with a 0 or using "oct":

chmod( 0644, $file); # right, has leading zero
chmod( oct(644), $file ); # also correct

The problem comes in when you take your numbers from something that Perl
thinks is a string, such as a command line argument in @ARGV:

chmod( $ARGV[0], $file); # wrong, even if "0644"

chmod( oct($ARGV[0]), $file ); # correct, treat string as octal

You can always check the value you're using by printing it in octal
notation to ensure it matches what you think it should be. Print it in
octal and decimal format:

printf "0%o %d", $number, $number;


The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in