Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

CGI inconsistencies

by arrow (Friar)
on Apr 07, 2003 at 02:01 UTC ( [id://248510] : perlquestion . print w/replies, xml ) Need Help??

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

I work with CGI on the internet, and lately I've been noticing my scripts doing weird things. One of these inconsistencies is a variable containing the year is set near the top of the script, and printed near the bottom. But there is not value in the variable when it is printed, it's just empty. I've checked the scope and it seems to lose its value at the end of the enclosing foreach block, but it was declared earlier as a universal variable. I wouldn't be so miffed about this if it had always done this, but before this, it had worked perfectly for a least a month, then one day it broke, then the next day it worked again! Is this a problem with my scripts (I can't imagine what) or with the server (it's a free british server)? Or is it my connection, which is dial-up (I know, I know). Any help would be greatly appreciated.

Just Another Perl Wannabe

Replies are listed 'Best First'.
Re: CGI inconsistencies
by Pardus (Pilgrim) on Apr 07, 2003 at 03:06 UTC
    Did you try it with use strict ? Scope can be more odd then you imagined without this.
    --
    Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<
Re: CGI inconsistencies
by graff (Chancellor) on Apr 07, 2003 at 02:56 UTC
    I'm not sure how one would diagnose this behavior without seeing some of the code, or (preferably) some snippet that demonstrates an equivalent behavior.

    Is your script running on a server where you can look to see what perl version is pointed to by the path on the shebang line? Do you have control over (or up-to-date info about) the perl installation and modules on the server? If not, try including something like print $] in your script, to see the perl version somewhere on the page that it creates; maybe someone was trying a different perl version for a day or so, then reverted or installed a patch (or did it right the second time)...

Re: CGI inconsistencies
by diotalevi (Canon) on Apr 07, 2003 at 03:36 UTC
Re: CGI inconsistencies
by DrManhattan (Chaplain) on Apr 07, 2003 at 10:59 UTC
    Are you declaring a lexically scoped variable at the top of your script and then printing it out in a subroutine? E.g.
    my $date = localtime(); &print_date(); sub print_date { print "$date\n"; }

    If the server you're on runs mod_perl, this could cause the symptoms you describe.

    -Matt

Re: CGI inconsistencies
by benn (Vicar) on Apr 07, 2003 at 09:32 UTC
    As noted above, it's always difficult to be precise without sufficient data, so here are a couple of general principles that I always try to bear in mind in these situations...
    1. It's pretty much always something you (or at least somebody you work with) did. Along with just about every other programmer I know, I spent a great deal of my first few coding years looking for 'bugs' in the database, the OS, the server, the libraries - anything but my own code, which I knew must be right :) After a while, it began to sink in that all these things were probably tested and refined rather more than my code was.
    2. Again, it's nearly always whatever you last did. Code that's been running for a while tends to keep running if you don't touch it. As soon as you open a file for editing - that's when it breaks :) Even something as simple as d/loading from a Unix server to a Windows machine, just *looking* at the code, then saving it out again can break things unless you've got all your FTP line endings stuff sorted out. If you haven't touched the script recently, check all the last-modified times for your script and its dependants modules anyway - *something* will have changed.

    Hope this helps
    ben
Re: CGI inconsistencies
by arrow (Friar) on Apr 08, 2003 at 00:12 UTC
    I know, I tried all those things. I have strict, and CGI:Carp for printing errors to browser, and I know what the error for lexically scoped variables looks like. The thing is, my script worked for some time. Then, one day, is suddenly didn't have the value for the variable anymore. The variable was empty, not outside of its scope. The weird thing is that the next day, everything was fine again. To me, this doesn't seem like a problem with my scripts. And no, I don't have access or control over anything my host does. By the way, I looked and the host has the newest version of Perl installed, so...

    Until Next Time...

    Just Another Perl Wannabe
      I looked and the host has the newest version of Perl installed, so...

      So, the script is working now (after not working for a day) without ever being modified? Does the variable's value depend on input from the cgi form? Are you worrying that it might happen again? Were you hoping we could provide some answers, or just venting about something that seems inexplicable? (Is this too many questions, and we should just drop it?)

      It could be interesting to know when this newest version of perl was installed on that server -- you could run "perl -V" in backticks and print the line that says "Compiled at ..."; indeed, print all the output, because there are many relevant facts provided there (e.g. whether your notion of "newest version" matches someone else's), and unless you provide at least some details of that sort, it's kind of pointless to give the appearance of asking for help.

        Well, mainly because, 1., I wanted an explanation as to the inconsistencies, and 2., Yes, I am afraid it's going to happen again, and would like to know how to fix it (or at least how to avoid it. Until next time...

        Just Another Perl Wannabe