| 1 | package Proc::Hevy::Reader; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use Errno qw( EWOULDBLOCK ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use IO::Pipe; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 134 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 13 | 134 | my ( $class, $name, $buffer ) = @_; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 134 | my $pipe; | |||||||||||||||||||||||||||||||
| 16 | 134 | ▶ | $pipe = IO::Pipe->new | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $buffer and ref $buffer ne "GLOB"
MC/DC50%1/2
defined $buffer and ref $buffer ne "GLOB"
defined $buffer
ref $buffer ne "GLOB"
Truth table66%2/3
A and B
A= defined $bufferB= ref $buffer ne "GLOB"
| |||||||||||||||||||||||||||||||||
| 17 | if defined $buffer and ref $buffer ne 'GLOB'; | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 134 | bless { name => $name, buffer => $buffer, pipe => $pipe }, $class; | |||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 40 | ▶ | sub child { | ||||||||||||||||||||||||||||||
|
Subroutine
child: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 23 | 40 | my ( $self, $std_h, $fileno ) = @_; | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 40 | my $handle; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 40 | ▶ | if( defined $self->{pipe} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 28 | 32 | $handle = $self->{pipe}->writer; | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | elsif( ref $self->{buffer} eq 'GLOB' ) { | ||||||||||||||||||||||||||||||||
| 31 | 0 | $handle = $self->{buffer}; | |||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | else { | ||||||||||||||||||||||||||||||||
| 34 | 8 | ▶ | open $handle, '>', '/dev/null' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | or confess "$self->{name}: open: /dev/null: $!\n"; | ||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 40 | $handle->autoflush; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 40 | ▶ | POSIX::dup2( $handle->fileno, $fileno ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
$std_h != $handle && (POSIX::dup2($handle->fileno, $fileno) || confess("$self->{'name'}: dup2: $!\n"))
POSIX::dup2($handle->fileno, $fileno) or confess(qq[$self->{"name"}: dup2: $!\n])
MC/DC0%0/2
$std_h != $handle && (POSIX::dup2($handle->fileno, $fileno) || confess("$self->{'name'}: dup2: $!\n"))
$std_h != $handle
POSIX::dup2($handle->fileno, $fileno)
Truth table0%0/3
A && B
A= $std_h != $handleB= POSIX::dup2($handle->fileno, $fileno)
| |||||||||||||||||||||||||||||||||
| 41 | or confess "$self->{name}: dup2: $!\n" | ||||||||||||||||||||||||||||||||
| 42 | if $std_h != $handle; | ||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 94 | ▶ | sub parent { | ||||||||||||||||||||||||||||||
|
Subroutine
parent: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 46 | 94 | my ( $self, $select ) = @_; | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 94 | ▶ | unless( defined $self->{pipe} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | delete $self->{buffer} | ||||||||||||||||||||||||||||||||
| 50 | 20 | ▶ | if defined $self->{buffer}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 20 | return; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 74 | $self->{scratch} = undef; | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 74 | my $handle = $self->{pipe}->reader; | |||||||||||||||||||||||||||||||
| 58 | 74 | $handle->blocking( 0 ); | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 74 | $select->add( $handle ); | |||||||||||||||||||||||||||||||
| 61 | 74 | $self->{select} = $select; | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 74 | return ( $handle, $self ); | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 110 | ▶ | sub read { | ||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=5 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 67 | 110 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 110 | my $handle = $self->{pipe}; | |||||||||||||||||||||||||||||||
| 70 | 110 | my $rc = $handle->sysread( my $data, 4096 ); | |||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 110 | ▶ | if( not defined $rc ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if( $! != EWOULDBLOCK ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 0 | $self->_flush( $self->{scratch} ); | |||||||||||||||||||||||||||||||
| 75 | 0 | confess "$self->{name}: sysread: $!\n"; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | elsif( $rc == 0 ) { | ||||||||||||||||||||||||||||||||
| 79 | 74 | $self->_flush( $self->{scratch} ); | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 74 | $self->{select}->remove( $handle ); | |||||||||||||||||||||||||||||||
| 82 | 74 | ▶ | $handle->close | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | or confess "$self->{name}: close: $!\n"; | ||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||||
| 86 | 36 | $self->_pack( $data ); | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 36 | ▶ | sub _pack { | ||||||||||||||||||||||||||||||
|
Subroutine
_pack: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 91 | 36 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 36 | ▶ | if( ref $self->{buffer} eq 'SCALAR' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 6 | ${ $self->{buffer} } .= $data; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||
| 97 | 30 | ▶ | my $scratch = ( defined $self->{scratch} ? $self->{scratch} : '' ) . $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 30 | ▶ | if( defined $/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 26 | ▶ | while( index( $scratch, $/ ) != -1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | 52 | ( my $line, $scratch ) = split m#$/#, $scratch, 2; | |||||||||||||||||||||||||||||||
| 102 | 52 | $self->_flush( $line ); | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 30 | ▶ | $self->{scratch} = length $scratch ? $scratch : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 126 | ▶ | sub _flush { | ||||||||||||||||||||||||||||||
|
Subroutine
_flush: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||
| 111 | 126 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | return | ||||||||||||||||||||||||||||||||
| 114 | 126 | ▶ | unless defined $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 56 | my $buffer = $self->{buffer}; | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 56 | ▶ | if( ref $buffer eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 119 | 38 | push @$buffer, $data; | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | elsif( ref $buffer eq 'CODE' ) { | ||||||||||||||||||||||||||||||||
| 122 | 18 | $buffer->( $data ); | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||||||
| 125 | 0 | confess "$self->{name}: API error\n"; | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 25 | 1 | |||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||