Re: Input from subprocess called using open() buffered?

From: g r a e m e b [at] c a d e n c e [dot] c o m ("g)
Date: 08/09/04


Date: Mon, 09 Aug 2004 18:51:43 +0100

Success! :)

Thanks for all the replies, I managed to get two solutions which do what I
want, neither of which are probably correct! ;D

The first one, using raw IO::Pty, looks a little like this:

my $pty = new IO::Pty;
my $ttyname = $pty->ttyname();
print "Spawned tty $ttyname\n";
open(COMMAND, "$cmd > $ttyname 2>&1|") || die "Couldn't run command";
while(<$pty>) {
   print ":: $_";
}
close($pty);

I take it that I'm supposed to run the command and direct the IO manually to
the pty I've opened (eg. "cvs update > /dev/pty56 2>&1")? This seems to work
fine, I just want to check I'm using it correctly.

I am a bit baffled as to why I need to have COMMAND as a filehandle - is there
a better way of doing this? And do I need to close both $pty and COMMAND?
Presumably all the output is going to $pty, so I don't even need COMMAND, or do I?

The other method, using Expect, looked a little like this (note that I hacked
this up BEFORE looking at the Expect docs!):

my $process = Expect->spawn("$cmd 2>&1");
while (<$process>) {
   print "|| $_";
}
close($process);

It's more succinct than the one above, but when I looked at the docs, it didn't
seem to even mention being able to loop over the filehandle returned (in the
question about "what if I only want to get output without 'expecting'
anything?". Not sure if this works by accident or design!

Any code criticism welcome...

Thanks,
Graeme.

PS. I was running 'cvs update' (and checkout) - it does indeed have the same
effect if I do 'cvs update 2>&1 | cat'. Thanks, I thought this was an obscure
perl thing, didn't realise other programs actually did different things
depending upon the output file...

510046470588-0001@t-online.de wrote:
> Brian McCauley <nobull@mail.com> writes:
>
>>Failing that, I Expect (hint, hint) there's something on CPAN you
>>could use to allow Perl to fool the other process into thinking its
>>output was going to a terminal.
>
>
>
> IO::Pty, which comes with IO::Tty
>
> Klaus Schilling



Relevant Pages

  • Re: Wait for background processes to complete
    ... To be able to execute commands in the background and wait for their ... The documentation I am referring to is http://perldoc.perl.org/. ... You can run a command in the background with: ... There is a general problem with perl documentation: ...
    (comp.lang.perl.misc)
  • Re: Perl For Amateur Computer Programmers
    ... >professional computer programmers could use with the same ease as Basic. ... >Perl For Amateur Computer Programmers ... Also, taking into account that you're appealing to "scientists", it ... Also, as a side note, you seem to use the noun "command" in a naive ...
    (comp.lang.perl.misc)
  • Obtaining complete Unix command line that evoked script as string
    ... If there is a more appropriate list for this, let me know; the other perl lists I've seen seem to specialised for this. ... Note this is not just the arguments of the call to the script, but everything including pipes and redirects, etc., e.g. ... Ideally the perl interpreter would grab the complete command line as its evoked and I'd access this via a variable. ...
    (perl.beginners)
  • Re: CLOSING Re: Obtaining complete Unix command line that evoked script as string
    ... > To: Grant Jacobs; beginners perl ... > perl and less, much less passing that information along to either ... Only shell that launched them is keeping track of the I/O ... > command is executed. ...
    (perl.beginners)
  • Thanks for the comments Jan. 25, 2006
    ... "shadow" operating system which is running behind Windows. ... Perl can then collect data from that program and do ... that by sending longer strings etc. to the Windows clipboard and then having ... SendKeysuse a Ctrl V command to paste the information to the text editor ...
    (comp.lang.perl.misc)