在 Oct 12, 2006 5:43 AM 時,Tim Bunce 寫到:
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