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

Hi..

I have an intersting little problem. I have some code that checks the spelling of a line using the Ispell program. I have a print statement at the top of my program that simply prints out "Content-type: text/html\n\nbeginning". Here's the code that spell checks: for my $r ( Lingua::Ispell::spellcheck($spellline) ) { ... }
This sends a line to the spell checking program and returns mispelled words etc.. When I exact this program from a command prompt everything works fine. I get the output "Content-type:text/html\n\nbeginning.." and then my misspelled words.
However, when I access the script through a web browser it prints out the lines below:

Content-type:text/html\n\n beginning.
any mispelled words
Content-type:text/html\n\n beginning.
any mispelled words.

Somehow it executes twice! When I call that Lingua::Ispell::spellcheck function somehow my program gets tossed to the first line and runs through itself again. Why would running the script through a web browser produce this behavoir?
Thanks,
Tom

Replies are listed 'Best First'.
Re: Repeated lines
by chipmunk (Parson) on Dec 23, 2000 at 01:31 UTC
    I'm not sure why your code behaves this way when executed as a CGI script (I'd want to see more of the code to figure that out), but I will point out that you are not using the right content-type. You're printing a content-type of text/html, when your content is actually text/plain.
      Here is the entire code.. The code works.. It's just that fact that it seemingly executes twice via a web browser like I mentioned before.
      #!/usr/bin/perl <BR> use Lingua::Ispell;<BR> print "Content-type: text/plain\n\n";<BR> print "beginning\n\n";<BR> $line="delte five";<BR> spellit();<BR> sub spellit<BR> {<BR> $spellline=$line;<BR> $realine=$line;<BR> for my $r ( Lingua::Ispell::spellcheck($spellline) ) {<BR> print "right after <BR>$spellline\n ";<BR> if ( $r->{'type'} eq 'miss' ) {<BR> print "$r->{'term'}";<BR> } <BR> } <BR> }<BR>
      This is the output via web browser activation:
      beginning Content-type: text/plain beginning right after delte five delte


      Output after running via command prompt:
      beginning Content-type: text/plain beginning right after delte five delte
        I'm a little confused... In your original post, you said that, when run as a CGI, the program output everything twice. But in the sample output you posted in your second node, you show Content-type: text/plain beginning output twice, but the list of mispelled words output only once.

        If the latter is the case, then I suspect that Lingua::Ispell is forking off a subprocess to handle the spell-checking, before the output buffers have been flushed, and so everything that was printed before the fork gets output twice.

        The solution is to turn on autflushing, by setting $| = 1 at the top of your program, before you print anything.

        (I believe that the latest version of Perl avoids this problem by automatically flushing output buffers when it's about to fork.)