in reply to question for open2, and by the way, resolves an old thread

The most robust solution would probably be to use Expect but that would probably require rewriting more code. You could just scan the output from <$reader> and look for the next prompt line. You'll have to know what the prompt looks like but you'll probably need to know that to use Expect anyway.

bbfu
Black flowers blossum
Fearless on my breath

  • Comment on (Expect) Re: question for open2, and by the way, resolves an old thread
  • Download Code

Replies are listed 'Best First'.
Re: (Expect) Re: question for open2, and by the way, resolves an old thread
by pg (Canon) on Jan 17, 2003 at 19:46 UTC
    That was what I thought, but interestingly, I didn't get the prompt back through $reader, Hm.. where did it go?

    If I can get it, the whole thing is resolved.

    Update:

    1. tye's suggestion definitely makes sense, as it might be just the prompt is not flushed yet. But I tried this:
      #!/usr/bin/perl use IPC::Open2; use strict; my ($reader, $writer); my $pid = open2($reader, $writer, "sh") || die "failed"; print $writer "history\n"; my $buffer; sysread($reader, $buffer, 4000); print "[", $buffer, "]\n"; print $writer "ls\n"; sysread($reader, $buffer, 4000); print "[", $buffer, "]\n";
      There is no prompt being printed between two prints. So it means/proves that the prompt is NOT THERE, doesn't matter what it is, but I do like tye's suggestion, it is 100% logical.

    2. I did a research, and someone said on internet that sh does not output prompt if it is not used interactively. This matches my result.

      But is there a switch to toggle this feature?

      Set your prompt to include a newline if you want to read it with <$reader>.

                      - tye
Re: (Expect) Re: question for open2, and by the way, resolves an old thread
by jdporter (Paladin) on Jan 17, 2003 at 19:42 UTC
    Even better, you can set the prompt, by setting the PS1 variable. That way you'll be sure to know what it looks like, and you can set it to be something complicated, to reduce the risk of false positives.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.