[Wellington-pm] perl -f check failed

Grant McLean grant at mclean.net.nz
Tue Jul 20 20:24:39 PDT 2010


On Wed, 2010-07-21 at 15:23 +1200, Grant McLean wrote:
> On Wed, 2010-07-21 at 15:13 +1200, Jethro Carr wrote:
> > Essentially the script creates a number of text files whilst running and
> > checks if any exist using -f - if they do, it knows to use a different
> > name for the new file.
> 
> That sounds like a classic race condition.  If you check whether the
> file exists before you create it then it's possible that another process
> might create it between the time your check returns false and when you
> create the file.
> 
> See "perldoc perlopentut" for a description of using O_EXCL to have the
> open fail if the file already exists.  Then check for that specific
> error in $! if the open fails.

Or use the core File::Temp module which can do all this for you.

Cheers
Grant



More information about the Wellington-pm mailing list