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   107 use 5.020;
  5         20  
4 5     5   27 use strict;
  5         10  
  5         107  
5 5     5   25 use warnings;
  5         10  
  5         204  
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         10  
  5         320  
47 5     5   35 use IO::Handle;
  5         6  
  5         163  
48 5     5   25 use Exporter;
  5         10  
  5         223  
49 5     5   34 use Scalar::Util qw(blessed);
  5         10  
  5         261  
50              
51 5     5   33 use IO::Pty;
  5         10  
  5         232  
52              
53 5     5   30 use constant BUFFERNAME => 'term_scroller_linefeed';
  5         9  
  5         1834  
54              
55             our @ISA = qw(Exporter);
56              
57             our @EXPORT_OK = qw(linefeed);
58              
59              
60             sub linefeed {
61 25     25 0 78 my $pty = shift;
62              
63 25 50 33     609 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       68 if (!exists ${*$pty}{BUFFERNAME}) {
  25         146  
70 3         15 ${*$pty}{BUFFERNAME} = [];
  3         72  
71             }
72              
73 25         69 my $buffer = ${*$pty}{BUFFERNAME};
  25         58  
74              
75 25 50       90 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         66 my $ptymask = '';
82 25         170 vec($ptymask, fileno($pty->slave), 1) = 1;
83              
84 25         834 my $ready = select($ptymask, undef, undef, undef);
85 25 50       129 croak "Error select(2)'ing on pty: $!" if $ready == -1;
86              
87 25         84 my $read = sysread($pty->slave, my $chunk, 4096);
88              
89 25 100       611 return undef unless $read; # EOF
90              
91 22         88 push @$buffer, split /^/m, $chunk;
92 22         138 return shift @$buffer;
93             }
94              
95             }
96