Well, if you only ever have "r", "w", "x", and "-" present, then you can use pack/unpack for this:
Use sprintf "0%o" on the return value if you want an octal string, but don't try to use that octal string as a number or you'll get the wrong number:sub perm2mode { my( $perm )= @_; if( 9 != length($perm) || 9 != ( $perm =~ tr/rwx-/1110/ ) ) { die "Unsupported permission string ($_[0])"; } return unpack "v", pack "b*", ''.reverse$perm; }
produces:my $mode= perm2mode("rw-r----x"); my $str= sprintf "0%o", $mode; print "mode=$mode str=$str\n"; my $bad= 0+$str; printf "bad=%d=0%o mode=%d=0%o\n", $bad, $bad, $mode, $mode;
So, for example, using chmod $str, $file; would be wrong just like chmod "0660", $file; is. - tye (but my friends call me "Tye")mode=417 str=0641 bad=641=01201 mode=417=0641
In reply to (tye)Re: Converting Permissionstring to Octal
by tye
in thread Converting Permissionstring to Octal
by stefan k
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |