| 1 | package Gnuplot::Builder::Process; | ||||||||||||||||||||||||||||
| 2 | 38 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 38 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use IPC::Open3 qw(open3); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 38 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 38 | ▶ | use Gnuplot::Builder::PartiallyKeyedList; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 38 | ▶ | use POSIX qw(:sys_wait_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 38 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 38 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 38 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 228 | ▶ | sub _get_env { | ||||||||||||||||||||||||||
|
Subroutine
_get_env: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 13 | 228 | my ($basename, @default) = @_; | |||||||||||||||||||||||||||
| 14 | 228 | my $name = "PERL_GNUPLOT_BUILDER_PROCESS_$basename"; | |||||||||||||||||||||||||||
| 15 | 228 | ▶ | if(defined($ENV{$name}) && $ENV{$name} ne "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{$name} and $ENV{$name} ne ""
MC/DC50%1/2
defined $ENV{$name} and $ENV{$name} ne ""
defined $ENV{$name}
$ENV{$name} ne ""
Truth table66%2/3
A and B
A= defined $ENV{$name}B= $ENV{$name} ne ""
| |||||||||||||||||||||||||||||
| 16 | 5 | return $ENV{$name}; | |||||||||||||||||||||||||||
| 17 | }else { | ||||||||||||||||||||||||||||
| 18 | 223 | ▶ | return wantarray ? @default : $default[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 38 | our $ASYNC = _get_env("ASYNC", 0); | |||||||||||||||||||||||||||
| 23 | 38 | our $NO_STDERR = _get_env("NO_STDERR", 0); | |||||||||||||||||||||||||||
| 24 | 38 | our @COMMAND = _get_env("COMMAND", qw(gnuplot --persist)); | |||||||||||||||||||||||||||
| 25 | 38 | our $MAX_PROCESSES = _get_env("MAX_PROCESSES", 2); | |||||||||||||||||||||||||||
| 26 | 38 | our $PAUSE_FINISH = _get_env("PAUSE_FINISH", 0); | |||||||||||||||||||||||||||
| 27 | 38 | our $TAP = undef; | |||||||||||||||||||||||||||
| 28 | 38 | our $ENCODING = _get_env("ENCODING", undef); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 38 | my $END_SCRIPT_MARK = '@@@@@@_END_OF_GNUPLOT_BUILDER_@@@@@@'; | |||||||||||||||||||||||||||
| 31 | 38 | my $processes = Gnuplot::Builder::PartiallyKeyedList->new; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _clear_zombies { | ||||||||||||||||||||||||||
|
Subroutine
_clear_zombies: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 34 | 0 | $_->_waitpid(0) foreach $processes->get_all_values(); ## cannot use each() method because _waitpid() manipulates $processes... | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 38 | my $null_handle; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub _null_handle { | ||||||||||||||||||||||||||
|
Subroutine
_null_handle: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | return $null_handle if defined $null_handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | my $devnull = File::Spec->devnull(); | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | open $null_handle, ">", $devnull or confess("Cannot open $devnull: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return $null_handle; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | ## PUBLIC ONLY IN TESTS: number of processes it keeps now | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub FOR_TEST_process_num { $processes->size } | ||||||||||||||||||||||||||
|
Subroutine
FOR_TEST_process_num: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ## PUBLIC ONLY IN TESTS | ||||||||||||||||||||||||||||
| 51 | 38 | *FOR_TEST_clear_zombies = *_clear_zombies; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ## Documented public method. | ||||||||||||||||||||||||||||
| 54 | 1 | ▶ | sub wait_all { | ||||||||||||||||||||||||||
|
Subroutine
wait_all: called CC=2 SCAR=10.5
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | while($processes->size > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | my $proc = $processes->get_at(0); | |||||||||||||||||||||||||||
| 57 | 0 | $proc->_waitpid(1); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | ## create a new gnuplot process, create a writer to it and run the | ||||||||||||||||||||||||||||
| 62 | ## given code. If the code throws an exception, the process is | ||||||||||||||||||||||||||||
| 63 | ## terminated. It returns the output of the gnuplot process. | ||||||||||||||||||||||||||||
| 64 | ## | ||||||||||||||||||||||||||||
| 65 | ## Fields in %args are: | ||||||||||||||||||||||||||||
| 66 | ## | ||||||||||||||||||||||||||||
| 67 | ## do (CODE-REF mandatory): the code to execute. $do->($writer). | ||||||||||||||||||||||||||||
| 68 | ## | ||||||||||||||||||||||||||||
| 69 | ## async (BOOL optional, default = false): If set to true, it won't | ||||||||||||||||||||||||||||
| 70 | ## wait for the gnuplot process to finish. In this case, the return | ||||||||||||||||||||||||||||
| 71 | ## value is an empty string. | ||||||||||||||||||||||||||||
| 72 | ## | ||||||||||||||||||||||||||||
| 73 | ## no_stderr (BOOL optional, default = $NO_STDERR): If set to true, | ||||||||||||||||||||||||||||
| 74 | ## the return value won't include gnuplot's STDERR. It just includes | ||||||||||||||||||||||||||||
| 75 | ## STDOUT. | ||||||||||||||||||||||||||||
| 76 | ## | ||||||||||||||||||||||||||||
| 77 | ## on_exit (CODE-REF optional, default: undef): See _new method. | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub with_new_process { | ||||||||||||||||||||||||||
|
Subroutine
with_new_process: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 80 | 0 | my $code = $args{do}; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | croak "do parameter is mandatory" if !defined($code); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $async = defined($args{async}) ? $args{async} : $ASYNC; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $no_stderr = defined($args{no_stderr}) ? $args{no_stderr} : $NO_STDERR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | my $process = $class->_new(capture => !$async, no_stderr => $no_stderr, on_exit => $args{on_exit}); | |||||||||||||||||||||||||||
| 85 | 0 | my $result = ""; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 87 | 0 | $code->($process->_writer); | |||||||||||||||||||||||||||
| 88 | 0 | $process->_close_input(); | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if(!$async) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $result = $process->_wait_to_finish(); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | }catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 0 | my $e = shift; | |||||||||||||||||||||||||||
| 94 | 0 | $process->_terminate(); | |||||||||||||||||||||||||||
| 95 | 0 | die $e; | |||||||||||||||||||||||||||
| 96 | 0 | }; | |||||||||||||||||||||||||||
| 97 | 0 | return $result; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | ## create a new gnuplot process. it blocks if the number of processes | ||||||||||||||||||||||||||||
| 101 | ## has reached $MAX_PROCESSES. | ||||||||||||||||||||||||||||
| 102 | ## | ||||||||||||||||||||||||||||
| 103 | ## Fields in %args are: | ||||||||||||||||||||||||||||
| 104 | ## | ||||||||||||||||||||||||||||
| 105 | ## capture (BOOL optional, default: false): If true, it keeps the | ||||||||||||||||||||||||||||
| 106 | ## STDOUT and STDERR of the process so that it can read them | ||||||||||||||||||||||||||||
| 107 | ## afterward. Otherwise, it just discards the output. | ||||||||||||||||||||||||||||
| 108 | ## | ||||||||||||||||||||||||||||
| 109 | ## no_stderr (BOOL optional, default: false): If true, STDERR is | ||||||||||||||||||||||||||||
| 110 | ## discarded instead of being redirected to STDOUT. | ||||||||||||||||||||||||||||
| 111 | ## | ||||||||||||||||||||||||||||
| 112 | ## on_exit (CODE-REF optional, default: undef): If set, this callback is called when the process | ||||||||||||||||||||||||||||
| 113 | ## exits. It's called like $on_exit->($status), where $status is the $? value for the process. | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub _new { | ||||||||||||||||||||||||||
|
Subroutine
_new: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 115 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 116 | 0 | _clear_zombies(); | |||||||||||||||||||||||||||
| 117 | 0 | my $on_exit = $args{on_exit}; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if(defined($on_exit) && ref($on_exit) ne 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $on_exit and ref $on_exit ne "CODE"
MC/DC0%0/2
defined $on_exit and ref $on_exit ne "CODE"
defined $on_exit
ref $on_exit ne "CODE"
Truth table0%0/3
A and B
A= defined $on_exitB= ref $on_exit ne "CODE"
| |||||||||||||||||||||||||||||
| 119 | 0 | croak("on_exit must be a CODE-REF"); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | while($MAX_PROCESSES > 0 && $processes->size() >= $MAX_PROCESSES) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Gnuplot::Builder::Process::MAX_PROCESSES > 0 and $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
MC/DC0%0/2
$Gnuplot::Builder::Process::MAX_PROCESSES > 0 and $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
$Gnuplot::Builder::Process::MAX_PROCESSES > 0
$processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
Truth table0%0/3
A and B
A= $Gnuplot::Builder::Process::MAX_PROCESSES > 0B= $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
| |||||||||||||||||||||||||||||
| 122 | ## wait for the first process to finish. it's not the smartest | ||||||||||||||||||||||||||||
| 123 | ## way, but is it possible to wait for specific set of | ||||||||||||||||||||||||||||
| 124 | ## processes? | ||||||||||||||||||||||||||||
| 125 | 0 | my $proc = $processes->get_at(0); | |||||||||||||||||||||||||||
| 126 | 0 | $proc->_waitpid(1); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 0 | my $capture = $args{capture}; | |||||||||||||||||||||||||||
| 129 | 0 | my $no_stderr = $args{no_stderr}; | |||||||||||||||||||||||||||
| 130 | 0 | my ($write_handle, $read_handle, $pid); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | ## open3() does not seem to work well with lexical filehandles, so we use fileno() | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $pid = open3($write_handle, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 134 | $capture ? $read_handle : '>&'.fileno(_null_handle()), | ||||||||||||||||||||||||||||
| 135 | $no_stderr ? '>&'.fileno(_null_handle()) : undef, | ||||||||||||||||||||||||||||
| 136 | @COMMAND); | ||||||||||||||||||||||||||||
| 137 | 0 | my $self = bless { | |||||||||||||||||||||||||||
| 138 | pid => $pid, | ||||||||||||||||||||||||||||
| 139 | write_handle => $write_handle, | ||||||||||||||||||||||||||||
| 140 | read_handle => $read_handle, | ||||||||||||||||||||||||||||
| 141 | on_exit => $on_exit, | ||||||||||||||||||||||||||||
| 142 | }, $class; | ||||||||||||||||||||||||||||
| 143 | 0 | $processes->set($pid, $self); | |||||||||||||||||||||||||||
| 144 | 0 | return $self; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | ######## | ||||||||||||||||||||||||||||
| 149 | ######## OBJECT METHODS | ||||||||||||||||||||||||||||
| 150 | ######## | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | ## Return the writer code-ref for this process. | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub _writer { | ||||||||||||||||||||||||||
|
Subroutine
_writer: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 154 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | croak "Input end is already closed" if not defined $self->{write_handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | my $write_handle = $self->{write_handle}; | |||||||||||||||||||||||||||
| 157 | 0 | my $pid = $self->{pid}; | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 159 | 0 | my $msg = join "", @_; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | $msg = Encode::encode($ENCODING, $msg) if defined $ENCODING; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $TAP->($pid, "write", $msg) if defined $TAP; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | print $write_handle ($msg); | |||||||||||||||||||||||||||
| 163 | 0 | }; | |||||||||||||||||||||||||||
| 164 | ## If we are serious about avoiding dead-lock, we must use | ||||||||||||||||||||||||||||
| 165 | ## select() to check writability first and to read from the | ||||||||||||||||||||||||||||
| 166 | ## read_handle. But I guess the dead-lock happens only if the | ||||||||||||||||||||||||||||
| 167 | ## user inputs too much data and the gnuplot outputs too much | ||||||||||||||||||||||||||||
| 168 | ## data to STDOUT/STDERR. That's rare. | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | ## lexical sub because MockTool uses it, too. | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | my $_finishing_commands = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if($PAUSE_FINISH) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | return ('pause mouse close', 'exit'); | |||||||||||||||||||||||||||
| 175 | }else { | ||||||||||||||||||||||||||||
| 176 | 0 | return ('exit'); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 38 | }; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | ## Close the input channel. You can call this method multiple times. | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub _close_input { | ||||||||||||||||||||||||||
|
Subroutine
_close_input: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 182 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | return if not defined $self->{write_handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | my $writer = $self->_writer; | |||||||||||||||||||||||||||
| 185 | 0 | $writer->("\n"); | |||||||||||||||||||||||||||
| 186 | 0 | foreach my $statement (qq{set print "-"}, qq{print '$END_SCRIPT_MARK'}, $_finishing_commands->()) { | |||||||||||||||||||||||||||
| 187 | 0 | $writer->($statement . "\n"); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 0 | undef $writer; | |||||||||||||||||||||||||||
| 190 | 0 | close $self->{write_handle}; | |||||||||||||||||||||||||||
| 191 | 0 | $self->{write_handle} = undef; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | sub _waitpid { | ||||||||||||||||||||||||||
|
Subroutine
_waitpid: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 195 | 0 | my ($self, $blocking) = @_; | |||||||||||||||||||||||||||
| 196 | 0 | ▶ | my $result = waitpid($self->{pid}, $blocking ? 0 : WNOHANG); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 0 | my $status = $?; | |||||||||||||||||||||||||||
| 198 | 0 | ▶ | if($result == $self->{pid} || $result == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$result == $self->{"pid"} or $result == -1
MC/DC0%0/2
$result == $self->{"pid"} or $result == -1
$result == $self->{"pid"}
$result == -1
Truth table0%0/3
A or B
A= $result == $self->{"pid"}B= $result == -1
| |||||||||||||||||||||||||||||
| 199 | 0 | $processes->delete($self->{pid}); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 0 | ▶ | if($result == $self->{pid} && defined($self->{on_exit})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$result == $self->{"pid"} and defined $self->{"on_exit"}
MC/DC0%0/2
$result == $self->{"pid"} and defined $self->{"on_exit"}
$result == $self->{"pid"}
defined $self->{"on_exit"}
Truth table0%0/3
A and B
A= $result == $self->{"pid"}B= defined $self->{"on_exit"}
| |||||||||||||||||||||||||||||
| 202 | 0 | $self->{on_exit}->($status); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | ## Blocks until the process finishes. It automatically close the input | ||||||||||||||||||||||||||||
| 207 | ## channel if necessary. | ||||||||||||||||||||||||||||
| 208 | ## | ||||||||||||||||||||||||||||
| 209 | ## If "capture" attribute is true, it returns the output of the | ||||||||||||||||||||||||||||
| 210 | ## gnuplot process. Otherwise it returns an empty string. | ||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub _wait_to_finish { | ||||||||||||||||||||||||||
|
Subroutine
_wait_to_finish: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 212 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 213 | 0 | $self->_close_input(); | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | my $result = ""; | |||||||||||||||||||||||||||
| 216 | 0 | my $read_handle = $self->{read_handle}; | |||||||||||||||||||||||||||
| 217 | 0 | ▶ | if(defined $read_handle) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | while(defined(my $line = <$read_handle>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 0 | $result .= $line; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | ## Wait for $END_SCRIPT_MARK that we told the gnuplot to | ||||||||||||||||||||||||||||
| 222 | ## print. It is not enough to wait for EOF from $read_handle, | ||||||||||||||||||||||||||||
| 223 | ## because in some cases, $read_handle won't be closed even | ||||||||||||||||||||||||||||
| 224 | ## after the gnuplot process exits. For example, in Linux | ||||||||||||||||||||||||||||
| 225 | ## 'wxt' terminal, 'gnuplot --persist' process spawns its own | ||||||||||||||||||||||||||||
| 226 | ## child process to handle the wxt window. That child process | ||||||||||||||||||||||||||||
| 227 | ## inherits the file descriptors from the gnuplot process, and | ||||||||||||||||||||||||||||
| 228 | ## it won't close the output fd. So $read_handle won't be | ||||||||||||||||||||||||||||
| 229 | ## closed until we close the wxt window. This is not good | ||||||||||||||||||||||||||||
| 230 | ## especially we are in REPL mode. | ||||||||||||||||||||||||||||
| 231 | 0 | my $end_position = index($result, $END_SCRIPT_MARK); | |||||||||||||||||||||||||||
| 232 | 0 | ▶ | if($end_position != -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 0 | $result = substr($result, 0, $end_position); | |||||||||||||||||||||||||||
| 234 | 0 | last; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | 0 | close $read_handle; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | ## Do not actually wait for the process to finish, because it can | ||||||||||||||||||||||||||||
| 240 | ## be a long-lasting process with plot windows. | ||||||||||||||||||||||||||||
| 241 | 0 | return $result; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | sub _terminate { | ||||||||||||||||||||||||||
|
Subroutine
_terminate: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 245 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 246 | 0 | kill 'TERM', $self->{pid}; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | #### #### #### #### #### #### #### #### #### #### #### #### #### | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | package Gnuplot::Builder::Process::MockTool; | ||||||||||||||||||||||||||||
| 252 | 38 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 253 | 38 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | ## tools for a process who mocks gnuplot, i.e., the process who | ||||||||||||||||||||||||||||
| 256 | ## communicates with Gnuplot::Builder::Process. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | ## Receive data from Gnuplot::Builder::Process and execute the $code | ||||||||||||||||||||||||||||
| 260 | ## with the received data. | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | sub receive_from_builder { | ||||||||||||||||||||||||||
|
Subroutine
receive_from_builder: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 262 | 0 | my ($input_handle, $output_handle, $code) = @_; | |||||||||||||||||||||||||||
| 263 | 0 | ▶ | while(defined(my $line = <$input_handle>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 0 | $code->($line); | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | ## Windows does not signal EOF on $input_handle so we must | ||||||||||||||||||||||||||||
| 267 | ## detect the end of script by ourselves. | ||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if(index($line, $END_SCRIPT_MARK) != -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | print $output_handle "$END_SCRIPT_MARK\n"; | |||||||||||||||||||||||||||
| 270 | 0 | $code->("$_\n") foreach $_finishing_commands->(); | |||||||||||||||||||||||||||
| 271 | 0 | last; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 38 | 1; | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | __END__ | ||||||||||||||||||||||||||||