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 3

Comments

  1. The first spurt creates the file fresh with the line start.

  2. Each spurt inside 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.

  3. Without :append, each pass would overwrite the file, and only the last line would survive.

Course navigation

Build up a log   |   Count the characters