in reply to example for ipc::open3

Here is one that I used for experimenting. It separates stderr and stdout. There are some glitches in it, like if you ask bc to compute a huge number and the buffer goes over 4096, but it demonstrates the idea.
#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; #interface to "bc" calculator my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc"); #if \*ERROR is false, STDERR is sent to STDOUT my $selread = new IO::Select(); my $selerror = new IO::Select(); $selread->add(\*READ); $selerror->add(\*ERROR); # may not be best use of IO::Select, but it works :-) 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"; #timing delay needed tp let bc output select(undef,undef,undef,.01); #see which filehandles have output if($selread->can_read(0)){print "ready->read\n"} if($selerror->can_read(0)){print "ready->error\n"} #get any error from bc sysread(ERROR,$error,4096) if $selerror->can_read(0); if($error){print "\e[1;31m ERROR-> $error \e[0m \n"} #get the answer from bc sysread(READ,$answer,4096) if $selread->can_read(0); if($answer){print "$query = $answer\n"} ($error,$answer)=('',''); } 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. flash japh

Replies are listed 'Best First'.
Re^2: example for ipc::open3
by Anonymous Monk on Jan 07, 2005 at 00:05 UTC
    my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc"); #if \*ERROR is false, STDERR is sent to STDOUT I don't understand \*WRITE \*READ... My in file : /home/.../rep1/file_in my out file : /home/.../rep1/file_out How write my $pid = open ... ??? I would like understand this! thanks anne
      I'm not clear as to what you are trying to do. The \*WRITE ,\*READ, etc are the filehandles which let you send data to and from the program you are running thru IPC. They are pipes.You can name them anything you want, but their order is important.(Read perldoc IPC::Open3). If instead of \*ERROR, you put a 0 (i.e.false), then the stderr of the program gets sent to it's stdout, and will be combined in \*READ. If you want to know how to save the data to a file, you need another couple of filehandles, like:
      ....... open(OUT,">file_out) or warn "$!\n"; # then where you read the output print it to # the OUT filehandle #get the answer from bc sysread(READ,$answer,4096) if $selread->can_read(0); if($answer){print OUT "$query = $answer\n"} ($error,$answer)=('',''); }
      If you want to read input from a file, you need to open that file, and read it line by line, and print each line to the WRITE handle, then wait for output. It sounds like you need to experiment with it a little bit. Here is a simpler version, without a separate stderr.
      #!/usr/bin/perl use warnings; use strict; use IPC::Open3; #interface to "bc" calculator #my $pid = open3(\*WRITE, \*READ, \*ERROR,"bc"); my $pid = open3(\*WRITE, \*READ,0,"bc"); #if \*ERROR is false, STDERR is sent to STDOUT while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; select(undef,undef,undef,2); #get the answer from bc chomp(my $answer = <READ>); print "$query = $answer\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. flash japh
        Hello Zentara,

        I am trying to run some processes simulataneously. When those processes are running simulataneously, I want to see the output of each process and as soon as I find a word I am looking for e.g. error, then I would like to immediatly stop all the processes. I am trying to implement using open3. Could you please advice how I could implement this?

        Thanks.