3
votes

I want to use the Perl CGI module for creating CGI scripts. I went through the documentation available here but I seem to have missed something obvious because I have run into problems with my first program. Here is the HTML:

<form name="form1" method="post" action="http://localhost/cgi-bin/filters.cgi">
<input name="mainbox" type="checkbox"> Mainbox<br> <br>
<input name="n1" type="checkbox">No. 1 <br><br>
<input name="n2" type="checkbox"> No. 2<br><br>
<input name="n3" type="checkbox">No. 3 <br>
<div style="text-align: center;"><input name="Submit" value="Submit" type="submit"></div>
</form>

I simply want the names of the parameters that are passed to the CGI file to be printed on a new page. So (with my limited understanding), I wrote the following in filters.cgi:

#!/xampp/perl/bin/perl -w
use strict;
use warnings;
use CGI;

my $query = CGI->new;
print $query = $query->header('text/html');
my @names = $query->param;

my $q1 = CGI->new;
print $q1->header('text/html');
print $q1->start_html('hello');
foreach my $name (@names) {
    print $q1->h1($name);
}
print $q1->end_html;

But this prints out nothing. It does not give me any error either and the syntax is OK. I know I am missing something very simple here but I really want some help with this. How do I write this script correctly? I am using XAMPP in Windows XP, if that makes any difference.

EDIT: Maybe I should mention that I have tried to figure this out myself. So I wrote the following script which works:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my @arr = ('ac', 'fg', 'ty');
my $q1 = CGI->new;
print $q1->header('text/html');
$q1->start_html('hello world');
foreach my $el (@arr) {
    print $q1->p($el);
}
$q1->end_html;

So the problem is somewhere in the parameters being passed. I don't even know where to look for help in the long documentation, so decided to ask here. Also, I have seen the link Nikhil has posted in the comments. One of the points mentioned there is that I should try running my script from the command line. How do I pass these parameters from the command line?

2
@NikhilJain thanks for the link, but I had already gone through it earlier. Could you be more specific as to which of those suggestions could apply to my case? Please see my edit.user828647

2 Answers

4
votes

The first issue you had was that you were assigning the result of calling $query->header('text/html') back into your $query variable, destroying the query object which meant that the next line my @names = $query->param wasn't working as expected.

Secondly, you were attempting to print the Content-type header twice, once using the $query CGI object and once using the $q1 object.

I have removed the unnecessary CGI object, $q1, and used the original $query object in all cases.

The following is the code with the above fixes applied.

#!/xampp/perl/bin/perl -w
use strict;
use warnings;
use CGI;

my $query = CGI->new;
my @names = $query->param;

print $query->header('text/html');
print $query->start_html('hello');

foreach my $name (@names) {
    print $query->h1($name);
}

print $query->end_html;
2
votes
print $query = $query->header('text/html');

This line is part of your problem. $query->header() returns some text, which isn't a useful value to set $query to. You're also creating two CGI objects ($query and $q1) where you only need one, and printing two sets of headers. Get rid of the duplication and the inappropriate assignment, and you should be fine.