In doing a bit of work with traits (roles) in Perl 5
(http://perlmonks.org/?node_id=577477), I've realized some edge cases
which could be problematic.
First, when a role is applied to a class at runtime, a instance of that
class in another scope may specifically *not* want that role. Is there
a way of restricting a role to a particular lexical scope short of
merely applying that role to instances instead of classes?
Second, how can I remove roles from classes? I've create some code
which adds an "is_selected" method to some classes but when I'm done,
I'd like top easily remove that role. How do I do that? Seems closely
related to my first question, but it's still a distinct issue, I think.
Third, http://dev.perl.org/perl6/doc/design/syn/S12.html says:
You can also mixin a precomposed set of roles:
$fido does Sentry | Tricks | TailChasing | Scratch;
Should that be the following?
$fido does Sentry & Tricks & TailChasing & Scratch;
Cheers,
Ovid
--
Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/
I always thought when a role is applied to a class at runtime you
get a new (anonymous) subclass. The original class isn't affected.
Tim.
> On Tue, Oct 10, 2006 at 01:31:59PM -0700, Ovid wrote:
>> Hi all,
>>
>> In doing a bit of work with traits (roles) in Perl 5
>> (http://perlmonks.org/?node_id=577477), I've realized some edge cases
>> which could be problematic.
>>
>> First, when a role is applied to a class at runtime, a instance of
>> that
>> class in another scope may specifically *not* want that role.
>
> I always thought when a role is applied to a class at runtime you
> get a new (anonymous) subclass. The original class isn't affected.
Right, that's what usually happens with:
my Dog $fido .= new;
$fido does Catlike;
Here $fido's class become a new (memoized, anonymous) class that
still has
"Dog" as its name, but is no longer equivalent to the vanilla ^Dog
class object.
To forcefully add a role to a class at a distance during runtime, use
a class object call
(see Moose::Meta::Class for more about these APIs):
^Dog.add_role(^Catlike);
Thanks,
Audrey
> > I always thought when a role is applied to a class at runtime you
> > get a new (anonymous) subclass. The original class isn't affected.
>
> Right, that's what usually happens with:
>
> my Dog $fido .= new;
> $fido does Catlike;
Am I missing something here? That's applying a role to an instance of
a class, not the class itself, and thus *should* create a new anonymous
class.
> To forcefully add a role to a class at a distance during runtime, use
> a class object call (see Moose::Meta::Class for more about these
> APIs):
>
> ^Dog.add_role(^Catlike);
That's more of what I was thinking, but where is this documented? I
can't find it in http://dev.perl.org/perl6/doc/design/syn/S12.html. I
see it in the Moose docs, but is this in Perl 6? I thought it was.
> --To forcefully add a role to a class at a distance during runtime,
> use
>
>> a class object call (see Moose::Meta::Class for more about these
>> APIs):
>>
>> ^Dog.add_role(^Catlike);
>
> That's more of what I was thinking, but where is this documented? I
> can't find it in http://dev.perl.org/perl6/doc/design/syn/S12.html. I
> see it in the Moose docs, but is this in Perl 6? I thought it was.
Well, it's in Perl 6, and Moose is currently the metaobject layer
when the
VM that runs Perl 6 happens to be perl5...
Right after the 6.2.13 release (which is ready now, except for
changelogging),
we'll switch to the Moose team's MO system, ensuring that the
introspectio
APIs work exactly the same way on both GHC and perl5 virtual machines.
Thanks,
Audrey