Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

IPC::pump in object oriented programming

by xhudik (Initiate)
on Jan 12, 2012 at 16:09 UTC ( [id://947566]=perlquestion: print w/replies, xml ) Need Help??

xhudik has asked for the wisdom of the Perl Monks concerning the following question:

Hi There,

I'm experiencing a problem with IPC::Run pump function. Well, when I wrote the code as non object-oriented - everything works well. But if I rewrite it into modulino(OOP), pump function get stuck. First, I thought some kind of mistype, but I controlled it many times and everything looks pretty much the same ...

Do you know why I'm getting:

... ;;;;;;6 IPC::Run 0001 [#1(9804)]: ** pumping
Instead of finishing pumping function and moving forward?

thanks, Tomas

Program code: (put it into file.pm and run it as: perl file.pm - it will automatically run the run function.

package pak; __PACKAGE__->run(@ARGV) unless caller(); use IPC::Run qw(start pump finish timeout); sub run { ref(my $class= shift) and die "Class name needed"; $|++; my $tok_program = "/bin/./cat"; my @tok_param=(); my $pokus = $class->new($tok_program,@tok_param); $pokus->{tok_in}="hallo\n"; do{ print "zz::$pokus->{tok_in};;$pokus-> {tok_out};;$pokus->{tok_err +};;".length($pokus->{tok_in})."\n"; $pokus->{tok}->pump; print "kk\n"; } while $pokus->{tok_out} !~ /\n\z/; #$r }#sub run sub new{ ref(my $class= shift) and die "Class name needed"; my $tok_program = shift; my @tok_param = @_; my ( $TOK_IN, $TOK_OUT, $TOK_ERR, $TOK ); $TOK = start [ $tok_program, @tok_param ], '<', \$TOK_IN, '1>pty>', +\$TOK_OUT, '2>', \$TOK_ERR, debug => 2 or die "Can't exec program: $?;\n"; my $self = {tok => $TOK, tok_in => $TOK_IN, tok_out => $TOK_OUT, tok +_err => $TOK_ERR, tok_program => $tok_program}; bless $self,$class; return $self; }

Replies are listed 'Best First'.
Re: IPC::pump in object oriented programming
by Eliya (Vicar) on Jan 12, 2012 at 17:14 UTC

    I think your problem is that you're passing a reference to $TOK_IN to the start function, but then are making a copy of the variable as the object attribute $self->{tok_in}.  In your run method, you're then assigning some input value to the copy ($pokus->{tok_in}="hallo\n";), while IPC::Run is attempting to transfer (pump) input from the original variable $TOK_IN, which is still empty...

    To avoid the problem, just set up the attributes before calling start, and pass \$self->{tok_in} instead of \$TOK_IN  (same holds for $TOK_OUT and $TOK_ERR):

    ... my $self = {tok_in => $TOK_IN, tok_out => $TOK_OUT, tok_err => $TOK_ +ERR, tok_program => $tok_program}; $self->{tok} = start [ $tok_program, @tok_param ], '<', \$self->{tok_in}, '1>pty>', \$self->{tok_out}, '2>', \$self->{tok_err}, debug => 2 or die "Can't exec program: $?;\n";

    Actually, you don't really need an extra $TOK_IN etc. at all — you can also just set the attributes to undefmy $self = {tok_in => undef, ...

      yep, this was it! thanks Eliya.

      Basicaly, some other minor changes were needed, but you found the major why.

      enjoy the weekend :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://947566]
Approved by Eliya
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found