[英]Perl - How to use a process Handle created in a Module in another Perl Script
Ultimately, what I want to do is to start a process in a module and parse the output in real time in another script. 最终,我想做的是在模块中启动一个进程,并在另一个脚本中实时解析输出。
What I want to do : 我想做的事 :
How I'm trying to do it and fail : 我如何尝试做到而失败:
Code example : 代码示例:
#module.pm
$self->{PROCESS_HANDLER};
sub doSomething{
...
open( $self->{PROCESS_HANDLER}, "run a .jar 2>&1 |" );
...
}
#perlScript.pl
my $module = new module(...);
...
$module->doSomething();
...
while( $module->{PROCESS_HANDLER} ){
...
}
package Thing;
use Moose;
use IO::Pipe;
has 'foo' => (
is => 'ro',
isa => 'IO::Handle',
default => sub {
my $handle = IO::Pipe->new;
$handle->reader('run a .jar 2>&1'); # note, *no* pipe character here
return $handle;
});
1;
package main;
use Thing;
my $t = Thing->new;
say $t->foo->getlines;
Your while
statement is missing a readline
iterator, for one thing: 您的while
语句缺少一个readline
迭代器,原因是:
while( < {$module->{PROCESS_HANDLER}} > ) { ...
or 要么
while( readline($module->{PROCESS_HANDLER}) ) { ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.