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

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

The -w test will do:
if (-w FILEHANDLE) { print "It's writeable by my effective uid or gid!\n"; }
This code was credited to chromatic by the author. --Ed.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I test if a file has write permission?
by fx (Pilgrim) on Dec 19, 2003 at 23:35 UTC

    Another, but probably less efficient way, is to open the file in append mode. If the open fails the file COULD be read-only:

    open(FILE, ">>some_file.txt") || print "File is read only";

    The advantage of this way, I suppose, is that if you really want to write things to the end of the file then it's now open :)

      Open fails when it can't write, period. So if a directory where a file is expected or you have an NFS timeout, this will fail for non-permission reasons.

      (Why would your current directory be missing? If someone else rm's it of course :)


      Play that funky music white boy..