I have an error while developing simple sign up form application with Perl.
This is my html.
<html>
<head>
<meta charset="UTF-8">
<title>Kayıt Formu</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<h2>Kayıt Formu</h2>
<form id="signup-form" action="/sample_perl_application/signup.cgi" method="GET">
<div class="form-group">
<label for="name">Ad</label>
<input class="form-control" id="name" name="name" type="text"/>
</div>
<div class="form-group">
<label for="surname">Soyad</label>
<input class="form-control" id="surname" name="surname" type="text"/>
</div>
<div class="form-group">
<label for="age">Yaş</label>
<input class="form-control" id="age" name="age" type="text"/>
</div>
<div class="form-group">
<label for="sexual">Cinsiyet</label>
<select class="form-control" id="sexual" name="sexual">
<option id="male">Bay</option>
<option id="female">Bayan</option>
</select>
</div>
<input class="btn btn-default" id="save" name="save" type="submit" value="Kaydet"/>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</html>
This is my perl script.
#!"c:\xampp\perl\bin\perl.exe"
use strict;
use warnings;
use CGI;
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$name = $FORM{name};
$surname = $FORM{surname};
$age = $FORM{age};
$gender = $FORM{sexual};
print CGI::header();
print $name." ".$surname." ".$age." ".$gender;
I have an error like this. "End of script output before headers: signup.cgi". How can I fix the problem ?
You haven't declared $age, $gender, $surname variables so the script is dying.
Try verifying the script manually via 'perl -w myscript.pl' and look in your webserver error log as @ikegami suggested.
The declaration of variables in your script is a bit different from 'modern Perl' usage.
I would usually declare things with 'my', and make the declarations closer to the usage so the scope of the variables is constrained:
Etc
Your first mistake is to try learning Perl from the TutorialsPoint site. They really have no idea what they are talking about. Try the Perl tutorials hub instead for pointers to better quality Perl tutorials.
Although CGI programs are designed to run on a web server, it's often useful to run them from a command line in order to debug them. In particular, when tracking down syntax errors, then you can use
perl -c
to see all of the problems. I put your code in a file called "testcgi" and ran the commandperl -c testcgi
. I got the following output:You can see that all of your errors are the same. You have forgotten to declare some of your variables. Your code should look like this:
Notice that I have used
my
to declare the variables, notlocal
.local
is largely a hangover from Perl 4. Since Perl 5 was released over twenty years ago,my
has been the best way to declare most variables in a Perl program. Notice, also, that I declare the variables as close as possible to where they are used.There are some other things we can change here.
param
subroutine from CGI.pm to replace your buggy CGI parameter parser.print
statement easier to read.Making those changes, your code reduces to this:
Doesn't that look simpler?
And you can test it from the command line:
The biggest lesson here is that if you're learning a new language, you shouldn't trust random tutorial sites on the internet. They are rarely any use. Ask people who know the language where to find good resources.