Perl: Retrieving Query Parameters and Headers

Next, let’s play with those methods that retrieve input parameters from query string including request content, and request headers. Here is a simple example Perl CGI script: CGI-pm-Request-Info.pl:

#!c:/local/perl/bin/perl.exe
#- CGI-pm-Request-Info.pl
#- Copyright (c) 2014 HerongYang.com, All Rights Reserved.
   
   use CGI;
   $query = CGI->new();

   $text = "";
   
#- Getting the request method
   $text .= "Request method = ".$query->request_method()."\n";

#- Getting input data from the query string and from the data content
   $text .= "Names and values from param():\n";
   @names = $query->param();
   foreach $name (@names) {
      $text .= "   $name = ".$query->param($name)."\n";
   }
   
#- Getting request headers
   $text .= "Names and values from http():\n";
   @names = $query->http();
   foreach $name (@names) {
      $text .= "   $name = ".$query->http($name)."\n";
   }
   
   print $query->header();
   print $query->start_html(-title=>'CGI-pm-Request-Info.pl');
   print $query->pre($text);
   print $query->end_html();

Copy CGI-pm-Request-Info.pl to C:\local\apache\htdocs and browse to: http://localhost/CGI-pm-Request-Info.pl?name=joe&age=21&student

You should see the following result in the browser. The result looks good.

Request method = GET
Names and values from param():
   name = joe
   age = 21
   student = 
Names and values from https():
   HTTP_ACCEPT_ENCODING = gzip, deflate
   HTTP_CONNECTION = keep-alive
   HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q...
   HTTP_HOST = localhost
   HTTP_DNT = 1
   HTTP_ACCEPT_LANGUAGE = en-US,en;q=0.5
   HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/2...