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

Hi,

I have some CGI perl scripts that works fine sometimes and give a blank page anothertimes. It seems as if, somehow, they have lost their STDOUT pointer or someone else has jus closed it, as I just print my output to STDOUT and I get nothing back. Is there a way to reopen STDOUT (as its a CGI script it must point to the open socket and of course, I canīt use select to retrieve the old filehandle) or at least to test if its open and to see where the filehandle points to? Checking my error log file gives nothing.
Using Perl 5.8.0 on Linux 2.4.20

Thanks in advance,

Ricardo Aguilera

Replies are listed 'Best First'.
Re: Lost STDOUT?
by ikegami (Patriarch) on Nov 04, 2004 at 20:48 UTC
    The fact you're not getting a 500 error indicates your script sent a header, so that means STDOUT was fine when your script started. I'd look elsewhere for the problem.
      Hmmm, thats correct. My code was as simple as
      print <<P1; Content-type:text/plain <some stuff> P1
      And it works fine sometimes, gives a blank page another times. Maybe this is an http server problem.

      Thanks to all again,

      Ricardo

Re: Lost STDOUT?
by tachyon (Chancellor) on Nov 04, 2004 at 22:29 UTC

    The key to your problem will be in the error logs.

    I will just reiterate what others have said. You won't have *lost* STDOUT. If your script does not print a valid header to STDOUT then you *will* get a 500 Internal Server Error. The fact that you get a blank screen means you have output a header but that your script has not sent anything (to STDOUT) after that header.

    The possible reasons are that it dies somewhere or you have a logic error. The answers will lie in the error logs +/- adding some debugging warns (these will appear in the error logs to) so you can trace program flow. If the error logs are empty (as you say they currently are) first add a line like warn "Sanity Check!\n" and make sure you see it in the error log. If you don't see it find the correct logs (look in httpd.conf*). If that works so you truly know you have no logged errors then you have a logic error that leads to your script exiting cleanly without actually doing what you expect. *Note you can have multiple httpd.conf files lurking on a system, sanity check you are looking at the right one by adding a syntax error and restarting the server (it won't start if it is the active file).

    See CGI Help Guide.

    cheers

    tachyon

Re: Lost STDOUT?
by welchavw (Pilgrim) on Nov 04, 2004 at 21:04 UTC
    I'll defer to this link. I can't assert its correctness, but the source is pristine.

    http://groups.google.com/groups?q=reopen+STDOUT&hl=en&lr=&selm=m14skhbfak.fsf%40halfdome.holdit.com&rnum=6

    Addition

    As an aside, if you were not doing CGI and could count on your code being launched from a commandline shell, you *might* be able to get away with something like this.

    #!/usr/bin/perl -w use strict; use AtExit; $_ = atexit(sub { open STDOUT, ">/dev/tty" or die; print "bar\n"; }); close STDOUT; exit 0;

    But that's not a great approach. Caching the file descriptor is a better approach, but as prior poster noted, this probably isn't the problem anyway ... its just slow at work today and I thought I'd dummy some code up and post.

    ,welchavw

Re: Lost STDOUT?
by csuhockey3 (Curate) on Nov 04, 2004 at 21:09 UTC
    I don't think it's STDOUT, A list of HTTP errors with explanations might be helpful to you. Do you have any code you can post?