Perl Roles

1. Roles Definition

A role is a set of methods that provide extra behavior to a class. Roles can’t be used independently they need a class to consume them. Roles are a good alternative to inheritance.

2. A sample role

A role is declared in a *.pm f…


This content originally appeared on DEV Community and was authored by DragosTrif

1. Roles Definition

A role is a set of methods that provide extra behavior to a class. Roles can't be used independently they need a class to consume them. Roles are a good alternative to inheritance.

2. A sample role

A role is declared in a *.pm file.

package Role::JSON;
use Moose::Role;

use JSON 'encode_json';

requires qw( data );

sub to_json {
  my $self = shift;

  return encode_json( $self->data() );
}

1;

3. Consuming a role

In the previous example the Role::JSON requires
the consuming class to have a method named data().

package Foo;
use Moose;
with "Role::JSON";

sub data {
 my $self = shift;
 return { foo => 'bar' }; 
}

__PACKAGE__->meta->make_immutable;

1;

Then you could would just call the method defined in the role in your program:


my $obj = Foo->new();

print $obj->to_json();

# And that prints the following JSON
# { foo : 'bar' }

4. Checking if class consumes a role

Because roles are not inherited you cannot use isa() to check if a class consumes a role instead you should use does():

$object->does("Role::JSON");

5. Roles without Moose/Moo

Cpan module Role::Tiny allows you to use roles with vanilla OOP not just with Moose or Moo. Like Moose or Moo Role::Tiny applies strict and warnings to the caller.

package Role::Foo;
use Role::Tiny;
sub data { 
 my $self = shift; 
 return { foo => 'bar' };
}
1;

package Bar;
use lib 'lib';
use Role::Tiny::With;
with  'Role::Foo';
....
1;

Role::Tiny makes available to your role the following method modifiers: before, around and after.
In this example when $self->data is called the around block executes and you get JSON returned.

package Role::JSON;
use Role::Tiny;
use JSON 'encode_json';

requires qw( data );

around data => sub  {
  my $orig = shift;
  my $self = shift;

  return encode_json( $self->$orig() );
};  

6. Compositional safety

Roles attempt to guarantee compositional safety. So if two roles have the same method defined and you try to consume them in the same class you will get an error message.

 Due to method name conflicts in roles ....

Just remember that in order to trigger the error message you need to consume all the roles at once:

# good
package Foo;
use lib 'lib';
use Role::Tiny::With;
with  'Role::XML', 'Role::JSON';

# bad second method is ignored
package Foo;
use lib 'lib';
use Role::Tiny::With;
with  'Role::XML'; 
with  'Role::JSON';

7. How to fix method collision

  • implement the methods yourself in your class, thus causing the corresponding role methods to be ignored

  • For Moose use the excludes key word

package Role::JSON;
use Moose::Role;

sub serialize { ... }

package Role::XML;
use Moose::Role;

sub serialize { ... }

package Foo;
use Moose;
with Role::Serializable::JSON,
     Role::Serializable::XML => { excludes => 'serialize' };
  • For Role::Tiny use namespace::clean
package Role::XML;

use Role::Tiny;

sub serialize { my $self = shift; print 'test'  };
# serialize() will not be imported in the consuming class
use namespace::clean;

1;

8. Bibliography


This content originally appeared on DEV Community and was authored by DragosTrif


Print Share Comment Cite Upload Translate Updates
APA

DragosTrif | Sciencx (2022-01-14T15:34:14+00:00) Perl Roles. Retrieved from https://www.scien.cx/2022/01/14/perl-roles/

MLA
" » Perl Roles." DragosTrif | Sciencx - Friday January 14, 2022, https://www.scien.cx/2022/01/14/perl-roles/
HARVARD
DragosTrif | Sciencx Friday January 14, 2022 » Perl Roles., viewed ,<https://www.scien.cx/2022/01/14/perl-roles/>
VANCOUVER
DragosTrif | Sciencx - » Perl Roles. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/14/perl-roles/
CHICAGO
" » Perl Roles." DragosTrif | Sciencx - Accessed . https://www.scien.cx/2022/01/14/perl-roles/
IEEE
" » Perl Roles." DragosTrif | Sciencx [Online]. Available: https://www.scien.cx/2022/01/14/perl-roles/. [Accessed: ]
rf:citation
» Perl Roles | DragosTrif | Sciencx | https://www.scien.cx/2022/01/14/perl-roles/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.