http://qs321.pair.com?node_id=154295

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

I'm about 95% certain I've seen the answer to this somewhere in the Monastery. Either my skills at using Super Search have greatly declined, or I dreamed it.

I have a script (which will eventually be CGI). When I execute it from the command line, the output is:

<HTML> <HEAD> <TITLE>test page</TITLE> <STYLE TYPE="text/css"> BODY {background: white;} H2.std-header {color: white; background-color: #006699;} H5.std-header {color: white; background-color: #006699;} P.std-title {color: #006699; background-color: #CCCCCC;} A {color: #006699;} A.header {color: #FFFFFF} EM {color: #006699;} KBD {font-family: serif; color: #006699;} PRE.code {color: #006699;} STRONG {color: #006699;} </STYLE> </HEAD> <BODY> <CENTER> <H2 CLASS="std-header">test page <H5 CLASS="std-header"> <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%"><TR> <TD WIDTH="20%"><A HREF="/shock/index.php" CLASS="header">Main</A></TD +> <TD WIDTH="20%"><A HREF="/" CLASS="header">Music</A></TD> <TD WIDTH="20%"><A HREF="/shock/linux/index.php" CLASS="header">Linux< +/A></TD> <TD WIDTH="20%"><A HREF="/shock/perl/index.php" CLASS="header">Perl</A +></TD> <TD WIDTH="20%"><A HREF="/family/index.php" CLASS="header">Family</A>< +/TD> <TD WIDTH="20%"><A HREF="/shock/links.php" CLASS="header">Links</A></T +D> </TR></TABLE> </H5> </H2> </CENTER> Test #1<br>2002-03-25<br>Various<br> This is a test. It might someday +have something interesting. <H2 CLASS="std-header"> <H5 CLASS="std-header"> <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%"><TR> <TD WIDTH="20%"><A HREF="/shock/index.php" CLASS="header">Main</A></TD +> <TD WIDTH="20%"><A HREF="/" CLASS="header">Music</A></TD> <TD WIDTH="20%"><A HREF="/shock/linux/index.php" CLASS="header">Linux< +/A></TD> <TD WIDTH="20%"><A HREF="/shock/perl/index.php" CLASS="header">Perl</A +></TD> <TD WIDTH="20%"><A HREF="/family/index.php" CLASS="header">Family</A>< +/TD> <TD WIDTH="20%"><A HREF="/shock/links.php" CLASS="header">Links</A></T +D> </TR></TABLE> </H5> Test Page </H2> </CENTER> </BODY> </HTML>
This displays perfectly - exactly as I was hoping it would appear. However, when I execute this same script through my browser, only the following appears:
Test #1<br>2002-03-25<br>Various<br> This is a test. It might someday +have something interesting.
Nothing else is printed to the browser. The script which produces this is:
#!/usr/bin/perl -w use strict; use exitwound::BuildLink qw(&buildLinks); my ($NodeTitle, $NodeDate, $NodeSection, @words) = buildLinks(1); ######################## # BEGIN STANDARD HEADERS ######################## require("HEADER01.pl"); require("standard.css.pl"); require("HEADER02.pl"); require("shock-header-links.pl"); require("HEADER03.pl"); ######################## # END STANDARD HEADERS ######################## print "$NodeTitle<br>"; print "$NodeDate<br>"; print "$NodeSection<br>"; print @words; # print "test page"; ######################## # BEGIN STANDARD FOOTERS ######################## require("FOOTER01.pl"); require("shock-footer-links.pl"); require("FOOTER02.pl"); ######################## # END STANDARD FOOTERS ########################
Each of the require statements calls a script which produces the appropriate output. Again, from the command line, I get what I'm expecting, but from the browser, I only get the information contained within the print statements. It's as though, when called as a CGI script, the print statements within the require'd scripts are being routed to some place other than STDOUT.

I think I must be missing something obvious, but missing it nonetheless. As always, any advice is most welcome.

Thanks.

If things get any worse, I'll have to ask you to stop helping me.

Replies are listed 'Best First'.
(Ovid) Re: Command Line Output Different from Output to Browser
by Ovid (Cardinal) on Mar 26, 2002 at 02:13 UTC

    I don't see where you are printing HTTP headers. If the command line output that you listed is complete, then that is your problem. However, since you appear to be getting something in the browser and since your footers don't show up, I would follow up on chromatic's suggestion.

    In the meantime, I have to point out that your site's architecture is going to make debugging difficult. If you are printing a header, how are you doing it? I can't tell? The first obvious print is this one:

    print "$NodeTitle<br>";

    If your headers are embedding in that, this is most confusing. If they aren't there, then you're printing headers when you use or require one of your modules, or printing them as a side effect of &buildLinks. I have no idea how anyone can reasonably keep track of that. In fact, I'm guessing that's what is going on as you claim your output is correct, so I can only speculate that when you require your footers that you are actually printing them at the same time. Having a module dump stuff to STDOUT when you use it is atypical behavior and is the sort of "action at a distance" that causes problems. It will be especially troublesome if you ever need to customize the headers for a particular page.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      The first 5 require statements produce the following:
      <HTML> <HEAD> <TITLE>Various :: Test Node #1</TITLE> <STYLE TYPE="text/css"> BODY {background: white;} H2.std-header {color: white; background-color: #006699;} H5.std-header {color: white; background-color: #006699;} P.std-title {color: #006699; background-color: #CCCCCC;} A {color: #006699;} A.header {color: #FFFFFF} EM {color: #006699;} KBD {font-family: serif; color: #006699;} PRE.code {color: #006699;} STRONG {color: #006699;} <STYLE> </HEAD> <BODY> <CENTER> <H2 CLASS="std-header">Various :: Test Node #1 <H5 CLASS="std-header"> <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%"><TR> <TD WIDTH="20%"><A HREF="/shock/index.pl?NodeID=1" CLASS="header">Main +</A></TD> <TD WIDTH="20%"><A HREF="/" CLASS="header">Music</A></TD> <TD WIDTH="20%"><A HREF="/shock/index.pl?NodeSection=linux" CLASS="hea +der">Linux</A></TD> <TD WIDTH="20%"><A HREF="/shock/index.pl?NodeSection=perl" CLASS="head +er">Perl</A></TD> <TD WIDTH="20%"><A HREF="/family/index.php" CLASS="header">Family</A>< +/TD> <TD WIDTH="20%"><A HREF="/shock/links.php" CLASS="header">Links</A></T +D> </TR></TABLE> </H5> </H2> </CENTER>
      I realize using the require statements introduce certain maintainability issues. My ultimate goal is to be able to make a change in one place and have that change reflected throughout the site. My current approach may not be "the best way to do it," but as I (attempted to say | said) at the outset, this is more a learning exercise than an actual production system.

      If things get any worse, I'll have to ask you to stop helping me.

        The problem here is in the HTML. It will probably looks ok in Internet explorer but not in Netscape. You need to close H2 and remove the H5 tag before the table and remove the close h2, h5 and center tags at the end...
        Oops. Okay, invalid HTML. I can correct that. However, even under Internet Explorer, it's never being output to the browser. If I move the print statements from the require script to the main script, the print statements work (invalid HTML and all). However, when print from within the require script, it never makes it to the browser.

        That's the real point of this thread (I hope). Why is the script operating differently when I move the print statements?

        Again, this is just a learning exercise for me. I don't care what the answer is. I just want to know what the answer is.

        If things get any worse, I'll have to ask you to stop helping me.

Re: Command Line Output Different from Output to Browser
by chromatic (Archbishop) on Mar 26, 2002 at 01:48 UTC
    Wild guess: the path set in your web server is substantially different from the command-line path, and several required files aren't available.

    I'd print the variables you're expecting after each require(), just to see what's there.

      I thought the same. The scripts are in ./ - but just to be sure, I tested with the full path name to the scripts (i.e., require("/www/htdocs/cgi-bin/......"). The result is the same.

      If I remove the require statements and cut-and-paste the contents of the require'd scripts into the main script, it works fine. There seems to be something peculiar about printing from the require'd script that's escaping me.

      If things get any worse, I'll have to ask you to stop helping me.

        It could be permission problem. When CGI is run it often runs with another UID. Check permissions of required files.

        --
        Ilya Martynov (http://martynov.org/)

Re: Command Line Output Different from Output to Browser
by stephen (Priest) on Mar 26, 2002 at 02:12 UTC
    One potential problem: you don't print the CGI header at any point.

    Try putting:

    print "Content-type: text/html\n\n";
    or better yet:
    use CGI qw(standard); print header();
    at the top of your code, before you print anything.

    stephen

Re: Command Line Output Different from Output to Browser
by dws (Chancellor) on Mar 26, 2002 at 01:58 UTC
    You say that the script will eventually be a CGI. If it isn't a CGI yet, what exactly do you mean by "execute this same script through my browser"?

    As an aside, your example cries out for using a templating mechanism, such as Text::Template.

      Sorry for the confusion. It's CGI.

      If things get any worse, I'll have to ask you to stop helping me.

Re: Command Line Output Different from Output to Browser
by weini (Friar) on Mar 26, 2002 at 11:21 UTC
    I agree with Stephen: You have to tell your browser what kind of data is coming over the line.

    As far as I can see you start your HTML-Output just with <HTML>.

    weini