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.
|