Hi,
are Roles allowed to contain submethods and does especially the BUILD
submethod
work as I presume in the following code?
class IRC::Bot {
has Array %:handler;
method add_handler(Str $event, Code $callback) {
push %:handler{$event}: $callback;
}
...;
}
role IRC::Bot::JoinOnInvite {
submethod BUILD() {
./add_handler("INVITE", -> Str $channel {
./join($channel);
});
}
}
role IRC::Bot::SayHelloOnJoin {
submethod BUILD() {
./add_handler("JOIN", -> Str $nick {
./msg($nick, "Hi $nick!");
});
}
}
class MyBot {
is IRC::Bot;
does IRC::Bot::JoinOnInvite;
does IRC::Bot::SayHelloOnJoin;
}
my MyBot $bot .= new;
# Are IRC::Bot::JoinOnInvite::BUILD and
# IRC::Bot::SayHelloOnJoin::BUILD called?
And:
my IRC::Bot $bot .= new;
# Up to now, no BUILDs of any role is called.
$bot does IRC::Bot::SayHelloOnJoin;
# Is IRC::Bot::SayHelloOnJoin's BUILD called now?
--Ingo
--
Linux, the choice of a GNU | To understand recursion, you must first
generation on a dual AMD | understand recursion.
Athlon! |