tags:

views:

56

answers:

2

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)
  • 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} ){
  ...
}
+2  A: 

 

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;
daxim
What would getlines return exactly? I mean, would it return the output since last time I called the method?
Zwik
[Yes](http://p3rl.org/IO::Handle#%24io-%3Egetlines).
daxim
Thank you for you answer but since I am not using Moose, this wasn't exactly what I was searching for.
Zwik
I think you're conflating Moose with the `IO` family of modules. `getlines` is from IO::Handle. This example works just fine without Moose, but unsugared OO code in Perl is lengthier and would cloud the important part.
daxim
+2  A: 

Your while statement is missing a readline iterator, for one thing:

while( < {$module->{PROCESS_HANDLER}} > ) { ...

or

while( readline($module->{PROCESS_HANDLER}) ) { ...
mobrule
< {$module->{PROCESS_HANDLER}} > did not work, but readline($module->{PROCESS_HANDLER}) did. Thank you very much.
Zwik
my $handle = $module->{PROCESS_HANDLER}; while (<$handle>) ... would work.
runrig