in reply to Re: Retrieving Form Data
in thread Retrieving Form Data

Here's a sample of what sompage.pl might be like:
use strict; if ($ARGV[0] eq "submit") { &submit_data; } else { $view_form; } #################### sub view_form { print "<HTML>\n"; print " <BODY>\n\n"; print " <FORM METHOD= \"POST\" ACTION= \"somepage.pl?submit\">\n" +; print " <B>Pass:</B> <INPUT TYPE= \"PASSWORD\" NAME= \"Pass\"><B +R>\n"; print " <BR>\n"; print " <B>Name:</B> <INPUT TYPE= \"TEXT\" NAME= \"Name\"><BR>\n +"; print " <B>URL:</B> <INPUT TYPE= \"TEXT\" NAME= \"URL\" SIZE=40> +<BR>\n"; print " <INPUT TYPE= \"SUBMIT\" VALUE= \"Submit\">\n"; print " </FORM>\n"; print " </BODY>\n"; print "</HTML>\n"; } #################### sub submit_data { # Get the data submitted from the form read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs my @pairs = split(/&/, $buffer); my %FORM; foreach my $pair (@pairs) { my ($name, $value) = split(/=/, $pair); # Remove plus signs and decode %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/<!--(.|\n)*-->//g; $FORM{$name} = $value; } ## Do something with the data }

- Monolith

Replies are listed 'Best First'.
Re: Re: Re: Retrieving Form Data
by Hero Zzyzzx (Curate) on Jul 15, 2001 at 04:32 UTC

    Oh my. I'd probably do that differently, especially by using CGI.pm for my form decoding. There's a million different reasons to CGI, the best is that it makes your job much easier!

    Here's your code, using CGI.pm:

    #!/usr/bin/perl -wT use strict; use CGI; my $q= CGI->new(); if (!$q->param('pass'){ print_form($q); } else { submit_data($q) } sub print_form{ my $q =shift; print $q->header(). $q->start_html(). $start_form(-method=>'POST', -action=>'/cgi-bin/somepage.pl'). $q->b('Pass:'). $q->textfield(-name=>'pass'). $q->br(). $q->b('Name:'). $q->textfield(-name=>'name'). $q->br(). $q->b('URL:'). $q->textfield(-name=>'url'). $q->submit(). $q->end_form(). $q->end_html(); } sub submit_data{ my $q=shift; # No need to go through all that form decoding stuff here. # It's done automatically, and in the Right Way by CGI.pm. #do stuff with data. }

    Why is this better? Less typing, for one. Easier to read, too. Plus CGI.pm is very, very good for creating forms and simple HTML (it does a lot more than what I used it for here, obviously!). CGI.pm is also very good for creating tables programatically, e.g. from a database. You can even use CGI to auto-fill your forms, when you validate data from users.

    If you're going to be doing more complex HTML in your code, you should look into a templating system that lets you separate your perl scripts from your HTML. A really good one to learn is HTML::Template. It'll seem like a pain at first, but it's worth it in the long run.

    Some links:
    Use CGI or Die! from Ovid. Really good reading.
    Ovid's CGI tutorial.
    The CGI.pm docs from Lincoln Stein.
    HTML::Template Tutorial at PerlMonth.

    Update:The way I REALLY would have done this is with CGI::Application, which I think is like a state machine, and is HTML::Template's (non-)evil twin.

      Yea, yea... I still need to get used to CGI. Thanks for the related links.