IT452 - Debugging Tips
You should try all of these before banging your head against the wall.
Use Chrome to debug. Its tools are better. The following assumes Chrome.
- Look at the Error Console. Right-click your page (anywhere), select "Inspect Elements". Then click the "Console" tab. Are there errors in red? Look at the line number on the error, look in your code to see if you see what is wrong. If there is an error but you can't tell from the code what is wrong, see below.
- Use alert statements. You know what line the error occurs on, so put an alert("debug here") statement immediately before that line to print something useful. Most likely this means printing a variable to see what its value is.
- Use the "Elements" tab in the debugging console. Right-click your page (anywhere), select "Inspect Elements". This brings up the Elements and you can navigate your page's DOM tree. You can find everything in there, so if you think your code should have created a <p> tag, try to find it! It may have appeared in a different place in your DOM than you expect, and this will help you discover that.
- Click the Perl script in Network tab. If Ajax sent the request, it showed up in the Network tab. Click your perl script there. It will now show on the right your "Headers" and your "Response". Look in the Headers and see if everything looks normal. A "Status Code" of "200 OK" means your Perl was called and it returned something valid. You also want to look at the first line, "Request URL" and lower down, "Query String Parameters" to see what your ajax code sent to your script. If that looks ok, click the "Response" tab. This shows you exactly what your Perl script returned to Ajax. If it is blank, then it returned nothing.
Perl is not working.
- Run Perl from ssh. It is time for real debugging. ssh into the webserver (intranet.cs.usna.edu). You can use putty or any other ssh client. 'cd' into your public_html/LabXX directory. Now run your perl script. Were there errors?
- /usr/bin/perl^M: bad interpreter: No such file or directory: Did you see this error? Anytime you see a ^M character, that means your file is in DOS-mode and has carriage return characters that unix doesn't like. You need to change from DOS-mode to UNIX. In Notepad++ you just go to Edit->EOL Conversion and click the UNIX button.
- /usr/local/bin/perl: bad interpreter: No such file or directory: The first line of your script is pointing to the wrong perl location. Make sure it is "/usr/bin/perl".
- Global symbol "$databaseHandle" requires explicit package name at ./allResults.pl line 11: This usually means you are declaring a variable without using the "my" specifier. Make sure it is "my $var =" and not just "$var =".
- Permission denied. Your file permissions are just set incorrectly. "chmod 755 " to turn them on. Try running it again.
- Perl runs from ssh, and even outputs with no errors.: Great! You are almost there. This is probably a problem with the header that you are printing first in your script. Remember, you need a header to tell the web browser what type of text you are outputting. If you are printing XML out, the very first print statement should be:
print( "Content-type: text/xml; charset=UTF-8\n\n");
If you are printing just text (no XML or HTML), the very first print statement should be:
print( "Content-type: text/plain; charset=UTF-8\n\n");