The Perl code 0666 produces the number four hundred and thirty-eight, which is 666 in octal and 438 in decimal.

Four hundred and thirty-eight is the correct value to pass to the chmod builtin to get rw-rw-rw-.

The Perl code 0777 produces the number five hundred and eleven, which is 777 in octal and 511 in decimal.

Five hundred and eleven is the correct value to pass to the chmod builtin to get rwxrwxrwx.


Quoting the mode causes it to lose the leading zero so that sub(q(0666)) becomes 666

The Perl code q(0666) produces the four-character string 0666.

If you were to treat that string as a number (e.g. 0+q(0666)) it would get converted in to the number six hundred and sixty-six. When Perl numifies strings, leading zeros do not have a special significance.

Six hundred and sixty-six is NOT the correct value to pass to the chmod builtin to get rw-rw-rw-.


If you want to print those numbers in octal, you can use sprintf "%o", $n.

$ perl -Mv5.14 -e'say sprintf "%o", $_ for 0666, 0777;' 666 777

Feel free to add a leading zero if you so desire.

$ perl -Mv5.14 -e'say sprintf "0%o", $_ for 0666, 0777;' 0666 0777

In reply to Re: octal number mysteriously changes after pass to subroutine by ikegami
in thread octal number mysteriously changes after pass to subroutine by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.