On Fri, Jan 28, 2011 at 10:25 PM, Chris Prather <[email protected]> wrote:
> On Fri, Jan 28, 2011 at 4:13 PM, Stevan Little
> <[email protected]> wrote:
>> On Jan 28, 2011, at 4:06 PM, Zbigniew Lukasiak wrote:
>>>
>>> On Fri, Jan 28, 2011 at 9:35 PM, Karen Etheridge <[email protected]> wrote:
>>>>
>>>> On Fri, Jan 28, 2011 at 09:29:04PM +0100, Zbigniew Lukasiak wrote:
>>>>>
>>>>> Are there any sane options for building Moose::Roles out of Non Moose
>>>>> classes?
>>>>>
>>>>> Or maybe it is trivial?
>>>>
>>>> The thing about roles is they aren't instantiated, so you can't really
>>>> do:
>>>>
>>>> package MyApp::Role::Foo;
>>>>
>>>> use base 'Some::Other::Class';
>>>> use Moose::Role;
>>>>
>>>> ...because there is no way to call up the inheritance tree to get at
>>>> methods in Some::Other::Class. However, if you imported functions from
>>>> that class into your role, you can make them available in your role just
>>>> the same as if you had implemented them into your role itself.
>>>>
>>>> Perhaps can you describe an example of what you want to do?
>>>
>>> Well - that's what I was thinking, but:
>>>
>>> use strict;
>>> use warnings;
>>>
>>> {
>>> package Aa;
>>> use Exporter 'import';
>>> our @EXPORT_OK = qw(foo);
>>>
>>> sub foo{
>>> print "foooooooooo\n";
>>> }
>>> }
>>> {
>>> package Bb;
>>> use Moose::Role;
>>> Aa->import( 'foo' );
>>>
>>> sub baz {
>>> foo(shift);
>>> }
>>>
>>> sub bar {
>>> print "baaaaaaaaaaar\n";
>>> }
>>> }
>>> {
>>> package Cc;
>>> use Moose;
>>> with 'Bb';
>>> }
>>>
>>> my $c = Cc->new();
>>> $c->bar;
>>> $c->baz;
>>> $c->foo;
>>>
>>>
>>> prints:
>>>
>>> baaaaaaaaaaar
>>> foooooooooo
>>> Can't locate object method "foo" via package "Cc" at a.pl line 37.
>>>
>>> So there is something missing, foo is not recognized as a method.
>>
>> No, because 'foo' is not a method, it is an imported function from another
>> package. Class::MOP checks the STASH of the CV to find out if it is a native
>> of that package and if not, it assumes it is an imported function and
>> therefore ignores it.
>>
>> It is a feature, not a bug :)
>>
>> - Stevan
>
> Additionally doing this is *really* nasty action-at-a-distance. Trying
> to explain what exactly is going on to someone who may or may not
> grasp the wonderful nuances that are Perl's package stashes and
> importation rules is not nearly as easy as explaining Attributes and
> Delegation. Think of the sanity of your downstream maintenance
> programmers.
Yeah - OK - I was not proposing that this should work, that was just
an example explaining the difficulties.
Anyway - let's go back to the core:
{
package Aa;
sub foo{ print "foooooooooo\n"; }
}
{
package Bb;
use Moose::Role;
...
}
{
package Cc;
use Moose;
with 'Bb';
}
my $c = Cc->new();
$c->foo;
What should go into the '...' there to make it work? Would it be
possible to make it also working if Aa inherited foo from some other
superclass?
I am testing here if it could be made easy to use a library with
multiple inheritance in Moose. Assuming that the additional base
classes already work in a way similar to Moose Roles.
--
Z