I've been digging into Perl 6 more lately and I noticed the Wikipedia example of fluent interfaces didn't have a Perl 6 example, so I fixed that.

To be fair, Martin Fowler's explanation (as usual) of fluent interfaces does a much better job of explaining them, but a key point is that setters have a return value. For many fluent interfaces, the setters set a value and actually return a new instance of a different object for you to call methods on. Thus, the examples in Wikipedia don't always meet the criteria of a fluent interface, but I added a Perl 6 version that closely modeled the PHP version (but more concisely, and with much better type safety). I sidestepped the entire fluent interface debate.

Here's my example:

class Employee { subset Salary of Real where * > 0; subset NonEmptyString of Str where * ~~ /\S/; has NonEmptyString $.name is rw; has NonEmptyString $.surname is rw; has Salary $.salary is rw; method gist { return qq:to[END]; Name: {$.name} Surname: {$.surname} Salary: {$.salary} END } } my $employee = Employee.new(); given $employee { .name = 'Sally'; .surname = 'Ride'; .salary = 200; } say $employee;

And that prints:

Output: Name: Sally Surname: Ride Salary: 200

The lovely folks on the #perl6 IRC channel helped me fix a few misunderstandings (they're really awesome), and once again, I find Perl 6 is just a joy to work with.