1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# file: Subprocess.pm
# Herve Saint-Amand
# Universitaet des Saarlandes
# Wed May 14 09:55:46 2008
# NOTE that to use this with Philipp Koehn's tokenizer.perl I had to modify
# that script to autoflush its streams, by adding a '$|++' to it
#------------------------------------------------------------------------------
# includes
package Subprocess;
use warnings;
use strict;
use Encode;
use IPC::Open2;
#------------------------------------------------------------------------------
# constructor
sub new {
my ($class, @cmd) = @_;
bless {
cmd => \@cmd,
num_done => 0,
child_in => undef,
child_out => undef,
}, $class;
}
#------------------------------------------------------------------------------
sub start {
my ($self) = @_;
open2 ($self->{child_out}, $self->{child_in}, @{$self->{cmd}});
}
sub do_line {
my ($self, $line) = @_;
my ($in, $out) = ($self->{child_in}, $self->{child_out});
$line =~ s/\s+/ /g;
print $in encode ('UTF-8', $line), "\n";
$in->flush ();
my $ret = decode ('UTF-8', scalar <$out>);
chomp $ret;
$self->{num_done}++;
return $ret;
}
sub num_done { shift->{num_done} }
#------------------------------------------------------------------------------
1;
|