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

From the command line I am building a script to determining if the user has read, write, and execute permissions of a file they input.

How do you find out what permissions you have for a file?

The script will be run like: perl permissions.pl C:\Documents and Settings\ ... file.extension

Replies are listed 'Best First'.
Re: checking file permissions
by tachyon (Chancellor) on Nov 14, 2004 at 00:47 UTC
    die "$file does not exist!\n" unless -e $file; my @perms; push @perms, 'read' if -r $file; push @perms, 'write' if -w $file; push @perms, 'execute' if -x $file; print @perms ? "You have @perms perms\n" : "You got zip\n";

    cheers

    tachyon

      Looks like you just did this tyke's homework. Bad Tachyon.

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

        Actually he has since added a clarification that places him on Win32, so he probably really wants Dave Roth's Win32::Perms

        cheers

        tachyon

      Also a good place to introduce _ even if it's not saving much here (discussed in JediWizard's perldoc.com link; _ is a hard thing to search for).

      die "$file does not exist!\n" unless -e $file; my @perms; push @perms, 'read' if -r _; push @perms, 'write' if -w _; push @perms, 'execute' if -x _; if ( @perms ) { printf "You have %s perms\n", join(", ", @perms); } else { print "You got zip\n"; }
Re: checking file permissions
by Old_Gray_Bear (Bishop) on Nov 13, 2004 at 20:24 UTC
    In solving this problem for my Perl 101 class, I used the file test operators.

    ----
    I Go Back to Sleep, Now.

    OGB

      You can be a little more detailed or link me to a node or page I can look at this? I have no idea what to even search for and I searched "file test operators".

      Thanks

        -X

        May the Force be with you
Re: checking file permissions
by Zaxo (Archbishop) on Nov 14, 2004 at 01:19 UTC

    You'll need to quote that file path in the command line usage you want. Otherwise it will be broken on spaces.

    After Compline,
    Zaxo