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 ($filehandle, $filename) = tempfile(...) you should be safe (assuming your temp directory isn't on NFS).

In reply to Re: Avoiding race conditions by nardo
in thread Avoiding race conditions by swiftone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.