in reply to chmod and oct

But when you want to make the permissions variable, you need to oct it. Can anyone explain why?

The best explanation is in the documentation (perlfunc, in this case), which says, in part

The first element of the list must be the numerical mode, which should probably be an octal number, and which definitely should not a string of octal digits: 0644 is okay, '0644' is not.

It's the bit pattern behind the number that is significant. Octal is used by convention, because the bits that chmod() cares about when specifying file protection are in three groups of three* -- a natural fit for octal. You could just as well specify them in hex or in decimal, though doing so obscures the intent of the operation.

chmod 0777, $file; chmod 0x1FF, $file; chmod 511, $file;
are equivalent.

*Additional bits are used for non-protection stuff like setuid, but it's the 9 low-order protection bits that people usually care about.

Replies are listed 'Best First'.
Re: Re: chmod and oct
by mce (Curate) on Dec 06, 2002 at 08:48 UTC
    Thanks,

    Than my first code was wrong, as it should be:

    chmod 0755, $file; # and not chmod '0755', $file;
    It is because my variable $chmod is regarded as a string and not a number, that is fails.

    Why doesn't a trick like this work then?

    $chmod='0644'; chmod $chmod+0, $file;
    Please note that it is friday today, and this might explain my stuped questions :-)


    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

      Why doesn't a trick like this work then?
      $chmod='0644'; chmod $chmod+0, $file;
      It doesn't work because "leading zero means octal" applies only to numbers, not strings of numeric digits.

      Try this:   print 0644, " != ", '0644' + 0, "\n"; This is all described in the on-line doc, in perldata.