blib/lib/Mojo/IOLoop/Stream/Role/LineBuffer.pm

filter: Statementstmt 100% 32 / 32 Branchbran 90.0% 9 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 94.3% 50 / 53 CC 7 SCAR 11.0
CC
7
Cov
95%
__ANON__
5.0
watch_lines
2.0
__ANON__
2.0
CRAP 3.0
↑ Summary
1 package Mojo::IOLoop::Stream::Role::LineBuffer;
2  
3 1 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.009';
6  
7 4 ▶has 'read_line_separator' => sub { qr/\x0D?\x0A/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
8 1 has 'write_line_separator' => "\x0D\x0A";
9  
10 1 requires qw(on emit write);
11  
12 6 ▶sub watch_lines {
Subroutine watch_lines: called CC=2 SCAR=6.9
Pod documented
13 6 my $self = shift;
14 6 ▶ return $self if $self->{_read_line_read_cb};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_read_line_read_cb"} 0 6 6
15 8 ▶ $self->{_read_line_read_cb} = $self->on(read => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
16 8 my ($self, $bytes) = @_;
17 8 $self->{_read_line_buffer} .= $bytes;
18 8 my $sep = $self->read_line_separator;
19 8 ▶ while ($self->{_read_line_buffer} =~ s/^(.*?)($sep)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_read_line_buffer"} =~ s/^(.*?)($sep)//su) 7 8 15
20 7 $self->emit(read_line => "$1", "$2");
21 } continue {
22 7 $sep = $self->read_line_separator;
23 }
24 6 });
25 6 ▶ $self->{_read_line_close_cb} = $self->on(close => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
26 6 my $self = shift;
27 6 ▶ if (length($self->{_read_line_buffer} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($self->{"_read_line_buffer"} // "")) 3 3 6
Condition50%1/2
$self->{"_read_line_buffer"} // ""
l!l
60
MC/DC0%0/1
$self->{"_read_line_buffer"} // ""
$self->{"_read_line_buffer"}
Truth table50%1/2
A
A= $self->{"_read_line_buffer"}
Aresult
11
00
28 3 my $sep = $self->read_line_separator;
29 3 ▶ while ($self->{_read_line_buffer} =~ s/^(.*?)($sep)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_read_line_buffer"} =~ s/^(.*?)($sep)//su) 2 3 5
30 2 $self->emit(read_line => "$1", "$2");
31 } continue {
32 2 $sep = $self->read_line_separator;
33 }
34 3 ▶ if (length(my $buffer = $self->{_read_line_buffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $buffer = $self->{"_read_line_buffer"})) 2 1 3
35 2 $self->{_read_line_buffer} = '';
36 2 $self->emit(read_line => $buffer);
37 }
38 }
39 6 });
40 6 return $self;
41 }
42  
43 3 ▶sub write_line {
Subroutine write_line: called CC=1 SCAR=0.0
Pod documented
44 3 my ($self, $line) = (shift, shift);
45 3 my $sep = $self->write_line_separator;
46 3 $self->write("$line$sep", @_);
47 }
48  
49 1 1;
50  
51 =head1 NAME
52  
53 Mojo::IOLoop::Stream::Role::LineBuffer - Read and write streams by lines
54  
55 =head1 SYNOPSIS
56  
57 use Mojo::IOLoop;
58 Mojo::IOLoop->client({port => 3000} => sub {
59 my ($loop, $err, $stream) = @_;
60 $stream->with_roles('+LineBuffer')->watch_lines->on(read_line => sub {
61 my ($stream, $line) = @_;
62 say "Received line: $line";
63 $stream->write_line('Line received');
64 });
65 });
66  
67 =head1 DESCRIPTION
68  
69 L<Mojo::IOLoop::Stream::Role::LineBuffer> composes the method
70 L</"watch_lines"> which causes a L<Mojo::IOLoop::Stream> object to emit the
71 L</"read_line"> event for each line received. The L</"write_line"> method is
72 also provided to add a line separator to the passed data before writing.
73  
74 =head1 EVENTS
75  
76 L<Mojo::IOLoop::Stream::Role::LineBuffer> can emit the following events.
77  
78 =head2 read_line
79  
80 $stream->on(read_line => sub {
81 my ($stream, $line, $separator) = @_;
82 ...
83 });
84  
85 Emitted when a line ending in L</"read_line_separator"> arrives on the stream,
86 and when the stream closes if data is still buffered. The separator is passed
87 as a separate argument if present.
88  
89 =head1 ATTRIBUTES
90  
91 L<Mojo::IOLoop::Stream::Role::LineBuffer> composes the following attributes.
92  
93 =head2 read_line_separator
94  
95 my $separator = $stream->read_line_separator;
96 $stream = $stream->read_line_separator(qr/\x0D\x0A/);
97  
98 Regular expression to indicate new lines in received bytes. Defaults to a
99 newline (LF) character optionally preceded by a CR character (C<\x0D?\x0A>).
100 Note that if you set this to L<the generic newline|perlrebackslash/"\R"> or
101 C<\v> (vertical whitespace), this may match the CR character of a CR/LF
102 sequence and consider the LF as a separate line if they are read separately.
103  
104 =head2 write_line_separator
105  
106 my $separator = $stream->write_line_separator;
107 $stream = $stream->write_line_separator("\x0A");
108  
109 Byte sequence to indicate new lines in data written with L</"write_line">.
110 Defaults to the network newline CR/LF (C<\x0D\x0A>).
111  
112 =head1 METHODS
113  
114 L<Mojo::IOLoop::Stream::Role::LineBuffer> composes the following methods.
115  
116 =head2 watch_lines
117  
118 $stream = $stream->watch_lines;
119  
120 Subscribe to the L<Mojo::IOLoop::Stream/"read"> and
121 L<Mojo::IOLoop::Stream/"close"> events, to buffer received bytes and emit
122 L</"read_line"> when L</"read_line_separator"> is encountered or the stream is
123 closed with buffered data.
124  
125 =head2 write_line
126  
127 $stream = $stream->write_line($bytes);
128 $stream = $stream->write_line($bytes => sub {...});
129  
130 Write a line to the stream by appending L</"write_line_separator"> to the data.
131 The optional drain callback will be executed once all data has been written.
132  
133 =head1 BUGS
134  
135 Report any issues on the public bugtracker.
136  
137 =head1 AUTHOR
138  
139 Dan Book <dbook@cpan.org>
140  
141 =head1 COPYRIGHT AND LICENSE
142  
143 This software is Copyright (c) 2018 by Dan Book.
144  
145 This is free software, licensed under:
146  
147 The Artistic License 2.0 (GPL Compatible)
148  
149 =head1 SEE ALSO
150  
151 L<Mojo::IOLoop::LineReader>, L<MojoX::LineStream>, L<POE::Filter::Line>,
152 L<IO::Async::Protocol::LineStream>
↑ Summary