tulians tulians - 1 year ago 60
Perl Question

Can't find reason for error 500 - Perl

I'm new to Perl and its use in CGI. I've been having this error 500 for some hours and still don't know where's the error(s). The script is placed in the corresponding

folder of the Apache server. It's then called by this simple HTML form:

<FORM action="http://localhost/cgi-bin/sensors.cgi" method="POST">
Sample period: <input type="text" name="sample_period"> <br>
<input type="submit" value="Submit">

As fas as I know, an error 500 appears when there was an improper upload or a bug in the script. But I've tested uploading other files and had no problems with it. Thats why I believe there may be a bug in the code. This is the Perl script:

use IO::Handle;

# Open the output file that contains the sensors' readings. It's open in write mode, and
# empties the content of the file on each opening.
open (my $readings, ">", "sensors_outputs.txt") || die "Couldn't open the output file.\n";

# Defines the physical magnitudes and sets each one a random value.
my $temp = rand 30;
my $hum = rand 100;
my $pres = 1000 + rand(1010 - 1000);
my $speed = rand 100;

for(;;) {
# Writes in the file-handler's file the values of the physical magnitudes.
print $readings "$temp\n$hum\n$pres\n$speed";
# Flush the object so as not to open and close the file each time a new set of
# values is generated.
# Move the file-handler to the beggining of the file.
seek($readings, 0, SEEK_SET);
# Generate new a new data set.
$temp = rand 10;
$hum = rand 100;
$pres = 1000 + rand 10;
$speed = rand 100;
sleep 1;
close $readings || die "$readings: $!";

Don't hesitate to ask me for more context if needed. Thanks in advanced.

Answer Source

The obvious problem there is that the script doesn't output an HTTP response, which the CGI specification requires.

At a minimum you would need something like:

print "Status: 204 No Content", "\n\n";

but it would be more usual to say

print "Status: 200 OK", "\n";
print "Content-type: text/plain", "\n\n";
print "Success!";

That said, 500 just means there there is an error. It might be related to the code. It might be related to the server configuration. The above is the obvious problem, but there might be other issues. When you get a 500 error you need to look at the server's error log and see what the error message actually says. There is no point in trying to solve the problem before you do that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download