Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Tips for Convenient CGI Scripting

By Eugene Logvinov
2005-04-28


Displaying script errors in the browser

Displaying the syntax and runtime errors on the browser screen can be a big help when you're debugging code. In our case, the scripting cycle will consist of editing script files, saving them, and reloading the browser screen. Let's take a look at how it works.

Listing 2. The most complete debug printing code implementation

#!/usr/bin/perl -Tw

use strict; #restrict unsafe constructs
use CGI ':standard';
use CGI::Carp qw/carpout fatalsToBrowser set_message/;
use diagnostics -verbose; #print warning diagnostics

BEGIN {
local *LOG;
my $size.= -s "my.log" || 0;
open LOG, ">>my.log" or die "Can't open: $!";
carpout(\*LOG);
my $errors.= 0;
sub handle_errors #will be called with the text of the error
{
$errors.= defined $_[0] && $_[0] || $errors, $size
}
set_message(\&handle_errors);
}

END {
my($errors, $size).= handle_errors;
if($errors)
{
local *LOG;
local $/.= undef;
open LOG, "my.log" or die "Can't open: $!";
seek LOG,$size,0; #skip previous error log
local $_.= <LOG>;
close LOG;
s/&/&/g; #replace special characters
s/"/"/g;
s/>/>/g;
s/</</g;
print "<table><tr><td bgcolo.=linen><pre styl.='color:black'>";
print "<b>$errors</b>
$_</pre></td></tr></table>";
}
}

print header,start_html('Test page'),'test',end_html;
Now you've seen an example of the most complete and popular approach (according to the modules used) to code debugging. Although all of the techniques will hardly be used simultaneously in normal circumstances, each one is worth investigating on its own.

While the -w flag and "use strict" are great to use here, the -T flag is absolutely necessary, as it highlights the security holes in the script.

Tutorial Pages:
» A Close Look at the CGI.pm Module
» The CGI module
» Displaying Script Errors in the Browser
» Displaying script errors in the browser
» CGI::LogCarp Usage and Shortcomings
» Standard Modules of CGI::* Type
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Random subroutines in Perl
» Log Script Use
» Creating Perl Modules for Web Sites
» Bit Vector, Using Perl Vec
» Build a Perl/CGI Voting System
» Perl Range Operator

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources