I looked at the source code. I even checked the latest version (0.22 at the time of writing). What I want to do with the :: Temp file is not allowed by design.
When an object is created, a temporary file is created using the context of the array as:
sub new { ...
Creating a temporary file in the context of an array implies that the file will never be automatically disconnected (even on a platform that supports it). The file is marked for delayed unlock, which means that it will be deleted when the program exits normally.
sub tempfile { ... _deferred_unlink($fh, $path, 0) if $options{"UNLINK"};
If I want to continue using File :: Temp, I need to set up a signal handler to force it to clear when my program is interrupted. I should also set up a sig handler if portability is any concern, because deferred unlink is all that is offered on some platforms. The best part is that File :: Temp creates an END block, so there is nothing to do:
use sigtrap qw(die normal-signals error-signals);
If portability is not a problem and the platform supports automatic file cleanup, then using tempfile is fine (in a scalar context).
The rationale for this design is explained by the following comment:
we must provide temporary information when opening the file. In general, we only need a true temporary file, if we return only a filehandle - if the user needs a file name, he probably does not want the file to disappear as soon as they close it (which may be important if they want the child process to use file)
source share