0

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} ){
  ...
}
1
  • What does self->{PROCESS_HANDLER} mean (without the leading $)? Commented Jun 10, 2010 at 15:47

2 Answers 2

3

 

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;
Sign up to request clarification or add additional context in comments.

3 Comments

What would getlines return exactly? I mean, would it return the output since last time I called the method?
Thank you for you answer but since I am not using Moose, this wasn't exactly what I was searching for.
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.
2

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

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

or

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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.