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

Please forgive my ignorance O' Great Monks, But is there a function or Module that will give me directory attributes such as owner,UID/GID/ect.. without having to issue system commands to extract the information instead of the below code??? Many thanks O' Magnificant Ones
#!/usr/bin/perl -w #pass a directory to script use strict; my $ownergroup=`ls -ld $ARGV[0]\| awk '{print \$3":"\$4}'`; print $ownergroup;

Replies are listed 'Best First'.
Re: File/Dir Attributes
by gellyfish (Monsignor) on Jun 21, 2005 at 18:53 UTC

    Yep, you want to look at the stat builtin function:

    my $filename = $ARGV[0]; + my ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks ) = stat($filename); + print "UID: $uid\nGID: $gid\n";

    /J\

      Very KEWL!!! Thanks a ton, that just made it so much easier to do what I am wanting to do. Thanks again gellyfish, You ROCK!

        I showed all of the values returned from stat for didactic purposes - if you just want the UID and GID you would be better to use an array slice:

        my ($uid,$gid) = (stat($filename))[4,5];

        /J\

      Hi

      Is there anything which equals to Unix 'file' command?

      Thanks,
      ShekarKCB
Re: File/Dir Attributes
by Xaositect (Friar) on Jun 21, 2005 at 18:56 UTC

    The stat() builtin function is what you're looking for. You may also be interested in the -X type functions which test specific bits of the stat info.


    Xaositect - Whitepages.com