package Mouse::Role;
-use strict;
-use warnings;
-use base 'Exporter';
+use Mouse::Exporter; # enables strict and warnings
-use Carp 'confess', 'croak';
-use Scalar::Util 'blessed';
+our $VERSION = '0.40_07';
-use Mouse::Meta::Role;
+use Carp qw(confess);
+use Scalar::Util qw(blessed);
-our @EXPORT = qw(before after around super override inner augment has extends with requires excludes confess blessed);
+use Mouse::Util qw(not_supported);
+use Mouse::Meta::Role;
+use Mouse ();
+
+Mouse::Exporter->setup_import_methods(
+ as_is => [qw(
+ extends with
+ has
+ before after around
+ override super
+ augment inner
+
+ requires excludes
+ ),
+ \&Scalar::Util::blessed,
+ \&Carp::confess,
+ ],
+);
+
+# XXX: for backward compatibility
+our @EXPORT = qw(
+ extends with
+ has
+ before after around
+ override super
+ augment inner
+
+ requires excludes
+
+ blessed confess
+);
sub before {
- my $meta = Mouse::Meta::Role->initialize(caller);
+ my $meta = Mouse::Meta::Role->initialize(scalar caller);
my $code = pop;
for (@_) {
}
sub after {
- my $meta = Mouse::Meta::Role->initialize(caller);
+ my $meta = Mouse::Meta::Role->initialize(scalar caller);
my $code = pop;
for (@_) {
}
sub around {
- my $meta = Mouse::Meta::Role->initialize(caller);
+ my $meta = Mouse::Meta::Role->initialize(scalar caller);
my $code = pop;
for (@_) {
sub super {
- return unless $Mouse::SUPER_BODY;
+ return if !defined $Mouse::SUPER_BODY;
$Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
}
sub override {
- my $classname = caller;
- my $meta = Mouse::Meta::Role->initialize($classname);
-
- my $name = shift;
- my $code = shift;
- my $fullname = "${classname}::${name}";
-
- defined &$fullname
- && confess "Cannot add an override of method '$fullname' " .
- "because there is a local version of '$fullname'";
-
- $meta->add_override_method_modifier($name => sub {
- local $Mouse::SUPER_PACKAGE = shift;
- local $Mouse::SUPER_BODY = shift;
- local @Mouse::SUPER_ARGS = @_;
-
- $code->(@_);
- });
+ # my($name, $code) = @_;
+ Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_);
}
# We keep the same errors messages as Moose::Role emits, here.
sub inner {
- croak "Moose::Role cannot support 'inner'";
+ Carp::croak "Roles cannot support 'inner'";
}
sub augment {
- croak "Moose::Role cannot support 'augment'";
+ Carp::croak "Roles cannot support 'augment'";
}
sub has {
- my $meta = Mouse::Meta::Role->initialize(caller);
-
+ my $meta = Mouse::Meta::Role->initialize(scalar caller);
my $name = shift;
- my %opts = @_;
- $meta->add_attribute($name => \%opts);
+ $meta->add_attribute($_ => @_) for ref($name) ? @{$name} : $name;
}
-sub extends { confess "Roles do not currently support 'extends'" }
+sub extends {
+ Carp::croak "Roles do not support 'extends'"
+}
sub with {
- my $meta = Mouse::Meta::Role->initialize(caller);
- my $role = shift;
- my $args = shift || {};
- confess "Mouse::Role only supports 'with' on individual roles at a time" if @_ || !ref $args;
-
- Mouse::load_class($role);
- $role->meta->apply($meta, %$args);
+ my $meta = Mouse::Meta::Role->initialize(scalar caller);
+ Mouse::Util::apply_all_roles($meta->name, @_);
}
sub requires {
- my $meta = Mouse::Meta::Role->initialize(caller);
- Carp::croak "Must specify at least one method" unless @_;
+ my $meta = Mouse::Meta::Role->initialize(scalar caller);
+ $meta->throw_error("Must specify at least one method") unless @_;
$meta->add_required_methods(@_);
}
-sub excludes { confess "Mouse::Role does not currently support 'excludes'" }
-
-sub import {
- my $class = shift;
-
- strict->import;
- warnings->import;
+sub excludes {
+ not_supported;
+}
- my $caller = caller;
+sub init_meta{
+ shift;
+ my %args = @_;
- # we should never export to main
- if ($caller eq 'main') {
- warn qq{$class does not export its sugar to the 'main' package.\n};
- return;
- }
+ my $class = $args{for_class}
+ or Carp::confess("Cannot call init_meta without specifying a for_class");
- my $meta = Mouse::Meta::Role->initialize(caller);
+ my $metaclass = $args{metaclass} || 'Mouse::Meta::Role';
- no strict 'refs';
- no warnings 'redefine';
- *{$caller.'::meta'} = sub { $meta };
+ my $meta = $metaclass->initialize($class);
- Mouse::Role->export_to_level(1, @_);
-}
+ $meta->add_method(meta => sub{
+ $metaclass->initialize(ref($_[0]) || $_[0]);
+ });
-sub unimport {
- my $caller = caller;
+ # make a role type for each Mouse role
+ Mouse::Util::TypeConstraints::role_type($class)
+ unless Mouse::Util::TypeConstraints::find_type_constraint($class);
- no strict 'refs';
- for my $keyword (@EXPORT) {
- delete ${ $caller . '::' }{$keyword};
- }
+ return $meta;
}
1;
=head1 NAME
-Mouse::Role - define a role in Mouse
+Mouse::Role - The Mouse Role
+
+=head1 VERSION
+
+This document describes Mouse version 0.40_07
+
+=head1 SYNOPSIS
+
+ package MyRole;
+ use Mouse::Role;
=head1 KEYWORDS
-=head2 meta -> Mouse::Meta::Role
+=head2 C<< meta -> Mouse::Meta::Role >>
Returns this role's metaclass instance.
-=head2 before (method|methods) => Code
+=head2 C<< before (method|methods) -> CodeRef >>
-Sets up a "before" method modifier. See L<Moose/before> or
+Sets up a B<before> method modifier. See L<Moose/before> or
L<Class::Method::Modifiers/before>.
-=head2 after (method|methods) => Code
+=head2 C<< after (method|methods) => CodeRef >>
-Sets up an "after" method modifier. See L<Moose/after> or
+Sets up an B<after> method modifier. See L<Moose/after> or
L<Class::Method::Modifiers/after>.
-=head2 around (method|methods) => Code
+=head2 C<< around (method|methods) => CodeRef >>
-Sets up an "around" method modifier. See L<Moose/around> or
+Sets up an B<around> method modifier. See L<Moose/around> or
L<Class::Method::Modifiers/around>.
-=over 4
-
-=item B<super>
+=head2 C<super>
-Sets up the "super" keyword. See L<Moose/super>.
+Sets up the B<super> keyword. See L<Moose/super>.
-=item B<override ($name, &sub)>
+=head2 C<< override method => CodeRef >>
-Sets up an "override" method modifier. See L<Moose/Role/override>.
+Sets up an B<override> method modifier. See L<Moose/Role/override>.
-=item B<inner>
+=head2 C<inner>
-This is not supported and emits an error. See L<Moose/Role>.
+This is not supported in roles and emits an error. See L<Moose/Role>.
-=item B<augment ($name, &sub)>
+=head2 C<< augment method => CodeRef >>
-This is not supported and emits an error. See L<Moose/Role>.
+This is not supported in roles and emits an error. See L<Moose/Role>.
-=back
-
-=head2 has (name|names) => parameters
+=head2 C<< has (name|names) => parameters >>
Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
this role. See L<Mouse/has>.
-=head2 confess error -> BOOM
+=head2 C<< confess(error) -> BOOM >>
L<Carp/confess> for your convenience.
-=head2 blessed value -> ClassName | undef
+=head2 C<< blessed(value) -> ClassName | undef >>
L<Scalar::Util/blessed> for your convenience.
=head2 unimport
-Please unimport Mouse (C<no Mouse::Role>) so that if someone calls one of the
+Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the
keywords (such as L</has>) it will break loudly instead breaking subtly.
+=head1 SEE ALSO
+
+L<Moose::Role>
+
=cut