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

Solution: A book

Here is a possible solution to the task.

Code

class Book {
    has Str $.title;
    has Str $.author;
}

my $b = Book.new(title => 'Raku', author => 'Larry');
say "{$b.title} by {$b.author}";

🦋 You can find the source code in the file book.raku.

Output

Raku by Larry

Comments

  1. Each has $.title and has $.author declares an attribute together with a read accessor.

  2. Both accessors are used inside one double-quoted string. Each is wrapped in curly braces — {$b.title} and {$b.author} — which is the code-interpolation form: whatever is in the braces is run and its result is inserted, so the accessors are called and their values placed into the string.

  3. Both attributes are strings, so it would be wise to declare them so.

Course navigation

A book   |   A changeable label