File::Temp handles most of the things that you really don't want to deal with yourself (race conditions, portability, etc.), and cleans up after itself nicely. In your case, you would prefer to take over that clean-up process so that the temp file is renamed rather than unlinked as the script exits. Just make sure to do so with as much exception-safety and error safety as possible. Here's an example:

use File::Temp; use File::Basename qw( basename ); use constant DEST_PATH => '/path/to/destination/'; use constant TEMP_PATH => '/tmp'; use constant TMP_TEMPLATE => 'prog_name.XXXX'; my $temp_fh = File::Temp->new( DIR => TEMP_PATH, TEMPLATE => TMP_TEMPLATE, UNLINK => 0 ); print "Temporary file created: ", $temp_fh->filename, "\n"; END{ my $new_filename = DEST_PATH . basename($temp_fh->filename) . '.html +'; rename $temp_fh->filename, $new_filename or do{ $temp_fh->unlink_on_destroy; die $temp_fh->filename, " couldn't be renamed: $!\n", "It will be unlinked instead."; }; print "Temp file was renamed to $new_filename\n"; }

This method will cause a temp file to be created using a known filename with some random digits appended. Then upon termination it renames the file to a new path while at the same time adding .html to the filename. The random digits are preserved. This assures that if your script runs concurrently the multiple runs won't clobber a single temp or single html file.


Dave


In reply to Re: How can I keep the name of a file as .tmp until it process and is closed and after that it should be .html by davido
in thread How can I keep the name of a file as .tmp until it process and is closed and after that it should be .html by Anonymous Monk

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.