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

Hi,

I have a simple CGI script that prints out a header and a footer for an HTML page. Between the two it prints out some content which depends on a parameter.

Depending on the parameter passed, the script will 'require' one of three different files. Each of these files contains a number of print statements to display its content along with a number of calculations.

Now the problem is that sometimes the content is displayed and sometimes it isn't. Whether or not it is displayed seems random.

Any idea of what is going on?

Replies are listed 'Best First'.
Re: Using 'Require' to Generate Content
by Transient (Hermit) on Jun 01, 2005 at 22:02 UTC
    A couple of questions:
    • What webserver are you using?
    • Are you using mod_perl?
    • What OS is this?
    • What does the code look like?
    • What do the webserver logs say?
    • What happens when you run this from the command line?
    • Is there anything additional (proxying, caching, etc.) going on that might affect the display of content?
Re: Using 'Require' to Generate Content
by tlm (Prior) on Jun 01, 2005 at 22:12 UTC

    I'm not sure if this has anything to do with the problem you're having but, according to the docs for require:

    Note that the file will not be included twice under the same specified name.

    Maybe do would be better than require for this? Or better yet, make proper exporting modules out of these files, and define functions print_content (for example) in these modules to perform the desired printing; then your code explicitly calls the appropriate one among these functions after executing the require statement.

    the lowliest monk

Re: Using 'Require' to Generate Content
by metaperl (Curate) on Jun 01, 2005 at 22:52 UTC
    Both CGI::Application and CGI::Prototype offer a structured approach to the problem you are presenting. Why don't you take a look at these 2 solutions to your problem?
Re: Using 'Require' to Generate Content
by wazoox (Prior) on Jun 01, 2005 at 23:01 UTC
    It's hard to make a relevant suggestion without seeing any example of the code... However when a CGI doesn't display anything, it's often a header problem. Perhaps one of your modules sometimes send some HTML before your script has any chance to send the http headers first?
Re: Using 'Require' to Generate Content
by BUU (Prior) on Jun 02, 2005 at 00:04 UTC
Re: Using 'Require' to Generate Content
by goober99 (Scribe) on Jun 01, 2005 at 22:03 UTC
    Make sure there is a return 1; at the end of each of the files you are requiring. The file must be returned as true when the script goes to require it, or it will fail.
Re: Using 'Require' to Generate Content
by PerlingTheUK (Hermit) on Jun 01, 2005 at 22:25 UTC
    Do you say "Require" with those nice quotation marks because you mean you have to open some text files and evaluate their comments? If that is the case you might have some open statements that just dont get executed for whichever reason. But without any code if even examples only it will be a wild guessing game to help you.

    Cheers,
    PerlingTheUK