Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

<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;

In reply to Re^7: IO::Socket tutorial (updated x2!) by haukex
in thread IO::Socket tutorial by BernieC

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (2)
    As of 2021-05-09 21:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Perl 7 will be out ...





      Results (102 votes). Check out past polls.

      Notices?