in reply to Re: example for ipc::open3
in thread example for ipc::open3

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

Replies are listed 'Best First'.
Re^3: example for ipc::open3
by zentara (Cardinal) on Jan 07, 2005 at 13:49 UTC
    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.

        Uh, sounds like work. :-) I would use the first example I gave you, which uses IO::Select to read the filehandles. Open each of your processes with IPC and add the \*READ filehandle to select, and store the $pid. I would put them in a hash for easy reference. When you read the output, run it through a regex, and when you find the word, kill all $pids. You probably would find an example of doing what you want with the Parallel::ForkManager module, which may be better suited for this kind of operation.

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