develooper Front page | perl.perl6.language | Postings from May 2005

Roles and BUILD

Thread Next
From:
Ingo Blechschmidt
Date:
May 16, 2005 08:12
Subject:
Roles and BUILD
Message ID:
loom.20050516T170803-176@post.gmane.org
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!                    |  
 


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About