File Coverage

blib/lib/Term/Scroller/Linefeed.pm
Criterion Covered Total %
statement 43 45 95.5
branch 7 10 70.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 0 1 0.0
total 61 69 88.4


line stmt bran cond sub pod time code
1             package Term::Scroller::Linefeed;
2              
3 5     5   98 use 5.020;
  5         15  
4 5     5   29 use strict;
  5         10  
  5         97  
5 5     5   24 use warnings;
  5         7  
  5         198  
6              
7             =head1 NAME
8              
9             Term::Scroller::Linefeed - Utility function for L, 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 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
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 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     5   34 use Carp;
  5         7  
  5         254  
47 5     5   26 use IO::Handle;
  5         10  
  5         194  
48 5     5   31 use Exporter;
  5         6  
  5         224  
49 5     5   34 use Scalar::Util qw(blessed);
  5         5  
  5         269  
50              
51 5     5   35 use IO::Pty;
  5         10  
  5         213  
52              
53 5     5   31 use constant BUFFERNAME => 'term_scroller_linefeed';
  5         10  
  5         1732  
54              
55             our @ISA = qw(Exporter);
56              
57             our @EXPORT_OK = qw(linefeed);
58              
59              
60             sub linefeed {
61 25     25 0 76 my $pty = shift;
62              
63 25 50 33     653 unless (blessed($pty) && $pty->isa('IO::Pty')) {
64 0         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 100       82 if (!exists ${*$pty}{BUFFERNAME}) {
  25         163  
70 3         9 ${*$pty}{BUFFERNAME} = [];
  3         89  
71             }
72              
73 25         60 my $buffer = ${*$pty}{BUFFERNAME};
  25         61  
74              
75 25 50       70 if ( @$buffer ) {
76             # Return next line if one is in the buffer
77 0         0 return shift @$buffer;
78             }
79             else {
80             # If buffer is empty, read more data then return next line
81 25         82 my $ptymask = '';
82 25         137 vec($ptymask, fileno($pty->slave), 1) = 1;
83              
84 25         873 my $ready = select($ptymask, undef, undef, undef);
85 25 50       134 croak "Error select(2)'ing on pty: $!" if $ready == -1;
86              
87 25         95 my $read = sysread($pty->slave, my $chunk, 4096);
88              
89 25 100       748 return undef unless $read; # EOF
90              
91 22         98 push @$buffer, split /^/m, $chunk;
92 22         119 return shift @$buffer;
93             }
94              
95             }
96