Chapter 5. Object oriented programming

5.5. Roles

What are roles?

Perl roles are similar to Java interfaces or Ruby mixins. Roles add behavior and/or state to a class. They can have attributes and methods and can do other roles.

Example

A role:

package Searchable;
use Moo::Role;

# state
has 'number_of_results' => (is => 'ro', default => sub { 5 }); 

# behavior
sub search { ... }

A class that consumes Searchable role.

package Thing;
use Moo;
with qw/Searchable/;

Using the object:

my $thing = Thing->new();
$thing->search();

Terminology

Requires

The require keyword requires the class which consumes this role to implement a method. If the method does not exist an exception is thrown.

Example role:

package Searchable;
use Moo::Role;

requires 'search_engine';

sub search { 
    my ($self, $query) = @_;
    my $json = $self->search_engine->search($query);
    return JSON->new->utf8->decode($json);
}

Example class which consumes the role:

package Thing;
use Moo;
with qw/Searchable/;

sub search_engine {
    return Bing->new();
}

Zen of roles

Suggestions for naming roles

Roles vs Inheritance