> On Nov 18, 2024, at 22:49, ToddAndMargo via perl6-users 
> <perl6-users@perl.org> 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 :
        # 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)

Reply via email to