ChuckularOne has asked for the wisdom of the Perl Monks concerning the following question:

I need help with the perlfunc stat(). Can anyone explain to me or point me to a node that explains how to read the 'mode' information it stat() returns?

A file with a mode of: 0755
returns a mode from stat() of: 33261

Replies are listed 'Best First'.
Re: Modes in perlfunc
by lhoward (Vicar) on Aug 02, 2000 at 22:34 UTC
    hint: 33261 expressed in octal (base-8) is 100755
Re: Modes in perlfunc
by clintp (Curate) on Aug 03, 2000 at 07:25 UTC
    For more information on this, find yourself a Unix system and check out the sys/stat.h file (you may have to follow a chain of #includes on particularly strange OS's like Linux) but you're looking for a file that contains #defines like this:

    #define __S_IFMT 0170000 /* These bits determine file type. */ /* File types. */ #define __S_IFDIR 0040000 /* Directory. */ #define __S_IFCHR 0020000 /* Character device. */ #define __S_IFBLK 0060000 /* Block device. */ #define __S_IFREG 0100000 /* Regular file. */ #define __S_IFIFO 0010000 /* FIFO. */
    And so on. If you bitwise AND these definitions against that 6-digit octal number (st_mode), you'll see how the -X file tests work in Perl.
Re: Modes in perlfunc
by Anonymous Monk on Aug 03, 2000 at 03:30 UTC
    I just figured out how to do this today, actually I finally found it on a newsgroup:

    my $mode = (stat("file"))2;
    my @mode = (($mode & 7000)>>9, ($mode & 0700)>>6, ($mode & 0070)>>3, ($mode & 0007));
    my $finalmode = join('', @modelist);


Re: Modes in perlfunc
by ChuckularOne (Prior) on Aug 02, 2000 at 22:38 UTC
    Thank you kind sir. I had tried converting it between everything and everything else, but didn't think to look at just a portion of the number.

    Your Humble Servant,
    -Chuck