5

I have a Perl CGI script that emits different HTML apparently randomly. None of the inputs change. E.g., I will run wget twice and get two different results. The CGI is backed by a development database that, too, doesn't change.

I have a debug statement that informs me that the same number of elements are returned from the DB into the script.

Frankly, I'm mystified. Logic, DB, and inputs don't change, but the output does.

The http server is apache2 on Ubuntu 10.04. Perl version is perl 5.10.

edit: whenever I run it from the command-line on the server, the output is correct.

edit2: some of the bad runs seem like prior versions of the script. I don't think Apache is caches CGIs, but it seems like it might be grabbing out-of-date cache versions....

Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
  • 2
    Without code to examine, no one will have any idea what the issue is. – Richard Simões Jul 02 '10 at 21:18
  • I'm not sure what part of the code here would matter, and I'm not able to dump the entirety of the code out here. – Paul Nathan Jul 02 '10 at 21:21
  • 1
    you don't happen to have `$[ = rand` in your code anywhere do you :) – Eric Strom Jul 02 '10 at 21:24
  • more seriously, have you tried dumping all of the environment and server variables to see if any of those are changing? – Eric Strom Jul 02 '10 at 21:25
  • @Eric: Hmm, good point. I hadn't thought to do that - haven't written Perl CGI before really. :-) – Paul Nathan Jul 02 '10 at 21:29
  • @Eric: I dumped %ENV out and there's no difference between a good run and a bad run except for the port number, which IMO is irrelevant. – Paul Nathan Jul 02 '10 at 21:36
  • with hard to find bugs like this, sometimes the fastest way is to start littering the code with asserts – Eric Strom Jul 02 '10 at 21:51
  • See [How can I troubleshoot my Perl CGI script?](http://stackoverflow.com/questions/2165022/how-can-i-troubleshoot-my-perl-cgi-script) – Ether Jul 02 '10 at 21:59
  • 1
    This question is vague and does not have enough information. You haven't stated what the inputs are, how they've changed, or what is being performed by them. --- What modules are being used? What encoding is being done? What charset is your database in? If the inputs are straight from the database, SQL could be the culprit, since ordered results aren't returned unless you use the ORDER BY clause. --- We need MORE information in order to help. – vol7ron Jul 02 '10 at 22:32
  • @voltron: modules: dbi, cgi, and a hand-written module to render the html from the sql. Encoding is "standard". Database is mysql. results are not requested to be ordered. The data comes from the database correctly, I've checked that. Possibly it has to do with the hand-written module, but it a 'pure' module, and does not depend upon elements out of my control. I was hoping that it would be a simple, "oh yes, this happens when for new people when they don't set the XYZ flag". – Paul Nathan Jul 02 '10 at 23:51
  • @paul, you still need to include code. at least give an example of two/three different outputs. – vol7ron Jul 03 '10 at 14:08

2 Answers2

5

Is your CGI script being run using Apache's standard CGI API or are you running it under mod_perl using the Apache::Registry (or ModPerl::Registry in Apache2) CGI emulation layer?

I have seen an effect similar to the one you describe, which results from the way mod_perl's CGI emulation works. The details are discussed here.

One workaround is to take any 'global' variables declared at the start of the script with 'my' and change the keyword 'my' to 'our'.

Of course your problem may be something completely different - it's very hard to say without more information.

Grant McLean
  • 6,898
  • 1
  • 21
  • 37
  • Hmmmmmm. I may have some weird interconfict going on there related to the data the link describes. I'll disable mod_perl(it's just a plain CGI script) for a while and see if that works. – Paul Nathan Jul 06 '10 at 17:54
  • The details in the linked discussion worked for me but I had to use the declaration of `local our $counter = 0;` for example so that the value is removed when the handler subroutine exits. Each successive call to the Perl script after that started "fresh". – TadLewis May 23 '18 at 16:04
0

Caching: Apache probably isn't caching, but your browser may be. Turn off caching (set your browser cache to 0MB).

Command line: If your output differs from webpage to CLI execution of the script, then it seems like either you're missing header information, or the HTML tags are not complete or broken. For example, you need the Content-Type statement with two newlines after.

If it works on CLI but now through CGI on the webserver, it has to be your code, a missing module in Apache, or something like that.