blib/lib/Term/Scroller/Linefeed.pm

filter: Statementstmt 95.7% 45 / 47 Branchbran 70.0% 7 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 86.3% 63 / 73 CC 7 SCAR 13.5
CC
7
Cov
88%
linefeed
7.5
BEGIN
1.0
BEGIN
1.0
CRAP 3.8
« Build ↑ Summary
1 package Term::Scroller::Linefeed;
2  
3 5 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Term::Scroller::Linefeed - Utility function for L<Term::Scroller>, used to read
10 lines from a pseudoterminal, allowing for non-terminated lines.
11  
12 =head1 SYNOPSIS
13  
14 use IO::Pty;
15 use Term::Scroller::Linefeed qw(linefeed);
16  
17 my $pty = IO::Pty->new();
18  
19 # (Set up something to write to the $pty however you like)
20  
21 while( my $line = linefeed($pty) ) {
22  
23 if ($line =~ /\n$/) {
24 print "Pty printed a complete line"
25 }
26 else {
27 print "Pty printed a partial line"
28 }
29  
30 }
31  
32 =head1 DESCRIPTION
33  
34 This module exports the C<linefeed> function which takes an IO::Pty instance and
35 returns the next line of text from read from the slave. The key difference
36 between this (as opposed to simply '<$pty->slave>') is that it will not wait
37 for a newline character at the end of input. So if a sequence of text I<not>
38 ending in a newline is written to the pty master, then it will be available
39 immediately as a "line" returned by this function.
40  
41 B<NOTE:> This stores an internal buffer of lines alongside the IO::Pty instance,
42 adding an arrayref 'term_scroller_linefeed' in the Pty's typeglob.
43  
44 =cut
45  
46 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 5 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 5 ▶use constant BUFFERNAME => 'term_scroller_linefeed';
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 5 our @ISA = qw(Exporter);
56  
57 5 our @EXPORT_OK = qw(linefeed);
58  
59  
60 sub linefeed {
61 25 ▶ my $pty = shift;
Subroutine linefeed: called CC=7 SCAR=20.2
Pod undocumented
62  
63 25 ▶ unless (blessed($pty) && $pty->isa('IO::Pty')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($pty) and $pty->isa("IO::Pty")) 0 25 25
Condition33%1/3
builtin::blessed($pty) and $pty->isa("IO::Pty")
!ll&&!rl&&r
0025
MC/DC0%0/2
builtin::blessed($pty) and $pty->isa("IO::Pty")
builtin::blessed($pty) $pty->isa("IO::Pty")
Truth table33%1/3
A and B
A= builtin::blessed($pty)B= $pty->isa("IO::Pty")
ABresult
0X0
100
111
64 0 croak "Must specify an IO::Pty instance";
65 }
66  
67 # Add buffer to Pty typeglob
68 # if it doesn't exist already
69 25 ▶ if (!exists ${*$pty}{BUFFERNAME}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists do { *$pty }->{"BUFFERNAME"}) 3 22 25
70 3 ${*$pty}{BUFFERNAME} = [];
71 }
72  
73 25 my $buffer = ${*$pty}{BUFFERNAME};
74  
75 25 ▶ if ( @$buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$buffer) { } 0 25 25
76 # Return next line if one is in the buffer
77 0 return shift @$buffer;
78 }
79 else {
80 # If buffer is empty, read more data then return next line
81 25 my $ptymask = '';
82 25 vec($ptymask, fileno($pty->slave), 1) = 1;
83  
84 25 my $ready = select($ptymask, undef, undef, undef);
85 25 ▶ croak "Error select(2)'ing on pty: $!" if $ready == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ready == -1 0 25 25
86  
87 25 my $read = sysread($pty->slave, my $chunk, 4096);
88  
89 25 ▶ return undef unless $read; # EOF
Branch: 1 branch
BranchTrueFalseTotal
unless $read 3 22 25
90  
91 22 push @$buffer, split /^/m, $chunk;
92 22 return shift @$buffer;
93 }
94  
95 }
96  
« Build ↑ Summary