Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: IPC::Open3 woes

by abstracts (Hermit)
on Mar 10, 2002 at 22:22 UTC ( [id://150775]=note: print w/replies, xml ) Need Help??


in reply to IPC::Open3 woes

Hello,
Here is a script that does what you wanted to to with enough comments to explain every step:
#!/usr/bin/perl -w # Script to test IPC::Open3 # Runs, displays $pid to STDOUT, but opened # files are empty. use strict; use warnings; use diagnostics; use IPC::Open3; use IO::Select; # for select use Symbol; # for gensym $|++; my $cmd = "./test.pl"; open(ERRLOG, ">error.log") or die "Can't open error log! $!"; open(OUTPUT, ">output.log") or die "Can't open output log! $!"; my ($infh,$outfh,$errfh); # these are the FHs for our child $errfh = gensym(); # we create a symbol for the errfh # because open3 will not do that for us my $pid; eval{ $pid = open3($infh, $outfh, $errfh, $cmd); }; die "open3: $@\n" if $@; print "PID was $pid\n"; my $sel = new IO::Select; # create a select object to notify # us on reads on our FHs $sel->add($outfh,$errfh); # add the FHs we're interested in while(my @ready = $sel->can_read) { # read ready foreach my $fh (@ready) { my $line = <$fh>; # read one line from this fh if(not defined $line){ # EOF on this FH $sel->remove($fh); # remove it from the list next; # and go handle the next FH } if($fh == $outfh) { # if we read from the outfh print OUTPUT $line; # print it to OUTFH } elsif($fh == $errfh) {# do the same for errfh print ERRLOG $line; } else { # we read from something else?!?! die "Shouldn't be here\n"; } } } close(ERRLOG) or die "Can't close filehandle! $!"; close(OUTPUT) or die "Can't close filehandle! $!";
So, basically, open3 gives you the file handles that you can use to read from or write to the child process. It will not automatically pipe the output to a file as you thought. What you need to do is read from the file handles and print them to the files yourself.

Hope this helps,,,

Aziz,,,

Replies are listed 'Best First'.
Re: Re: IPC::Open3 woes
by particle (Vicar) on Mar 11, 2002 at 15:59 UTC
    looks like many people beat me to it on this thread. i guess i should expect that now that my days are filled with ksh and not perl {grin}

    abstracts this is good code. very good.

    but not perfect.

    from select (but not listed in IO::Select -- it should be!):

    WARNING: One should not attempt to mix buffered I/O (like read() or <FH>) with select(), except as permitted by POSIX, and even then only on POSIX systems. You have to use sysread() instead.

    it is a simple task to modify your code to use sysread, and i don't have the time to do it myself now. if nobody's attacked this by wednesday evening, i'll try to post it then.

    otherwise, you'll be Suffering from Buffering

    ~Particle ;Þ

Re: Re: IPC::Open3 woes
by rjray (Chaplain) on Mar 10, 2002 at 22:44 UTC

    This would be essentially the sort of parent-does-the-reading script I suggested in my longer response below. Nicely done, to, at least from a cursory reading-over. ++, Aziz... :-)

    --rjray

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://150775]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found