[Melbourne-pm] Wierd CGI Error - different displays for different executers

Paul Fenwick pjf at perltraining.com.au
Sun Mar 26 17:15:16 PST 2006

G'day Sam,

Sam Brent wrote:

> Has anyone ever had a CGI script that creates the right output when
> run in a terminal but the incorrect ouput when run in a browser?

I just glanced through your code, and my guess would be that when it is run from
a browser it's using a different current working directory than when you're
executing it from the command line.  As such, your code that opens and reads
your file will fail.

This is an easy theory to test.  Presently your code using the following line,
which does not check to see if the file was opened successfully:

	open ROUND,"<round"; $round=<ROUND>; chomp $round; close ROUND;

Instead you may wish to try:

	open(ROUND,"<round") or die "Cannot open round - $!";
	chomp $round;

Or, if you want a solution with a more best-practice coding style using scalar
filehandles and lexical variables:

	open(my $round_fh, "<", "round") or die "Cannot open round - $!";
	my $round = <$round_fh>;

The advantage here is that your program will die noisy if it cannot open the
file it's after.  You can even add:

	use CGI::Carp qw(fatalsToBrowser);

to have those errors display to the browser, which may make debugging and
development easier.

All the very best,


Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

More information about the Melbourne-pm mailing list