Benh,
An imported sub is not a method, Roles only deal with methods.
So in the first test the looks_like_number is only available as a
function in the role package itself and not in My::Class since the
role composition sees that it is imported from Scalar::Util and not
defined in the role itself.
In the second one, the import method is created by Exporter and
therefore *should* be composed into My::Class2 however I suspect that
&import would, if you were to introspect it have a package of
"Exporter" and a name of "__ANON__" and so Moose role composition
would ignore it. Also, this wouldn't work as expected anyway because
with 'My::Role' does not call ->import it only loads the role package
with require.
In the 3rd example with extends, you are not inheriting the exports
so you cannot call it as looks_like_number and have Perl's sub
dispatcher find it. However you could call it as a method $self-
>look_like_number and have Perls method dispatcher find it. However
this is not very useful since $self will never be a number and will
be the first arg passed to look_like_number.
In the 4th example, your My::Ext2 class will actually inherit the
import method, so if you were to do:
package Foo;
use My::Ext2;
Then looks_like_number would actually get imported into Foo. However,
as with roles, when you do extends 'My::Base' it will not call
My::Base->import and so never import the looks_like_number function.
As I mentioned on IRC, apparently Spiffy used to do this, and Ingy
mentioned at YAPC that he would like to write a MooseX:: module to do
the same. It would be fairly simple and would take some work on your
part to determine what is imported and what is not, but all the tools
are there for you (Class::MOP::Package symbol table introspection
will help a lot, along with the Class::MOP::get_code_info util sub).
- Stevan
On Jun 23, 2008, at 7:37 PM, benh wrote:
So I'm sure that this is due to my lack of understating on how Objects
are built behind the fur but it seems that if I use something in a
role that I should use it in the caller, same with a base class with a
sub class? Have I completely missed something obvious?
I've written up a test to illustrate the point. Sadly all the tests
pass (throws_ok) ?
http://develonizer.com/svn/misc/role_use.t
--
benh~