in reply to Turning 0755 into rwxr-xr-x

Just in case you wondered how you might go about doing this yourself without using a module:

sub mode_to_string { my ($mode) = @_; + my @str_mode; + for (0 .. 2) { unshift @str_mode, $mode & 1 ? 'x':'-'; unshift @str_mode, $mode & 2 ? 'w':'-'; unshift @str_mode, $mode & 4 ? 'r':'-'; $mode = $mode >> 3; } + return join '', @str_mode; } + print mode_to_string(0647);
Of course you are almost certainly better off using the module as it will handle the setuid, setgid and sticky bits properly, but this is by way of example how things are often not as tricky as they seem.

/J\

Replies are listed 'Best First'.
Re^2: Turning 0755 into rwxr-xr-x
by Nkuvu (Priest) on Aug 11, 2005 at 17:40 UTC
    This isn't working properly for me. If I give the sub 755, it produces -wxrw--wx. Very different from rwxr-xr-x.

    So I revised the sub a bit:

    sub split_mode_to_string { my @sections = reverse split '', $_[0]; my @str_mode; for my $mode (@sections) { unshift @str_mode, $mode & 1 ? 'x':'-'; unshift @str_mode, $mode & 2 ? 'w':'-'; unshift @str_mode, $mode & 4 ? 'r':'-'; } return join '', @str_mode; }

    Which gives the right permissions for 755. As well as all of the other tests I tried (001, 010, 100, et cetera). I think the issue with the original sub is that 755 shifted right by 3 is not 75 -- it's 94.

      Er, well you would be right if you weren't missing the fact that the mode here is actually an octal number that has to begin with a zero in Perl - that is to say 0755 will work fine whereas the 755 will not.

      This is probably even easier to understand if you do:

      my $mode = (stat('.'))[2] & 07777; print $mode ,"\n"; printf "%o\n", $mode ; print mode_to_string($mode);
      Where the 07777 is a mask to remove the filetype information from the mode (as described in perlfunc).

      /J\

        Actually I see the issue now. I copied your sub, and changed to 755 in the code, which didn't work (for obvious reasons). Then I wrapped an input loop around it so that I could test different values, and 755 and 0755 are both read in as decimal values. Makes sense now. Sorry, been a long week.