Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

How do I test if a file has write permission?

by ChuckularOne (Parson)
on Apr 20, 2000 at 22:04 UTC ( #8224=perlquestion: print w/replies, xml ) Need Help??

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..

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://8224]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2021-04-20 05:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found