Many times in Perl applications you need to be able to create a temporary file (a Perl temp file). In this scenario you don't want to get into the system-specific details of temporary directories and things like that; you just want to create and use a temporary file.
Here are some Perl code snippets that should help get you started down this road.
How to create a Perl temp file
The process of creating and using temp files in your Perl programs is made easy with the File::Temp module. The first step in the process is to
use this module, and here's how I normally do that:
use File::Temp qw(tempfile);
Next, here's how I get a reference to a new Perl temp file:
# remove the file when the reference goes away with the UNLINK option $tmp_fh = new File::Temp( UNLINK => 1 );
Or, if you want the temp file to stay around after the reference goes away, set the
UNLINK option like this:
$tmp_fh = new File::Temp( UNLINK => 0 );
You can also access the Perl temp filename, like this:
print "temp filename: $tmp_fh\n";
Writing to the temp file
You can write to the temporary file just like you write to any other file with Perl, like this:
print $tmp_fh "Yada yada yada ...\n";
In fact, you can just treat this like any other normal filehandle, knowing that all the system-specific temporary file handling stuff is taken care of for you.