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

I am familiar with chmod() but I want to be able to tell if a file is a+w g+w or o+w instead of just writable. is there a way to do this with out doing something fancy with chmod? I don't want to change the perms I just want to check to see if lets say any of my files in my $HOME are world writable. -(tango mike)

Replies are listed 'Best First'.
Re: permissions
by lshatzer (Friar) on Jul 13, 2001 at 22:21 UTC
    Try looking at: perlfunc:_X, it has various filetest operators.
Re: permissions
by thatguy (Parson) on Jul 14, 2001 at 00:42 UTC
    I tried using File::stat (which would be another way to do this) but I couldn't grasp what it was doing..

    It suggests something like:

    use File::stat; $st = stat($file) or die "No $file: $!"; if ( ($st->mode & 0111) && $st->nlink > 1) ) { print "$file is executable with lotsa links\n"; }

    My problem was the 0111 and how I could tell if the file was writeable even if it were a readable and executable by group/user/world.. those are different numbers. I guess I would have to know and test against these numbers..

    Does anyone have any insight on that? In the interim, there is this oogly thing..

    my @listfiles=`ls -l ./`; foreach(@listfiles) { chomp; $_=~s/ /\t/g; my @details = split(/\t/, $_); print "$details[-1]: $details[0]\n"; }

    it works, but then, it's just a ls -l of the contents of the dir. whoo.
      Just use the regular stat function.

      From perldoc -f stat

      -----------------------------
      Because the mode contains both the file type and its permissions, you should mask off the file type portion and (s)printf using a C<"%o"> if you want to see the real permissions.

      $mode = (stat($filename))[2]; printf "Permissions are %04o\n", $mode & 07777;
      -----------------------------

      -Blake

        Excellent! that did just what I wanted. Thanks!

        Why is it that I everything I do in perl must be so long? heh.