sub import {
my $target = caller;
- my $me = $_[0];
+ my $me = shift;
strictures->import;
return if $INFO{$target}; # already exported into this package
# get symbol table reference
}
1;
+__END__
=head1 NAME
package Some::Class;
- require Role::Tiny;
+ use Role::Tiny::With;
# bar gets imported, but not foo
- Role::Tiny->apply_role_to_package('Some::Role', __PACKAGE__);
+ with 'Some::Role';
sub foo { ... }
Role::Tiny->apply_role_to_package('Some::Package', 'Some::Role');
-Composes role with package
+Composes role with package. See also L<Role::Tiny::With>.
=head2 apply_roles_to_object
--- /dev/null
+package Role::Tiny;
+
+use strict;
+use warnings FATAL => 'all';
+
+use Exporter 'import';
+our @EXPORT = qw( with );
+
+sub with {
+ my $target = caller;
+ Role::Tiny->apply_role_to_package($target, @_)
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Role::Tiny::With - Neat interface for consumers of Role::Tiny roles
+
+=head1 SYNOPSIS
+
+ package Some::Class;
+
+ use Role::Tiny::With;
+
+ with 'Some::Role';
+
+ # The role is now mixed in
+
+=head1 DESCRIPTION
+
+C<Role::Tiny> is a minimalist role composition tool. C<Role::Tiny::With>
+provides a C<with> function to compose such roles.
+
+=head1 AUTHORS
+
+See L<Moo> for authors.
+
+=head1 COPYRIGHT AND LICENSE
+
+See L<Moo> for the copyright and license.
+
+=cut
+
+
--- /dev/null
+use strictures 1;
+use Test::More;
+use Test::Fatal;
+
+BEGIN {
+ package MyRole;
+
+ use Role::Tiny;
+
+ sub bar { 'role bar' }
+
+ sub baz { 'role baz' }
+}
+
+BEGIN {
+ package MyClass;
+
+ use Role::Tiny::With;
+
+ with 'MyRole';
+
+ sub foo { 'class foo' }
+
+ sub baz { 'class baz' }
+
+}
+
+is(MyClass->foo, 'class foo', 'method from class no override');
+is(MyClass->bar, 'role bar', 'method from role');
+is(MyClass->baz, 'class baz', 'method from class');
+
+done_testing;