Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Common Problems with CGI

The Basics

Common Problems with CGI

by  goBoating  Posted    (Edited  )
There are several common problems when trying to get a CGI to run.

1 - line endings:
If the code was written on a Windows box and is being
run on a UNIX box, then the line ending must be
converted to Unix flavor. M$ lines end with a carriage
return and a new line. Unix lines end with just a new
line. The required conversion can frequently be done
with the gui ftp client that is used to move the file.
Another option is to telnet to the Unix box and use
the 'dos2unix' command. If you are not sure
how to get this done, check with your server
administrator for suggestions.

2 - incorrect 'first line'
When you install perl on a M$ box, the OS knows how to
run a piece of perl by looking at the file extension.
Consequently,(unless you are using Apache on a M$ box),
the first line of a piece of perl on a M$ box is almost
irrelevant. Conversely, on a unix box, the first line
of a piece of perl MUST point at the perl interpreter
on that box. So, when the file is uploaded, make sure
our first line really is pointing at the perl inter-
preter on that UNIX box.

(If you are running apache on a M$ box, then the first
line is significant. Apache uses it to run the CGI
application.)

3 - perl syntax error
It is difficult to tell when you have a perl syntax
error in a CGI script. I suggest a two step approach
to remedying this. First, immediately prior to moving
the file to the unix box, do
>perl -c your_code.pl <return>

That will report any syntax errors.

Second, don't use the 'die' function in any CGI without
wrapping it in a sub routine. For instance, given
this 'open' statement,[color blue]
open(INPUTFILE,"<some_file.txt") or
die "Failed to open INPUTFILE, $!\n";[/color]
You would never see the complaint in the 'die'.
Instead, use a very short sub to print your complaint
and then 'die'.[color blue]
open(INPUTFILE,"<some_file.txt")
or ooops("Failed to open INPUTFILE, $!\n");[/color]

sub ooops
{
my $complaint = shift;
print "<p>$complaint</p></body></html>\n";
exit; # a little better than die in this context
}

That way you will see the failure of the 'open' printed
to the browser.

Alternatively, use the CGI.pm module's ability to send
fatals errors to the browser.

4 - execute bit not set
On M$ systems, the OS identifies executable files by
their extensions. A *nix system identifies executables
by their permissions. When moving a CGI from a M$ to a
*nix, you must set the execute bits in order for the OS
to 'percieve' the file as executable. The required
conversion can frequently be done with the gui ftp
client that is used to move the file. Or, if you can
telnet to the *nix box, do
>chmod +x your_code.cgi <return>

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top