Last updated on January 19th, 2022 at 11:26 am

In this tutorial we will see how to get the query string value using very simple perl CGI script.

When we hit a URL from the web browser with a query string say
http://mistonline.in/cgi/data.cgi?program=php
In which program=php is the query passed.Now we will see how we get the same value using perl CGI script.

Please note: you don’t have to really run a webserver to test the the script below. You can even use command line like this

# ./check_perl.pl program=php

Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Cgi Query String Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Values Passed</h1>
<table border="1" cellspacing="0" cellpadding="0">
<tr><th>program</th><td>php</td></tr>
</table>
</body>
</html>

As you can see I passed parameter as program=php and the output can be seen in this section of the script

<tr><th>program</th><td>php</td></tr>

Complete Code

#!/usr/bin/perl
#Script From Mistonline.in
use strict;
use CGI;
my $cgi = new CGI;
print
$cgi->header() .
$cgi->start_html( -title => 'Cgi Query String Results') .
$cgi->h1('Values Passed') . "\n";
my @params = $cgi->param();
print '<table border="1" cellspacing="0" cellpadding="0">' . "\n";
foreach my $parameter (sort @params) {
print "<tr><th>$parameter</th><td>" . $cgi->param($parameter) . "</td></tr>\n";
}
print "</table>\n";
print $cgi->end_html . "\n";
exit (0);

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *