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

I have a need to capture STDOUT and STDERR and send both to screen. I am currently using open3 tto pipe to the command and capturing STDOUT, STDERR but I need the output to be sent real time as the child writes it rather than what I am doing now which is printing it after the fact. I see no way of getting open3 to dup the file handle without closing it but this is what I really want. Here is the code snip-it of what I am doing now:

open3 (\*PIPE, ">&PIPEOUT", \*PIPEERR, qq[$Command]) or die "cannot pi +pe to $Command "; print PIPE "Hello, World\n"; close PIPE; $pid2 = waitpid(-1,0); my $ExitCode = $? >> 8; print "ExitCode $ExitCode\n" ; @SpoolFile = <PIPEOUT>; print "SpoolFile @SpoolFile\n"; @CMDERR = <PIPEERR>;

If using IO::Tee and open3 in conjunction solve the issue then I cannot figure out how to do. Would appreciate help.

Replies are listed 'Best First'.
Re: Capture STDOUT and send to screen with open3
by haukex (Archbishop) on Mar 04, 2018 at 11:05 UTC
    I have a need to capture STDOUT and STDERR and send both to screen.

    Have a look at tee from Capture::Tiny, I have found it to be a great module.

      Yes, after I posted I found that and you are right. My issue right now is I would like to use a more base module in perl to improve portability. Capture::Tiny is not available for install w/o cpan on Oracle Linux

        Capture::Tiny is not available for install w/o cpan on Oracle Linux

        You don't need cpan, does the manual installation procedure not work?

        $ wget http://search.cpan.org/CPAN/authors/id/D/DA/DAGOLDEN/Capture-Ti +ny-0.46.tar.gz $ tar zxvf Capture-Tiny-0.46.tar.gz $ cd Capture-Tiny-0.46 $ perl Makefile.PL $ make $ make test $ make install

        Or perhaps cpanm:

        $ curl -L https://cpanmin.us | perl - App::cpanminus $ cpanm Capture::Tiny
Re: Capture STDOUT and send to screen with open3
by BrowserUk (Patriarch) on Mar 04, 2018 at 03:56 UTC
    I have a need to capture STDOUT and STDERR and send both to screen.

    If you don't capture it, won't it just end up on the screen anyway?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
      "...anyway?"

      Control?

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Capture STDOUT and send to screen with open3
by karlgoethebier (Abbot) on Mar 05, 2018 at 08:41 UTC
    "...capture STDOUT and STDERR..."

    Probably IPC::Run is what you want. See also Re: error variables after IPC::Run.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      I am currently trying to work out how to do it with IPC::Run and having trouble. Could you provide a code snipit of how to accomplish? The closest example I have right now is how to duplicate IPC::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 $?;