in reply to Re: need help with open2, please!
in thread need help with open2, please!

This sounds like a problem I'm having with both the Lingua::ISpell and Text::ISpell modules. Every script I write with them just hangs when an attempt is made to read from the spawned process's output pipe. Both modules use an open2 call from an _init() function prior to calling spellcheck(). Here's what they look like:
sub _init { ... $Lingua::Ispell::pid = open2( *Reader, *Writer, $Lingua::Ispell::path, '-a', '-S', @options, ); ... } sub spellcheck { _init() or return(); # caller should really catch the exception fro +m a failed open2. my $line = shift; local $/ = "\n"; local $\ = ''; chomp $line; $line =~ s/\r//g; # kill the hate $line =~ /\n/ and croak "newlines not allowed in arguments to Lingua +::Ispell::spellcheck!"; print Writer "^$line\n"; my @commentary; local $_; while ( <Reader> ) { chomp; last unless $_ gt ''; push @commentary, $_; }
From a debugger, I see that it hangs at the line:
while ( <Reader> ) {
By the by, I'm trying to write a litte script to check fields in a database for spelling errors and write out a log of misspelled words. Any ideas on what I'm doing wrong? ( I can't even run the example code for these modules without hanging. Oh, I'm on a Windows machine and cannot use a real operating system.)

Replies are listed 'Best First'.
Re (tilly) 3: need help with open2, please!
by tilly (Archbishop) on May 03, 2001 at 07:06 UTC
    It sounds like you are Suffering from Buffering. Though not exactly. Anyways you are expecting output while it is expecting input, and nobody is going to be happy.

    What happens if you try to close Writer before reading? Once it finds out that there will be no more input, you should get your output and be happy...

      call select on those file handles to prevent the reader/writer dead-locking. (Or just fork into seperate reader/writer process if that's feasible).
        I thought about suggesting select to turn off buffering, but didn't recommend that directly because it is less reliable.

        What select would do is affect how you are buffering your communication to the other process. But you cannot with select guarantee that it won't buffer the input coming from you, in which case you are still SOL. But when you close the handle, your contents get flushed and it will know from the eof that there is no more to buffer.

        Incidentally this is the basic danger with open2 and open3. You have to be very careful that your assumptions and the assumptions of the other program do not conflict in any way. For instance the above pattern (send, close, then read the response) works fine for small amounts of data. But if you try to send more than a page or 2 at once, it may decide to send you a response to the first bit before getting more from you. But you are still trying to send information. When this happens to you at a party, it is only mildly annoying. Humans being multi-tasking, one or the other usually spots the problem and the issue is resolved. But programs are willing to block on communication over this kind of boneheaded mistake for as long as it takes...

      I edited the module code to close Writer before reading, but no luck. I'm afraid I may be dealing with limitations of the ispell compiled for Windows NT - but this is just a guess from reading some of the docs on open2... Thanks though.