Course of Raku / Objects, I/O, and exceptions / Classes and objects / Attributes / Exercises / A changeable label

Solution: A changeable label

Here is a possible solution to the task.

Code

class Label {
    has $.text is rw;
}

my $l = Label.new(text => 'draft');
$l.text ~= ' (revised)';
say $l.text;

🦋 You can find the source code in the file changeable-label.raku.

Output

draft (revised)

Comments

  1. The is rw trait makes the accessor return a writable container, so it can appear on the left of an assignment.

  2. Because it is writable, the compound operator ~= works on it too: $l.text ~= ' (revised)' reads the current text, concatenates the suffix, and stores the result back — all through the same accessor.

Course navigation

A changeable label   |   Methods