File Coverage

blib/lib/Mojo/IOLoop/LineReader.pm
Criterion Covered Total %
statement 39 39 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 55 55 100.0


line stmt bran cond sub pod time code
1              
2             package Mojo::IOLoop::LineReader;
3             $Mojo::IOLoop::LineReader::VERSION = '0.2';
4             # ABSTRACT: Non-blocking line-oriented input stream
5              
6 5     5   81653 use Mojo::Base 'Mojo::IOLoop::Stream';
  5         6  
  5         26  
7              
8 5     5   656747 use Scalar::Util ();
  5         9  
  5         1728  
9              
10             has 'input_record_separator';
11              
12             sub new {
13 15     15 1 13039 my $self = shift->SUPER::new(@_);
14 15         205 $self->{lr_chunk} = '';
15 15         42 $self->input_record_separator($/);
16 15         83 return $self->_setup;
17             }
18              
19             sub _setup {
20 15     15   15 my $self = shift;
21              
22 15         52 Scalar::Util::weaken($self);
23 15     15   105 $self->on(close => sub { shift; $self->_closeln(@_) });
  15         2253  
  15         27  
24 15     10   110 $self->on(read => sub { shift; $self->_readln(@_) });
  10         1485  
  10         20  
25              
26 15         65 return $self;
27             }
28              
29             sub _closeln {
30 15     15   20 my ($self) = @_;
31 15 100       47 $self->emit(readln => $self->{lr_chunk}) if length $self->{lr_chunk};
32 15         1458 $self->{lr_chunk} = '';
33             }
34              
35             sub _readln {
36 10     10   13 my ($self, $bytes) = @_;
37              
38 10     3   173 open my $r, '<', \$bytes;
  3         23  
  3         3  
  3         19  
39 10         2448 my $n;
40 10         25 local $/ = $self->input_record_separator;
41 10         100 while (<$r>) {
42 45 100       66 unless (defined $n) {
43 10         18 $n = $self->{lr_chunk} . $_;
44 10         23 next;
45             }
46 35         56 $self->emit(readln => $n);
47 35         1622 $n = $_;
48             }
49 10 100       33 if (chomp(my $tmp = $n)) {
50 6         11 $self->emit(readln => $n);
51 6         36 $n = '';
52             }
53 10         48 $self->{lr_chunk} = $n;
54             }
55              
56             1;
57              
58             __END__