Course of Raku / Objects, I/O, and exceptions / Input and output / Working with files / Exercises / Build up a log
Solution: Build up a log
Here is a possible solution to the task.
Code
spurt 'log.txt', "start\n";
for 1..3 -> $i {
spurt 'log.txt', "entry $i\n", :append;
}
print slurp 'log.txt';🦋 You can find the source code in the file append-a-line.raku.
Output
start
entry 1
entry 2
entry 3Comments
The first
spurtcreates the file fresh with the linestart.Each
spurtinside the loop uses:append, so it adds its line after the existing content rather than replacing the file. This is exactly how a log file grows: every iteration (or every run of a program) tacks one more line onto the end.Without
:append, each pass would overwrite the file, and only the last line would survive.