http://qs321.pair.com?node_id=130871

Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

What is the best, most versatile, most acceptable way to get the data submitted from a web form into my perl script?

Multiple times I have posted some code along with my questions here at PerlMonks, and multiple times I have been told to use CGI.pm to get the form data rather than what I had in my code. However, I have not yet been told how to do so, nor have I been able to find the correct way I should do so.

Could someone please provide a good, solid sample (or multiple variants) of exactly how to get the data from a form to a hash variable in a perl script. I am looking for an example with separate HTML page and Perl script code, however, Perl only code examples may also be provided.

Replies are listed 'Best First'.
Re: How do I get data from a web form (the correct way)?
by andye (Curate) on Dec 11, 2001 at 18:07 UTC
    Hi Monolith-0, you sound (understandably) frustrated. I'll try and clear things up a little.

    First, you need to have the CGI.pm module installed. You almost certainly do already, as it comes as standard (I believe).

    Second, you need to use it as part of your script. There are two ways of doing this, the object-oriented style, and the 'normal' style. I'll show you examples of both.

    You need to make your HTML form. This is just a normal form, so I won't go into it - except to say that the action of the form should be the URL of your script.

    Now, examples. The CGI.pm docs are here:
    http://search.cpan.org/doc/LDS/CGI.pm-2.78/CGI.pm
    Here are the examples at the top of that file, in 'normal' format:

    use CGI qw/:standard/; print header, start_html('A Simple Example'), h1('A Simple Example'), start_form, "What's your name? ",textfield('name'),p, "What's the combination?", p, checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe'], -defaults=>['eenie','minie']), p, "What's your favorite color? ", popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse']),p, submit, end_form, hr; if (param()) { print "Your name is",em(param('name')),p, "The keywords are: ",em(join(", ",param('words'))),p, "Your favorite color is ",em(param('color')), hr; }
    and OO format:
    #!/usr/local/bin/perl -w use CGI; # load CGI routines $q = new CGI; # create new CGI object print $q->header, # create the HTTP header $q->start_html('hello world'), # start the HTML $q->h1('hello world'), # level 1 header $q->end_html; # end the HTML
    I prefer the OO format, but you can use either. Note also that you don't have to use the output functions (e.g. $q->h1('hello world') in order to use the input functions (e.g.$q->param('foo'))

    So, the most basic example of parsing form input would be:

    my $query = new CGI; my $cheese = $query->param('cheese');
    assuming that the HTML form has an input field called "cheese", e.g. <input type="text" name="cheese">

    If you need to copy your parameters into a hash, then have a look at this part of the CGI.pm documentation:
    http://search.cpan.org/doc/LDS/CGI.pm-2.78/CGI.pm#FETCHING_THE_PARAMETER_LIST_AS_A_HASH_.
    NB: you need to pay special attention to what this says about 'multivalued CGI parameters', i.e. if there's more than one input tag in the form which has the same name, as in the case of checkboxes. Note also that if you're not using the OO style, then in order to use the  Vars() function, you need to use CGI ':cgi-lib'; (instead of the normal qw(:standard) ) at the start of your script.

    Further answers can be found in the CGI.pm documentation and in O'Reilly's CGI Programming with Perl (Scott Guelich et al).

    Hope that helps,
    andy.

Re: How do I get data from a web form (the correct way)?
by andyram27 (Initiate) on Apr 25, 2002 at 02:22 UTC
    Same thing bugged the hell out of me. Here's how I do it. This program takes input from a generated form and prints it to the browser.

    Make sure you have CGI.pm installed

    #!usr/bin/perl -w #include warning switch #loads CGI with standard shortcuts use CGI qw/:standard/; #good for keeping track of variable scope, etc use strict; my $name = param('name') || ''; if($name) { &page2; } else { &page1; } sub page1 { print header; print '<form method="post" action="thisfile.pl">'; print '<input type="text" name="name">'; print '<input type="submit" value="submit">'; print '</form>'; } sub page2 { print header; print 'Hi, '.$name.', your variable has been submitted.'; }
Re: How do I get data from a web form (the correct way)?
by Hofmator (Curate) on Dec 12, 2001 at 21:15 UTC
    You might also want take a look at Ovid's CGI course which explains how to use the CGI module and why to use it.
Re: How do I get data from a web form (the correct way)?
by Voytek (Initiate) on Dec 29, 2001 at 10:40 UTC
    Hey! If you go to www.htmlgoodies.com, and check the PERL Primers, they will give you the code below:
    if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; }
    The code above will make it so that if you put $FORM{'name'}, you will get the name variable. The different variables will be made from the form's name for the different things you use (text box, text area, etc.). Hope this helps. -Voytek

    Originally posted as a Categorized Answer.