On 10/10/24 12:31, ToddAndMargo via perl6-users wrote:
Oh you know it would be helpful to show the results:
[...]
Specify a prefix and suffix for the filename:
my ($filename, $filehandle) = tempfile(:prefix('foo'), :suffix(".txt"));
Note that tempfile returns a tuple containing the filename and a
filehandle to the temporary file. You can use the filehandle to write
to the file, and the filename to access the file later.
Make sure to use the use File::Temp statement at the top of your Raku
program to enable the File::Temp module.
When I followed your link I got a slightly different answer:
Note that the |tempfile| subroutine returns a list containing the
filename and a filehandle. You can use the filehandle to write to the
file, and then close it when you’re done:
|$filehandle.write("Hello, world!"); # write to the file
$filehandle.close; # close the file|
Remember to unlink the file when you’re finished with it to avoid
leaving temporary files lying around:
|$filehandle.unlink; # unlink the file|
Alternatively, if you’re using Raku 2020 or later, you can use the
|IO::Path| class to create a temporary file:
|my $tmpfile = IO::Path.new(:temp);|
This will create a temporary file with a random name in the system’s
temporary directory. You can then use the |write| and |close| methods
to interact with the file.
Here you can see the Broken Clock Machine working as expected:
1) IO::Handle.write is for writing binary data with Buf, and trying to
pass a string will give you a type error
2) IO::Handle doesn't have an unlink method, only IO::Path does. You can
do $filehandle.path.unlink if you want.
3) There is no "Raku 2020", there is rakudo versions from 2020 of
course, but the versions of the Raku language have letters instead.
4) The :temp argument to IO::Path.new is as far as I know just not a
thing, and has never been. And even if it existed, you have to .open on
an IO::Path before you can .close or .write to it.