in reply to Re^2: How to stop printing the output of a command on screen when the command fails?
in thread How to stop printing the output of a command on screen when the command fails?

I wasn't addressing your primary problem (although if you are using linux then try &2>1), but what appears to be a separate bug that will manifest itself when the current blocker bug is fixed.

True laziness is hard work
  • Comment on Re^3: How to stop printing the output of a command on screen when the command fails?

Replies are listed 'Best First'.
Re^4: How to stop printing the output of a command on screen when the command fails?
by perl_mystery (Beadle) on Dec 24, 2010 at 07:34 UTC

    Hi, I am new to open3 and perldoc is too vague to understand I am trying a simple perforce command to run in open3.Basically I want to capture both STDOUT and STDERR aswell.I have tried various ways but none of them seem to be working.I am hoping someone will comment.

    foreach my $file (@changed_files) { #my $p4where_output=`p4 where $file > NUL: 2>&1`;#backticks option not + working,so commented out #print "P4 where output:$p4where_output\n"; my $cmd = "p4 where $file"; local(*HIS_IN, *HIS_OUT, *HIS_ERR); my $childpid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, p4 where $cmd); print HIS_IN "stuff\n"; close(HIS_IN); # Give end of file to kid. my @outlines = <HIS_OUT>; # Read till EOF. my @errlines = <HIS_ERR>; # XXX: block potential if massive print "STDOUT:\n", @outlines, "\n"; print "STDERR:\n", @errlines, "\n"; print @errlines; close HIS_OUT; close HIS_ERR; waitpid($childpid, 0); if ($?) { print "That child exited with wait status of $?\n"; } if(@errlines eq 'file(s) not in client view') { push @changed_paths,"$file\n"; } }
      You probably want to use IO::Select with the IPC3 filehandles, so you won't block. Look at this example, using the calculator program, bc. You can adapt your p4 to run this way.
      #!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; #interface to "bc" calculator my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        I really need help on this.Tried in a different way as below,am not able to store the STDERR output into any variable.The below program doesnt print anything.I have been working on this since last night,couldnt get a handle on this.Any help is truly appreciated.

        #!/usr/bin/perl -w use strict; use warnings; use diagnostics; #use diagnostics; use IPC::Open3; my @changed_paths; my @changed_files=("//depot/perl/tools/scripts/files/data.c"); foreach my $file (@changed_files) { my($wtr, $rdr, $err); use Symbol 'gensym'; $err = gensym; my $pid = open3($wtr, $rdr, $err,'p4 where $file'); print "$err\n"; #prints as GLOB(0x183f108),why is it so? #waitpid( $pid, 0 ); //Why do we need this?Program never halts if th +is is present my $child_exit_status = $? >> 8; if ($err eq 'file(s) not in client view') { push @changed_paths,$file; } print @changed_paths; }

        Erasing this,posted twice