package TalkBot; # Package for CCR bots that communicate by talking. # New variables: # textFunctions mapping of strings to functions to call # $verb what the last read verb was # %args parsed args of that verb. # New functions: # initRobot overrides CCRRobot, also says hello to everyone # doInput reads a line, parses it, if it's a say type of # thing then run it through textFunctions # emit spit text out to the world use CCRBot; use ApplyTable; @ISA = qw(CCRBot); @EXPORT = (@CCRBot::EXPORT, @ApplyTable::Export, qw($textFunctions $verb %args initRobot doInput emit)); $textFunctions = ''; # set this to reference to table $verb = ''; # set by doInput() %args = ''; # set by doInput() # override CCRBot initRobot function - call it, then do our own thing sub initRobot { &CCRBot::initRobot(); emit("$ccrName (aka $name) has woken."); } # Read a line of input. If it's past tense text not spoken by us, then # try to handle it via applytable. Your handler is called with # ($self, $verb, %args) as arguments. This nicely matches Perl OO convention # Returns: # undef if EOF # /Handled if we handled the verb # $verb otherwise. sub doInput { my $rc; if (eof(STDIN)) { return undef; } else { ($verb, %args) = parseccrl(readLine()); if ($args{'Tense'} eq '::Past' && $verb =~ m!/(Say|Answer|Ask|Exclaim|Shout|Whisper)! && $args{'Subject'} ne $ccrName) { $rc = applytable($args{'Text'}, $textFunctions, $verb, %args); } if ($rc) { return "/Handled"; } else { return $verb; } } } # Speak out a line of text. sub emit { ccrl("$_[0]"); } 1;