I'm including an entire sample program that looks like a great deal
more than it is... It's just a simple shell that supports three
commands: /quit, /go and /np.
If you run this and issue a /np; possibly even a /np 200, you'll see
50 or 200 lines of randomly generated text. Boring.
However, if you were to run /go 200 you would see the Wheel crash
horribly. It will still technically function, but in a miserable sort
of way -- and even though it still sortof works, the output will be
truncated. I have spent a few hours trying to figure out why, but I
haven't put my finger on it.
In any case, it seems to me that a POE Wheel should survive put()
events while readline is already get()ing....
-Paul
use strict;
use POE qw(Wheel::ReadLine);
my $rls = POE::Session->create( package_states => [
main=>[qw(_start input spam)]
]);
sub garbage {
my $garbage = "";
$garbage .= chr( (ord ' ') + (int rand 36) ) for 1 .. 80;
return $garbage;
}
sub input {
my ($heap, $kern, $input, $exception) = @_[HEAP, KERNEL, ARG0,
ARG1];
my $rl = $heap->{wheel};
if( defined $input ) {
$rl->addhistory($input);
if( $input =~ m/^\s*\/?(?:quit|exit)/ ) {
exit;
} elsif( $input =~ m/^\s*\/(?:go|spam)\s*(\d*)/ ) {
$kern->delay( spam => 2, ($1||50) )
} elsif( $input =~ m/^\s*\/(?:np|npspam)\s*(\d*)/ ) {
$rl->put("** np spam: " . &garbage) for 1 .. ($1||50);
} else {
$rl->put("** heard: $input");
}
} else {
$rl->put("** unknown exception: $exception");
}
$rl->get("test> ");
}
sub _start {
my $heap = $_[HEAP];
$heap->{wheel} = new POE::Wheel::ReadLine( InputEvent =>
'input' );
$heap->{wheel}->get("test> ");
}
sub _stop {
delete $_[HEAP]->{wheel};
}
sub spam {
my $rl = $_[HEAP]->{wheel};
my $n = $_[ARG0];
$rl->put("** spam ($_/$n): " . &garbage) for 1 .. $n;
}
run POE::Kernel;
exit;