While "safe" might be in the eye of the beholder (and I know I'm going to draw comments on that statement) reliable is another matter.

First off, I try and write my code in such a way that I don't need temporary files. Leaving surds all over the disk is somthing I don't like to do and I'll go to great lengths to avoid it. My biggest issue with temporary files is they tend to become "permanent" files because quite often either because of oversight on the behalf of the programmer or because Something Bad Happened™ to the process during its lifecycle the temporary files don't get cleaned up after.

Having asserted that premise I'll go on to say that in the rare occasion that I need a temporary file I tend to use one of two methods.

Name it by PID

The most common way I enact a temporary file is to create a filename that is related to both the reason I'm creating it and the process id for example:

my $unsortedUsersFilename="munger-unsortedusers.$$.txt"; | etc
In this example, munger is the name of the script doing the work, unsertedusers refers to the fact that it will contain a set of unsorted users (hey! work with me here!) and the $$ is the *nix PID of the script that is creating it.

During debugging sessions I may well leave off the step of removing the temp file so I can examine this file and know what was going on if the code doesn't work the way I expected it to.

The don't reinvent the wheel method

I like using File::Temp once my script has been debugged and I no longer care about the name of the being generated. While the module does provide a templating function for filenames, the names are still random. The biggest reason I like this module is it is platform portable. While 99.999% of my scripting is done on the *nix family of OSs every once in a while I end up writing something on the Evil Empire® platform and so I like things that I write to be portable.

Hope this at least provides you with food for thought.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

In reply to Re: Safe Temp Files by blue_cowdawg
in thread Safe Temp Files by gmock

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.