简体   繁体   English

Perl-如何使用在另一个Perl脚本中的模块中创建的进程句柄

[英]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 : 我想做的事 :

  • Open a process Handler (IPC) 打开流程处理程序(IPC)
  • Use this attribute outside of the Module 在模块外部使用此属性

How I'm trying to do it and fail : 我如何尝试做到而失败:

  • Open the process handler 打开流程处理程序
  • Save the handler in a module's attribute 将处理程序保存在模块的属性中
  • Use the attribute outside the module. 使用模块外部的属性。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM