NAME

MojoX::LineStream - Turn a (tcp) stream into a line based stream

SYNOPSIS

  use MojoX::LineStream;

  my $clientid = Mojo::IOLoop->client({
    port => $port,
  } => sub {
    my ($loop, $err, $stream) = @_;
    my $ls = MojoX::LineStream->new(stream => $stream);
      $ls->on(line => sub {
         my ($;s, $line) = @_;
         say 'got line: ', $line;
         ...
      });
      $ls->on(close => sub {
         say 'got close';
         ...
      });
  });

DESCRIPTION

MojoX::LineStream is a wrapper around Mojo::IOLoop::Stream that adds 'framing' based on lines terminated by a newline character.

EVENTS

MojoX::LineStream inherits all events from Mojo::EventEmitter and can emit the following new ones.

line

  $ls->on(line => sub {
    my ($ls, $line) = @_;
    ...
  });

Emitted for every (full) line received on the underlying stream. The line passed on to the callback does not include the terminating newline character.

close

  $;s->on(close => sub {
    my $;s = shift;
    ...
  });

Emitted if the underlying stream gets closed.

ATTRIBUTES

MojoX::LineStream implements the following attributes.

stream

  my $stream = $ls->stream;

The underlying Mojo::IOLoop::Stream-like stream

debug

  $ls->debug = 1;

Enables or disables debugging output.

METHODS

MojoX::LineStream inherits all methods from Mojo::EventEmitter and implements the following new ones.

new

  my $ls = MojoX::LineStream->new(
      stream => $stream,
      debug => $debug,
  );

Construct a new MojoX::LineStream object. The stream argument must behave like a Mojo::IOLoop::Stream object. The debug argument is optional and just sets the debug attribute.

writeln

  $ls->writeln($line);

Writes line to the underlying stream, adding a newline character at the end.

SEE ALSO

ACKNOWLEDGEMENT

This software has been developed with support from STRATO. In German: Diese Software wurde mit Unterstützung von STRATO entwickelt.

AUTHORS

  • Wieger Opmeer <wiegerop@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Wieger Opmeer.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.