Request Parameters in Perl

It’s some time that I’m exploring Perl and I like it for its wittiness. I’ve been starting using it to write some system admin scripts, but since it was a good experience I tried to write some web script too, so I will add to my notebook here how to get Request Parameters.

#!/usr/bin/perl

use warnings;
use strict;
use CGI;

print "Content-type: text-plain\n\n";

my $req = new CGI;
print "The value of parameter field is: ", $req->param('field'), "\n";

This script is quite simple, after the shebang I activate warnings and strict mode and include the CGI Perl module, then it outputs a minimal header, creates the CGI object and call the param method on it to get the parameter.

This all seems quite straightforward, so if I call my script adding a field parameter to the URL, the value of that parameter is going to be printed out.

However it also works with mod_perl, because it emulates a CGI interface. I actually use apache2-mod-perl2.

It gets even better than this, it also works on the command line! In the command line parameters are separated by spaces and don’t have a name, however I can write a parameter called “field=myvalue” and myvalue will be printed by the script.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>