Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Capture STDOUT and send to screen with open3 (updated)

by haukex (Archbishop)
on Mar 05, 2018 at 17:23 UTC ( [id://1210365]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Capture STDOUT and send to screen with open3
in thread Capture STDOUT and send to screen with open3

Could you provide a code snipit of how to accomplish?

Something like this maybe?

Update: Now with STDERR support.

use warnings; use strict; use IPC::Run qw/ run new_chunker /; my @cmd = ('yourcommand', 'arg1', 'arg2'); my (@out,@err); run \@cmd, '>', new_chunker("\n"), sub { my $line = shift; print $line; push @out, $line; }, '2>', new_chunker("\n"), sub { my $line = shift; print STDERR $line; push @err, $line; } or die $?;

Replies are listed 'Best First'.
Re^4: Capture STDOUT and send to screen with open3 (updated)
by karlgoethebier (Abbot) on Mar 06, 2018 at 11:33 UTC
Re^4: Capture STDOUT and send to screen with open3 (updated)
by computergeek (Novice) on Mar 05, 2018 at 18:10 UTC

    I am sorry, I am trying it and it hangs. Here is what I am executing: What am I doing wrong? Also, will this print a line real time from cat as if output were not being captured?

    my @cat = qw( cat ); run \@cat, '<pipe', \*PIPE, '>', new_chunker("\n"), sub { my $line = shift; print $line; push @out, $line; }, '2>', new_chunker("\n"), sub { my $line = shift; print STDERR $line; push @err, $line; } or die $?; print PIPE "some input\n"; close PIPE; print "out @out"; print "err @err";

      IPC::Run's run basically runs a command to completion, if you want interactive communication with the subprocess, as your code example appears to be showing, you'll want its start/pump/finish interface. The module's documentation is a bit long, but it is a powerful module, so I recommend reading up on it. Also, a more detailed description of what this subprocess is and what you want to communicate to it would be helpful.

      Otherwise, if all you want to do is send a single string to its STDIN, then I think you should just be able to do run \@cmd, '<', \$data, ....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-20 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found