blib/lib/IO/Async/Protocol/LineStream.pm

filter: Statementstmt 100% 28 / 28 Branchbran 75.0% 3 / 4 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 93.4% 43 / 46 CC 5 SCAR 7.1
CC
5
Cov
94%
on_read
3.1
configure
3.0
BEGIN
1.0
CRAP 2.0
« blib/lib/IO/Async/OS/linux.pm ↑ Summary blib/lib/IO/Async.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Protocol::LineStream 0.805;
7  
8 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use base qw( IO::Async::Protocol::Stream );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 C<IO::Async::Protocol::LineStream> - stream-based protocols using lines of
18 text
19  
20 =head1 SYNOPSIS
21  
22 =for highlighter language=perl
23  
24 Most likely this class will be subclassed to implement a particular network
25 protocol.
26  
27 package Net::Async::HelloWorld;
28  
29 use strict;
30 use warnings;
31 use base qw( IO::Async::Protocol::LineStream );
32  
33 sub on_read_line
34 {
35 my $self = shift;
36 my ( $line ) = @_;
37  
38 if( $line =~ m/^HELLO (.*)/ ) {
39 my $name = $1;
40  
41 $self->invoke_event( on_hello => $name );
42 }
43 }
44  
45 sub send_hello
46 {
47 my $self = shift;
48 my ( $name ) = @_;
49  
50 $self->write_line( "HELLO $name" );
51 }
52  
53 This small example elides such details as error handling, which a real
54 protocol implementation would be likely to contain.
55  
56 =head1 DESCRIPTION
57  
58 =cut
59  
60 =head1 EVENTS
61  
62 The following events are invoked, either using subclass methods or CODE
63 references in parameters:
64  
65 =head2 on_read_line $line
66  
67 Invoked when a new complete line of input is received.
68  
69 =cut
70  
71 =head1 PARAMETERS
72  
73 The following named parameters may be passed to C<new> or C<configure>:
74  
75 =head2 on_read_line => CODE
76  
77 CODE reference for the C<on_read_line> event.
78  
79 =cut
80  
81 sub _init
82 {
83 2 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
84 2 $self->SUPER::_init;
85  
86 2 $self->{eol} = "\x0d\x0a";
87 2 $self->{eol_pattern} = qr/\x0d?\x0a/;
88 }
89  
90 sub configure
91 {
92 3 ▶ my $self = shift;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
93 3 my %params = @_;
94  
95 3 foreach (qw( on_read_line )) {
96 3 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 2 1 3
97 }
98  
99 3 $self->SUPER::configure( %params );
100 }
101  
102 sub on_read
103 {
104 4 ▶ my $self = shift;
Subroutine on_read: called CC=3 SCAR=11.4
Pod documented
105 4 my ( $buffref, $eof ) = @_;
106  
107 # Easiest to run each event individually, in case it returns a CODE ref
108 4 ▶ $$buffref =~ s/^(.*?)$self->{eol_pattern}// or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$$buffref =~ s/^(.*?)$self->{"eol_pattern"}//u or return 0 0 4 4
109  
110 4 ▶ return $self->invoke_event( on_read_line => $1 ) || 1;
Condition50%1/2
$self->invoke_event('on_read_line', $1) || 1
l!l
40
MC/DC0%0/1
$self->invoke_event('on_read_line', $1) || 1
$self->invoke_event('on_read_line', $1)
Truth table50%1/2
A
A= $self->invoke_event('on_read_line', $1)
Aresult
11
00
111 }
112  
113 =head1 METHODS
114  
115 =cut
116  
117 =head2 write_line
118  
119 $lineprotocol->write_line( $text );
120  
121 Writes a line of text to the transport stream. The text will have the
122 end-of-line marker appended to it; C<$text> should not end with it.
123  
124 =cut
125  
126 sub write_line
127 {
128 1 ▶ my $self = shift;
Subroutine write_line: called CC=1 SCAR=0.0
Pod documented
129 1 my ( $line, @args ) = @_;
130  
131 1 $self->write( "$line$self->{eol}", @args );
132 }
133  
134 =head1 AUTHOR
135  
136 Paul Evans <leonerd@leonerd.org.uk>
137  
138 =cut
139  
140 2 0x55AA;
« blib/lib/IO/Async/OS/linux.pm ↑ Summary blib/lib/IO/Async.pm »