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

I have a Perl script that is called from multiple other scripts. Sometimes the calling script has already sent the HTTP header and when my script does this it gets the header displayed in the browser. Some of the calling scripts have not sent the header so my script is required to do so. How can I tell if the header has already been sent? Thanks for your help.

Replies are listed 'Best First'.
Re: Don't reprint header
by ikegami (Patriarch) on Jan 14, 2011 at 18:36 UTC
    Well, how can you tell if you told someone something?
    • You can remember whether you told them or not.
    • You can ask them if you told them or not.
    • You can observe for the effects of having told them.

    There's no way to do the last two, so that leaves the first. You need to keep track of it. (If you're using some library or framework, it may track it for you.)

    It's a bad design to output all over the place. Your problem is the consequence of using that design.

    Update: Fleshed out my reply.

      The fourth way, as you suggest at the very end, is to:

      • Have a policy of not telling people anything until just before you go home for the day.

      Not so good in social situations, but it works great for web pages.

        Not really, it just shifts the question from being about the message to being about the day.

        And these shifts are quite common in social situations. (I'll publish my report at the meeting. I'll discuss my plans after I nail down some details.)

        What I suggest at the end is really a means of tracking it. (Code X never tells, so I know that code X didn't tell.)

Re: Don't reprint header
by TomDLux (Vicar) on Jan 14, 2011 at 21:08 UTC

    Suggestion 1 - provide a command line option to have the program print the header. That way, scripts can ask it to provide the header, if it hasn't been printed yet, or tell it to not bother, if it already has been handled. The calling script might even want one version in one situation, and the other in another.

    Suggestion 2 - have the program return a text string to the calling cgi, rather than sending it to the remote client. That way the invoking cgi script can determine what happens. In an extreme case, you might merely want to know how many characters were used to represent the message.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Don't reprint header
by molecules (Monk) on Jan 14, 2011 at 20:34 UTC

    You are asking the wrong question. Checking a log file on the server at this point is like talking and then stopping in mid sentence to ask yourself what you had already said earlier in the sentence.

    The browser can ask your server for information and then your server can send a response. You are responsible for building up a complete response without any additional feedback from the browser (which isn't available anyway).

    What you are talking about is building up this response from various scripts. If you don't have control over the whole process, then you need to talk to whoever does have control over it so that you can coordinate.

    UPDATED

Re: Don't reprint header
by erpence (Initiate) on Jan 14, 2011 at 18:53 UTC
    I really don't know which program may have already printed a header. This is something I have to figure out for myself. Isn't there a way to get this information from the server? Or cashed somewhere in the browser (the calling programs would be in the same browser session)?

      Isn't there a way to get this information from the server?

      You are the server. Something in your framework may track it for you, but you didn't mention any of the components.

      Or cashed somewhere in the browser

      There's no way to ask the browser if you told it or not. There's no way to observe the browser for the effects of having told it.