Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

example for ipc::open3

by Anonymous Monk
on Jan 06, 2005 at 14:11 UTC ( #419919=perlquestion: print w/replies, xml ) Need Help??

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

hi, I would like a small example for use ipc::open3. I don't understand the examples of perlipc and perlfaq8 I have a command who use a file to generate a new out file I'd like viewing the error message in a file or somthing else... Thanks Anne

Replies are listed 'Best First'.
Re: example for ipc::open3
by zentara (Archbishop) on Jan 06, 2005 at 19:25 UTC
    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
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://419919]
Approved by wolfger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2019-11-19 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (95 votes). Check out past polls.

    Notices?