I use a similiar technique. When debugging a CGI application, however, prints can really mess you up.
For that situation, I have a routine (below) that writes to a file. It accepts two parameters, one which is a class string, and a message string. The section string allows me to put messages in a arbitrary category (say, 'debug', 'authentication', or 'SQL'), and the text to be printed.
Each message is timestamped, and also stamped with a user ID (that's internal to me application, and could be ignored).
I also have a similiar version that use the syslog facility to write /var/log/messages.
sub debug_log
{
@_ == 2 or confess "Incorrect number of arguments";
my ($section, $action) = @_;
my $userid = 0;
my $string = 0;
$userid = $globalUserID || "<nobody>";
$string = sprintf ("%s %s %s:%s\n", scalar localtime, $userid, $sec
+tion, $action);
open (SINK, ">> /home/httpd/somedir/logfile") or confess "Can't ope
+n logfile";
print SINK $string;
close (SINK) or confess "Can't close logfile";
}