How do you use temporary files and File::Temp with a short example?

Perl, File::Temp, Temporary Files
An example of using Perl's File::Temp module to create and manage temporary files.
use strict;
use warnings;
use File::Temp qw(tempfile);

# Create a temporary file
my ($fh, $filename) = tempfile();

# Write some content to the temporary file
print $fh "This is a temporary file.\n";
print $fh "You can use it to store temporary data.\n";

# Close the file handle
close($fh);

# Display the name of the temporary file
print "Temporary file created: $filename\n";

# Now you can read from the temporary file if needed
open(my $temp_fh, '<', $filename) or die "Could not open '$filename' $!\n";
while (my $line = <$temp_fh>) {
    print $line;
}
close($temp_fh);

# The temporary file will be deleted automatically when the program exits
    

Perl File::Temp Temporary Files