Sanchita Ghai
Sanchita Ghai

Reputation: 63

Automatically delete temporary files in child process in perl

I am forking in a perl script and exiting from parent process after fork(). In child process, then I am creating temporary files by this method in a function

my ( $S_TEMP_FILE,  $stmpfile )  = tempfile( UNLINK => 1, DIR => $TMPDIR );

Now the issue is that this temporary file is not getting deleted automatically when I am exiting from function. I have created some temporary files before forking too, but they are getting automatically deleted on exit of functions.I have gone through all the links regarding this but i am not able to figure out the reason for this behaviour.

Upvotes: 1

Views: 541

Answers (1)

ikegami
ikegami

Reputation: 385976

Files created by tempfile are "automatically removed when the program exits", not when some subroutine exits.

Note that if the program calls _exit or if it's killed by a signal, the program won't get a chance to delete these files.

If you want the file to be deleted sooner, you'll need to delete it yourself or use File::Temp->new() instead of tempfile. (Files created by File::Temp->new() are be deleted when the object is destroyed.)

Upvotes: 4

Related Questions