September 2017
Beginner
402 pages
9h 52m
English
The but operator mixes in a role to an object, similar to how the does operator is doing that. The but operator does not modify an object itself and always returns a new object. In addition, but allows us to use already instantiated objects, as shown in the following example:
my $value = 0 but True;say 'It is true' if $value;
The $value becomes True in the Boolean context now, while it still contains pure zero.
In the case of mixing in roles, the same object starts behaving as an object belonging to different types, depending on the situation. Consider the following code snippet:
role Barking { method bark() { say "Bow-wow!"; }}my $dog = 14 but Barking;say $dog; # 14$dog.bark(); # Bow-wow!
The $dog variable is printed as ...