You can use File::Temp to return both the filehandle and filename. When this is done, there is no race condition. When File::Temp opens a file, it does so with O_CREAT|O_EXCL set in the flags and a mode of 600, which ensures that the file will be created and only you will be able to read/write to it. A race condition would be to return only the filename and then open() it because the file could have been generated by someone else in between the time of File::Temp checking to see if it exists and you opening it. So, if you use the File::Temp functions in list format
you should be safe (assuming your temp directory isn't on NFS).