<update2> Please disregard the following code example, I was pointed to Mojo::IOLoop::Stream::Role::LineBuffer which makes the implementation much nicer - many thanks to the fine people on #mojo on freenode! See this example instead. </update2>
<update> Note that this uses the slightly lower-level API instead of the higher-level one shown at the top of Mojo::IOLoop. However, I don't yet see a easy way to use one of the existing readline modules with that interface (Mojo::IOLoop::LineReader or MojoX::LineStream, the latter has a bug and doesn't support changing the input record separator). I may have another update in a little while, we'll see. </update>
POE.. it seems awfully complicated for something as simple as what I need {basically *nothing* more than an unblocking read} Is there a less complicated package that provides the simple functionality I need?
Yes, I agree POE has an "interesting" interface, although once you get into it, it works well. As I mentioned, Mojolicious includes an event loop whose interface I find nicer. There's also Mojo::IOLoop::LineReader that handles the buffering and splitting of read events into lines:
mojo_serv.pl:
use warnings;
use strict;
use Data::Dump qw/dd pp/;
use Mojo::IOLoop::Server;
use Mojo::IOLoop::LineReader;
my $server = Mojo::IOLoop::Server->new;
$server->on(accept => sub {
my ($serv, $handle) = @_;
my $peer = $handle->peerhost.":".$handle->peerport;
print $peer,": Connect\n";
my $stream = Mojo::IOLoop::LineReader->new($handle);
$stream->timeout(30);
$stream->on(readln => sub {
my ($strm, $line) = @_;
print $peer,": Got line ",pp($line),"\n";
});
$stream->on(close => sub {
print $peer,": Closed\n";
$stream->stop;
$stream = undef; # free reference
});
$stream->on(error => sub {
my ($strm, $err) = @_;
warn "$peer: Error: $err";
});
$stream->start;
$stream->write("Hello, client from $peer.\n");
});
$server->listen(address => '127.0.0.1', port => 3000, reuse => 1);
$server->start;
$server->reactor->start unless $server->reactor->is_running;
mojo_cli.pl:
use warnings;
use strict;
use Data::Dump qw/dd pp/;
use Mojo::IOLoop::Client;
use Mojo::IOLoop::LineReader;
my $client = Mojo::IOLoop::Client->new;
$client->on(connect => sub {
my ($cli, $handle) = @_;
print "Connect\n";
my $stream = Mojo::IOLoop::LineReader->new($handle);
$stream->timeout(10);
$stream->on(readln => sub {
my ($strm, $line) = @_;
print "Got line ",pp($line),"\n";
});
$stream->on(close => sub {
print "Closed\n";
$stream->stop;
$stream = undef; # free reference
});
$stream->on(error => sub {
my ($strm, $err) = @_;
warn "Error: $err";
});
$stream->start;
$stream->write("Hello, server, I am a client.\n");
});
$client->on(error => sub {
my ($cli, $err) = @_;
warn "Error: $err";
});
$client->connect(address => '127.0.0.1', port => 3000);
$client->reactor->start unless $client->reactor->is_running;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.