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
<FORM action="http://localhost/cgi-bin/sensors.cgi" method="POST">
Sample period: <input type="text" name="sample_period"> <br>
<input type="submit" value="Submit">
# 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;
# 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;
close $readings || die "$readings: $!";
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!";
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.