CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Basic debugging question...

 



Merlin
journeyman

Jan 24, 2001, 4:56 PM

Post #1 of 7 (1089 views)
Basic debugging question... Can't Post

Seems my earlier post disapeared! Here we go again! I'm getting those darn "internal server errors" even when using a use CGI::Carp 'fatalsToBrowser'... What's your way of finding what's wrong with your scripts when you always get those "internal errors"?

Example, and this one has me dumbfounded... I've been writing some beginner to advanced scripts in the last six months and can't figure this one out (it's probably one of those small, insignificant errors I can't see...). The code:

This next one works (test.cgi) :
#!/usr/bin/perl

use CGI qw/:standard/; use CGI::Carp "fatalsToBrowser";
$objetcgi = new CGI;

print $objetcgi->header;
print $objetcgi->start_html;
print "Ceci est un test!";
print $objetcgi->end_html;

Of course it works! It's so simple! So, I had another script that used user input to build a html report page. The thing is, it worked fine this morning. Then, out of nowhere, I get internal errors from the server! I decided to comment out parts of the script until I found where it hit the snag. I commented out the WHOLE script and it still gave me errors, I tried patching the small script I just showed in the script so it looked like (called miseajourfiche.pl) :

#!/usr/bin/perl

use CGI qw/:standard/; use CGI::Carp "fatalsToBrowser";
$objetcgi = new CGI;

# Script de mise a jour et d'affichage de la fiche d'analyse.

print $objetcgi->header;
print $objetcgi->start_html;
print "Ceci est un test!";
print $objetcgi->end_html;
# rest of script commented out
#

and it DOESN'T work!!! 8P I can't seem to know what the difference is between the scripts that makes the first one work and the other doesn't... Any help appreciated. Thanks!

Merlin



kencl
User

Jan 25, 2001, 2:00 AM

Post #2 of 7 (1083 views)
Re: Basic debugging question... [In reply to] Can't Post

I put this in all my scripts:

Code
# Allows you to troubleshoot script crashes without have to have access to the server logs 
BEGIN { open (STDERR, '>CGIerrors.log'); }

Then you can just read the errors log for yourself to see what happened.

If you want a record of crashes, just change write to append (> to >> in the open statement).

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


Merlin
journeyman

Jan 25, 2001, 8:07 PM

Post #3 of 7 (1078 views)
Re: Basic debugging question... [In reply to] Can't Post

Thanks for the info but it doesn't seem to work since I still get internal errors. I just put it after #!/usr/bin/perl but doesn't the open need to write to the file and not just open it? Am I missing something here? Thanks!



kencl
User

Jan 26, 2001, 10:31 AM

Post #4 of 7 (1068 views)
Re: Basic debugging question... [In reply to] Can't Post

Put a dash c on your shebang line. You're browser will still display a script error, but you should be able to open CGIerrors.log to find out if it compiled successfully or not, and if not, why.

Code
#!/usr/bin/perl -c

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


modified
Deleted

Jan 26, 2001, 1:33 PM

Post #5 of 7 (1064 views)
Re: Basic debugging question... [In reply to] Can't Post

Ken. I put the BEGIN thing in a test script, and I added -c to the shebang, but I get a 500 error. Does that only work on NT or something?



rGeoffrey
User / Moderator

Jan 26, 2001, 8:44 PM

Post #6 of 7 (1059 views)
Re: Basic debugging question... [In reply to] Can't Post

I also reset STDERR at the beginning of my scripts, and it usually works great. But there are two potential problems.

1) You don't have permission to write to the file you try to open. You are attempting to open STDERR, but if it generates an error, where would the error message get printed? Because there is no STDERR to write to.

2) You don't turn off buffering, then the program can die and there will be error messages waiting for STDERR that have never been printed.


Below is the function I call. I have all my error scripts live in one directory so you will notice the two lines that set $dir. You might want to change them to point to your favorite place for the error scripts. I also turn off the buffering for both STDERR and STDOUT so errors and my HTML print as soon as possible. And because I have closed STDERR, if the open doesn't work I call &Log_File_Error to print the message to the browser.


Code
sub Flush_Buffers_Quickly 
{
my ($file, $append) = @_;

if ($file) {
my $dir = $ENV{DOCUMENT_ROOT};
$dir =~ s|virtual_html|log/scripts|;
unless ($append) {
$dir = ">$dir/$file";
} else {
$dir = ">>$dir/$file";
}
close (STDERR);
open (STDERR, $dir) or &Log_File_Error ($file);
}
select (STDERR); $| = 1;
select (STDOUT); $| = 1;

} #Flush_Buffers_Quickly

sub Log_File_Error
{
my ($file) = @_;

select (STDOUT); $| = 1;

#///////////////////////////message
print STDOUT <<EOF;
Content-type: text/html

<html>
<head>
<title>Problem with log file</title>
</head>
<body>
<center>
<h1>ERROR!!!</h1>
This program wants to remap STDERR to a log file '$file'. But it died trying
to open the file '$file'.
<p>
The most likely cause is a lack of write permission on the log file.
<p>
Please check the permissions and then try again.
</center>
</body>
</html>
EOF
#///////////////////////////message
exit (0);

} #Log_File_Error

$file and $append are optional. If you don't send any values, then the buffers are turned off, but STDERR is not changed. I generally don't include $append and have only the last run of the script in the error file, but that can be changed to keep all the data around. The choice is yours.

---
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


Merlin
journeyman

Jan 27, 2001, 2:41 PM

Post #7 of 7 (1054 views)
Re: Basic debugging question... [In reply to] Can't Post

Well, I tried the -c and it didn't work. Next, I tried the script to Flush buffers and it didn't work either... I tried calling the sub at the top of my script and also enclosed the sub call in a BEGIN with the same 500 error and no error logs written... This is a bit alien to me so I'm confused. Am I supposed to have the CGIerrors.log somewhere? I looked for it in various dirs (like cgi-bin) and didn't see it anywhere (before and after running the various error catching schemes). I tried passing a dir to the sub that has 777 permissions so I could write the file with no better results. Any other help would be good so I could sleep a little less dum tonight... ;) Thanks!


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives