Course of Raku / Objects, I/O, and exceptions / Input and output / File handles / Exercises / Write with a handle
Solution: Write with a handle
Here is a possible solution to the task.
Code
my $fh = open 'out.txt', :w;
$fh.say($_) for 1..3;
$fh.close;
print slurp 'out.txt';🦋 You can find the source code in the file write-with-a-handle.raku.
Output
1
2
3Comments
openwith the:wflag gives a handle for writing. The handle stays open across the loop, so each$fh.sayadds another line — this is the advantage of a handle overspurt, which would reopen the file every time.closeflushes the data to disk, so the followingslurpreads back all three lines.