in reply to Re: Turning 0755 into rwxr-xr-x
in thread Turning 0755 into rwxr-xr-x

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.

Replies are listed 'Best First'.
Re^3: Turning 0755 into rwxr-xr-x
by gellyfish (Monsignor) on Aug 11, 2005 at 17:54 UTC

    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.