Last updated on November 6th, 2024 at 02:06 pm

In this tutorial, we will explore the method of retrieving the query string value through a straightforward Perl CGI script.

When a URL containing a query string, such as https://webtutorials.dev/cgi/data.cgi?program=php, is accessed via a web browser, the segment “program=php” represents the query that has been transmitted. We will now examine how to extract this value using a Perl CGI script.

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

# ./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);