in reply to converting stat() $mode to unix file permissions

printf '%o', (stat $filename)[2] & 07777; # Updated: Thanks to [duff] $unixmode = (stat $filename)[2] & 07777; # Updated. One too few 7s.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!

Replies are listed 'Best First'.
Re: Re: converting stat() $mode to unix file permissions
by duff (Parson) on Jan 25, 2004 at 17:00 UTC

    I think perhaps he'd want to use a bit mask of 07777 instead of 0777 so that he could get the set*id bits. In fact, I think there's even an example in perldoc -f stat that shows exactly this.

Re: Re: converting stat() $mode to unix file permissions
by knowmad (Monk) on Mar 30, 2004 at 19:14 UTC

    Note that in the example, the $unixmode will come out in decimal mode. You may therefore prefer to use the following code to convert the output to the more common octal format:

    $unixmode = sprintf("04%o", (stat $filename)[2] & 07777)