http://www.perlmonks.org?node_id=8221

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

How do I test whether or not a file is writable?

Originally posted as a Categorized Question.

  • Comment on How do I test whether or not a file is writable?

Replies are listed 'Best First'.
Re: How do I test whether or not a file is writable?
by chromatic (Archbishop) on Apr 20, 2000 at 22:05 UTC
    The -w test will do:
    if (-w FILEHANDLE) { print "It's writeable by my effective uid or gid!\n"; }
    For more information, see -X (or perldoc -f -X).
Re: How do I test whether or not a file is writable?
by kiruthika.bkite (Scribe) on Mar 18, 2010 at 10:56 UTC
    if(-f $name ) { if(!(-w $name)) { print "File '$name' doesn't have write permission\n"; } }

    -f switch is used to check whether $name is file or not.
    -w switch is used to test whether file has write permission or not.
      There's no point testing for both -f and -w. If the file doesn't exists (or if its existence cannot be determined), -w will return false. And if -w returns true, the file exists.
        Yes, but -w would also return true if the file is actually a writeable directory
A reply falls below the community's threshold of quality. You may see it by logging in.