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.


In reply to Re: Re: Re: Retrieving Form Data by Hero Zzyzzx
in thread Retrieving Form Data by Monolith-0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.