in reply to How can I keep the name of a file as .tmp until it process and is closed and after that it should be .html
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
|
|---|