Levan has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have a question regarding how to delete file that was creating when using a file handle.

When I was using my script, I have used quite a lot of file handles. And the problem is that now I have quite a lot of intermediate file which I want to get rid of once and for all without having to change the code.

So is there any commands that I can use to help me delete the file that the file handles created.

Thanks
Levan

Replies are listed 'Best First'.
•Re: Deleting files
by merlyn (Sage) on Oct 23, 2003 at 06:38 UTC
Re: Deleting files
by Roger (Parson) on Oct 23, 2003 at 06:34 UTC
    I think the question you are asking is whether you can get the name of the file from its file handle.

    The answer is no.

    Unfortunately you have to keep track of the names of your temporary files. You can store them in a list, perhaps, when they are first created with:
    push @tempfiles, $tmpfilename;
    and then remove them at the end with:
    unlink @tempfiles;
    Another method is to put your temporary files under a temporary folder created by your script, and simply prune the temporary folder at the end of processing.

    Update: And yes, you can rewrite your script using File::Temp as pointed out by merlyn.