X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FRole.pm;h=eb2ec97d40306d83d90c77ecf280d38f848b1c13;hp=1f897605753516f7f17e48062d2c9d87aaae2233;hb=HEAD;hpb=825f7cdadcd71fb73aa7f6fa7c29b4f2d0c25366 diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index 1f89760..eb2ec97 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -1,13 +1,11 @@ package Mouse::Role; use Mouse::Exporter; # enables strict and warnings -our $VERSION = '0.74'; +our $VERSION = '0.95'; -use Carp qw(confess); -use Scalar::Util qw(blessed); +use Carp (); +use Scalar::Util (); -use Mouse::Util qw(not_supported); -use Mouse::Meta::Role; use Mouse (); Mouse::Exporter->setup_import_methods( @@ -104,7 +102,7 @@ sub requires { } sub excludes { - not_supported; + Mouse::Util::not_supported(); } sub init_meta{ @@ -139,74 +137,110 @@ Mouse::Role - The Mouse Role =head1 VERSION -This document describes Mouse version 0.74 +This document describes Mouse version 0.95 =head1 SYNOPSIS - package MyRole; - use Mouse::Role; + package Comparable; + use Mouse::Role; # the package is now a Mouse role -=head1 KEYWORDS + # Declare methods that are required by this role + requires qw(compare); -=head2 C<< meta -> Mouse::Meta::Role >> + # Define methods this role provides + sub equals { + my($self, $other) = @_; + return $self->compare($other) == 0; + } -Returns this role's metaclass instance. + # and later + package MyObject; + use Mouse; + with qw(Comparable); # Now MyObject can equals() -=head2 C<< before (method|methods|regexp) -> CodeRef >> + sub compare { + # ... + } -Sets up a B method modifier. See L. + my $foo = MyObject->new(); + my $bar = MyObject->new(); + $obj->equals($bar); # yes, it is comparable -=head2 C<< after (method|methods|regexp) => CodeRef >> +=head1 DESCRIPTION -Sets up an B method modifier. See L. +This module declares the caller class to be a Mouse role. -=head2 C<< around (method|methods|regexp) => CodeRef >> +The concept of roles is documented in L. +This document serves as API documentation. -Sets up an B method modifier. See L. +=head1 EXPORTED FUNCTIONS -=head2 C +Mouse::Role supports all of the functions that Mouse exports, but +differs slightly in how some items are handled (see L below +for details). -Sets up the B keyword. See L. +Mouse::Role also offers two role-specific keywords: -=head2 C<< override method => CodeRef >> +=head2 C<< requires(@method_names) >> -Sets up an B method modifier. See L. +Roles can require that certain methods are implemented by any class which +C the role. -=head2 C +Note that attribute accessors also count as methods for the purposes of +satisfying the requirements of a role. -This is not supported in roles and emits an error. See L. +=head2 C<< excludes(@role_names) >> -=head2 C<< augment method => CodeRef >> +This is exported but not implemented in Mouse. -This is not supported in roles and emits an error. See L. +=head1 IMPORT AND UNIMPORT -=head2 C<< has (name|names) => parameters >> +=head2 import -Sets up an attribute (or if passed an arrayref of names, multiple attributes) to -this role. See L. +Importing Mouse::Role will give you sugar. C<-traits> are also supported. -=head2 C<< confess(error) -> BOOM >> +=head2 unimport -L for your convenience. +Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the +keywords (such as L) it will break loudly instead breaking subtly. -=head2 C<< blessed(value) -> ClassName | undef >> +=head1 CAVEATS -L for your convenience. +Role support has only a few caveats: -=head1 MISC +=over -=head2 import +=item * -Importing Mouse::Role will give you sugar. +Roles cannot use the C keyword; it will throw an exception for now. +The same is true of the C and C keywords (not sure those +really make sense for roles). All other Mouse keywords will be I +so that they can be applied to the consuming class. -=head2 unimport +=item * -Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the -keywords (such as L) it will break loudly instead breaking subtly. +Role composition does its best to B be order-sensitive when it comes to +conflict resolution and requirements detection. However, it is order-sensitive +when it comes to method modifiers. All before/around/after modifiers are +included whenever a role is composed into a class, and then applied in the order +in which the roles are used. This also means that there is no conflict for +before/around/after modifiers. + +In most cases, this will be a non-issue; however, it is something to keep in +mind when using method modifiers in a role. You should never assume any +ordering. + +=back =head1 SEE ALSO +L + L +L + +L + =cut