#!/usr/bin/perl use Parse::RecDescent; use POE; use POE::Component::Client::TCP; use POE::Component::Server::TCP; use POE::Filter::Line; use Socket; use strict; use vars qw/ %base %man @try_again /; %man = ( 'first' => "Who", 'second' => "What", 'third' => "I Don't Know" ); %base = map { lc } reverse %man; @try_again = ( "So, who's on first?", "I want to know who's on first!", "What's the name of the first baseman?", "Let's start again. What's the name of the guy on first?", "Okay, then, who's on second?", "Well then, who's on third?", "What's the name of the fellow on third?" ); sub Parse::RecDescent::choose { $_[1 + int rand $#_]; } my $hostname = gethostbyname( $ARGV[0] || '' ); if ( defined $hostname ) { my $costello = Parse::RecDescent->new( q{ Interpretation: Meaning <reject: $item[1] eq $thisparser->{'prev'} > { $thisparser->{'prev'} = $item[1] } | { choose( @::try_again ) } Meaning: Question | UnclearReferent | NonSequitur Question: Preface Interrogative /[i']s on/ Base { choose( "Yes, what is the name of the guy on $item[2]? +", "The $item[4] baseman?", "I'm asking you! $item[2]", "I don't know" ); } | Interrogative { choose( "That's right, $item[1]?", "What?", "I don't know!" ); } UnclearReferent: "He's on" Base { choose( "Who's on $item[2]?", "Who is?", "So, what is the name of the guy on $item[2]?" ); } NonSequitur: ( "Yes" | "Certainly" | /that's correct/i ) { choose( "$item[1], who?", "What?", @::try_again ); } Interrogative: /who/i | /what/i Base: 'first' | 'second' | 'third' Preface: ...!Interrogative /\S*/ }); POE::Component::Client::TCP->new( 'RemoteAddress' => inet_ntoa( $hostname ), 'RemotePort' => 12477, 'Filter' => 'POE::Filter::Line', 'Connected' => sub { print STDOUT "<costello> Who's on first?\n"; $_[ HEAP ]->{'server'}->put( "Who's on first?" ); }, 'ServerInput' => sub { my ( $heap, $line ) = @_[ HEAP, ARG0 ]; my $reply = $costello->Interpretation( $line ); print STDOUT "<abbott> ", $line, "\n"; sleep 1; print STDOUT "<costello> ", $reply, "\n"; $heap->{'server'}->put( $reply ); } ); } else { my $abbott = Parse::RecDescent->new( q{ Interpretation: ConfirmationRequest | NameRequest | BaseRequest ConfirmationRequest: Preface(s?) Name /[i']s on/ Base { ( lc $::man{ $item[4] } eq lc $item[2] ) ? "Yes" : "No, $::man{ $item[4] }\'s on $item[4]" } | Preface(s?) Name /[i']s the (name of the)?/ Man /('s n +ame )?on/ Base { ( lc $::man{ $item[6] } eq lc $item[2] ) ? "Certainly" : "No. \u$item[2] is on " . $::base{ lc $ite +m[2] } } BaseRequest: Preface(s?) Name /(is)?/ { "He's on " . $::base{ lc $item[2] } } NameRequest: /(What's the name of)?the/i Base "baseman" { $::man{ $item[2] } } Preface: ...!Name /\S*/ Name: /who/i | /what/i | /i don't know/i Base: 'first' | 'second' | 'third' Man: 'man' | 'guy' | 'fellow' }); POE::Component::Server::TCP->new( 'Alias' => 'server', 'Port' => 12477, 'ClientInput' => sub { my ( $heap, $line ) = @_[ HEAP, ARG0 ]; my $reply = $abbott->Interpretation( $line ); print STDOUT "<costello> ", $line, "\n"; sleep 1; print STDOUT "<abbott> ", $reply, "\n"; $heap->{'client'}->put( $reply ); } ); } $poe_kernel->run; exit 0;

In reply to Wasting time with Parse::RecDescent and POE by rob_au

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.