Re: I am getting flooded with tmp files

[email protected] (ToddAndMargo via perl6-users)
Newsgroups perl.perl6.users
Message-ID <[email protected]>
On 11/18/24 21:04, Bruce Gray wrote:
> 
> 
>> On Nov 18, 2024, at 22:49, ToddAndMargo via perl6-users <perl6- 
>> [email protected]> wrote:
>>
>> On 11/18/24 20:06, ToddAndMargo via perl6-users wrote:
>>> I am getting flooded with tmp files.  I though
>>> I was erasing them (unlink).  What am I doing
>>> wrong:
>>> use File::Temp;
>>> ...
>>> my Str $TmpFile = $?FILE;
>>> $TmpFile ~~ s| .* $( Q[/] ) ||;  # we need just the name
>>> $TmpFile ~~ s| $( Q[.] ) .* ||;
>>> ($TmpFile, $TmpHandle) = tempfile(:tempdir("."), $TmpFile ~ "_******" 
>>> ~ ".tmp" );
>>> ...
>>> ($TmpFile, $TmpHandle) = tempfile(:tempdir("."), :!unlink);
>>
>>
>> It seems that all my tmp files are being removed
>> when my program exits normally.  It was crashing
>> until I fix a problem.
>>
>> But why wait till the program exits when I tell
>> it to unlink before that?
> 
> You are not "telling it to unlink before that".
>  From https://raku.land/zef:raku-community-modules/File::Temp <https:// 
> raku.land/zef:raku-community-modules/File::Temp> :
> # Automatically unlink files at end of program (this is the default)
> my ($filename, $filehandle) = tempfile("******", :unlink);
> ...
> # don't unlink this one
> my ($filename, $filehandle) = tempfile(:tempdir('.'), :!unlink);
> 
> The first call to `tempfile` in your code does not mention `:unlink`, so 
> the module defaults to marking the tempfile for deletion at the *end* of 
> the program.
> If you will want to delete the tempfile before the end of program, 
> create it with `:!unlink` so that it is not marked for deletion at all, 
> and you can add your own code to delete the file just after your code 
> closes the filehandle.
> 
> Note that unlike Perl, (due to differences in garbage collection), the 
> filehandle will not close itself as soon you leave the scope where it 
> was created. You will need to use `.close` in your own code.
> 
> -- 
> Hope this helps,
> Bruce Gray (Util of PerlMonks)


That explains it.  Thank you!
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.