roles - A simple pragma for composing roles.
version 0.01
package Eq { use strict; use warnings; sub equal_to; sub not_equal_to { my ($self, $other) = @_; not $self->equal_to($other); } } package Comparable { use strict; use warnings; use roles 'Eq'; sub compare; sub equal_to { my ($self, $other) = @_; $self->compare($other) == 0; } sub greater_than { my ($self, $other) = @_; $self->compare($other) == 1; } sub less_than { my ($self, $other) = @_; $self->compare($other) == -1; } sub greater_than_or_equal_to { my ($self, $other) = @_; $self->greater_than($other) || $self->equal_to($other); } sub less_than_or_equal_to { my ($self, $other) = @_; $self->less_than($other) || $self->equal_to($other); } } package Printable { use strict; use warnings; sub to_string; } package US::Currency { use strict; use warnings; use roles 'Comparable', 'Printable'; sub new { my ($class, %args) = @_; bless { amount => $args{amount} // 0 } => $class; } sub compare { my ($self, $other) = @_; $self->{amount} <=> $other->{amount}; } sub to_string { my ($self) = @_; sprintf '$%0.2f USD' => $self->{amount}; } }
This is a very simple pragma which takes a list of roles as package names, adds them to the @DOES package variable and then schedule for composition to occur during UNITCHECK.
@DOES
Stevan Little <stevan@cpan.org>
This software is copyright (c) 2017 by Stevan Little.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install roles, copy and paste the appropriate command in to your terminal.
cpanm
cpanm roles
CPAN shell
perl -MCPAN -e shell install roles
For more information on module installation, please visit the detailed CPAN module installation guide.