PDA

View Full Version : Perl programming question


simX
Jul 23, 2003, 05:31 PM
Does anybody know what would cause a perl program to spontaneously not print out things that it's supposed to? I'm currently making a perl script, and for some reason it just seems to ignore the first few print lines of a specific subroutine in my program.

If you go here (http://ssrl.slac.stanford.edu/smbin/dataextractnew.pl), you'll note that it just shows a bunch of text, but it should actually be displaying a couple images and text inside a table. If you look at the source, it doesn't have a start HTML or BODY tags, and doesn't even have the HEAD tag even though I've explicitly issued a command to print that text.

The subroutine itself is horrendously long so I didn't particularly want to post it here, but I was wondering if anybody knew some places to check for this sort of error. There aren't any compilation errors, either -- it just ignores a bunch of print lines.

I'm about to start banging my head against the wall because of this problem. :\

Rower_CPU
Jul 23, 2003, 05:47 PM
How about just the code immediately surrounding the print statements for the beginning HTML, HEAD and BODY tags?

Use the PHP button to insert the code so it won't be commented out by the BB system.

crenz
Jul 23, 2003, 06:11 PM
There's loads of possible reasons. Can you show us some source code? Without source code, it is impossible to help you, I guess.

You might also try to get help at PerlMonks (http://www.perlmonks.com), a great Perl community. People there are very helpful (if you ask questions the right way).

simX
Jul 23, 2003, 06:38 PM
I've narrowed down the problem to a couple of lines in the code:

#print hidden("xminval",$xTickLabels{'0'}),hidden("xmaxval",$xTickLabels{'1'});
#print hidden("yminval",$yTickLabels{'0'}),hidden("ymaxval",$yTickLabels{'1'});

When I comment these two lines out (like they already are), everything displays fine. However, when I uncomment them (since they're necessary for the script to function correctly), the whole top part of the page just vanishes as if it were not printed at all.

Any ideas as to why these lines would cause this sort of problem?

(I would post surrounding code, but like I said, it probably wouldn't be of much use since it's so incredibly messy that I would have to explain everything.)

[EDIT: GRR! I explicitly printed out those values later on in the HTML, so I could see it in the part of the page that isn't cut off at the top. The values are valid! Moreover, these lines don't cause a problem when first loading the script, but they do once the user enters a value in a specific text box. I don't get it!]

simX
Jul 24, 2003, 12:33 PM
The other thing I've tried is to replace the CGI module calls with straight HTML, and I find that I can get one of those hidden fields in the HTML with no problem. However, as soon as I allow another one to be in the HTML, the top part of the page gets cut off again. I'm totally confused!

Could the HTML itself be causing the problem? Or is it likely something wrong with my programming?

*sigh* If you want to see the script in action, go here. Note that it works fine when it first starts up, but then when you enter something in the chemical element box (for example, say, "Fe") and submit the bottom form, the problem will occur. But if I hide three of those four hidden fields, everything works fine. The values are valid, too. So I just don't understand what in the world could be going wrong.

[EDIT: Hmm. It seems that it's not just those specific lines. Commenting out any three hidden fields will solve the problem. Is there a limit to how many hidden fields you can use in an HTML file?!? That would be lame.]

firestarter
Jul 24, 2003, 06:05 PM
My guess is that you're putting something strange into the hidden fields. If you did something strange like put a single quote in one, you might take everything to the next quote out of the print statement.

Are you using cgi tainting?

If you use a regexp to filter the user input, you could then make sure that the input is valid.

Just a guess, but in a case like this I always focus on the differences between each (working and not working) run of your code. The only difference here is the new hidden values you're using.

Good luck!!

simX
Jul 24, 2003, 06:33 PM
Originally posted by firestarter
My guess is that you're putting something strange into the hidden fields. If you did something strange like put a single quote in one, you might take everything to the next quote out of the print statement.

Are you using cgi tainting?

If you use a regexp to filter the user input, you could then make sure that the input is valid.

Just a guess, but in a case like this I always focus on the differences between each (working and not working) run of your code. The only difference here is the new hidden values you're using.

Good luck!!

See, the thing is, most of these hidden fields are there on the first run, anyway, which works fine. Commenting out lines that print any three hidden text fields make the script run fine on the part with which I'm currently having problems. So I don't think it can possibly be a problem with misplacing quotes or something like that.

This has got to be the most mind-boggling problem I've ever had.

P.S. Does anyone know how to get the CPAN installer to recognize a fink-install GD? That would help in testing this problem, so I could test it out offline (since I don't have Chart::Plot installed on my local machine, which is required for the script, and also requires GD to be installed). Chart::Plot keeps complaining that GD isn't there when it is.

simX
Jul 24, 2003, 06:52 PM
One of the very nice users over at perlmonks.com solved the problem! All I needed to do was to enable autoflush, by issuing a

$| = 1;

command at the beginning of my script (that's the pipe operator following the dollar sign).

Since I didn't know this, maybe it will help other people out, too. Here's (http://www.perldoc.com/perl5.8.0/pod/perlfaq5.html#How-do-I-flush-unbuffer-an-output-filehandle---Why-must-I-do-this-) a relevant link explaining what this does.