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
|