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 command perl -c testcgi
. I got the following output:
$ perl -c testcgi
Global symbol "$buffer" requires explicit package name (did you forget to declare "my $buffer"?) at testcgi line 8.
Global symbol "@pairs" requires explicit package name (did you forget to declare "my @pairs"?) at testcgi line 8.
Global symbol "$pair" requires explicit package name (did you forget to declare "my $pair"?) at testcgi line 8.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 8.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 8.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 8.
Global symbol "$buffer" requires explicit package name (did you forget to declare "my $buffer"?) at testcgi line 13.
Global symbol "@pairs" requires explicit package name (did you forget to declare "my @pairs"?) at testcgi line 17.
Global symbol "$buffer" requires explicit package name (did you forget to declare "my $buffer"?) at testcgi line 17.
Global symbol "$pair" requires explicit package name (did you forget to declare "my $pair"?) at testcgi line 19.
Global symbol "@pairs" requires explicit package name (did you forget to declare "my @pairs"?) at testcgi line 19.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 20.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 20.
Global symbol "$pair" requires explicit package name (did you forget to declare "my $pair"?) at testcgi line 20.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 21.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 22.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 23.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 23.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 23.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 26.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 26.
Global symbol "$surname" requires explicit package name (did you forget to declare "my $surname"?) at testcgi line 27.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 27.
Global symbol "$age" requires explicit package name (did you forget to declare "my $age"?) at testcgi line 28.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 28.
Global symbol "$gender" requires explicit package name (did you forget to declare "my $gender"?) at testcgi line 29.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 29.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 33.
Global symbol "$surname" requires explicit package name (did you forget to declare "my $surname"?) at testcgi line 33.
Global symbol "$age" requires explicit package name (did you forget to declare "my $age"?) at testcgi line 33.
Global symbol "$gender" requires explicit package name (did you forget to declare "my $gender"?) at testcgi line 33.
testcgi had compilation errors.
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:
#!"c:xamppperlinperl.exe"
use strict;
use warnings;
use CGI;
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
my $buffer;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
my @pairs = split(/&/, $buffer);
my %FORM;
foreach my $pair (@pairs) {
my ($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
my $name = $FORM{name};
my $surname = $FORM{surname};
my $age = $FORM{age};
my $gender = $FORM{sexual};
print CGI::header();
print $name." ".$surname." ".$age." ".$gender;
Notice that I have used my
to declare the variables, not local
. 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.
- If we import named subroutines from CGI.pm, we can make the calls to them a little cleaner by omitting the package name.
- We can use the
param
subroutine from CGI.pm to replace your buggy CGI parameter parser.
- We can use the fact that Perl variables are expanded in double-quoted strings to make your
print
statement easier to read.
Making those changes, your code reduces to this:
#!"c:xamppperlinperl.exe"
use strict;
use warnings;
use CGI qw(param header);
my $name = param('name');
my $surname = param('surname');
my $age = param('age');
my $gender = param('sexual');
# We're outputing plain text, not HTML
print header(-content_type => 'text/plain');
print "$name $surname $age $gender";
Doesn't that look simpler?
And you can test it from the command line:
$ perl testcgi2 'name=foo&surname=bar&age=18&sexual=M'
Content-Type: text/plain; charset=ISO-8859-1
foo bar 18 M
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.