While I find coding HTML with CGI.pm cumbersome, I can think of a couple reasons why you might want to use it.
Most scripts just gather input, validate and process one form.
By utilizing CGI.pm, you can bring error handling and presentation up a notch.
I recently wrote a simple module that inherets from CGI that allows me to register informational error messages for any missing/bad parameters with "error bullets" that are automatically displayed next to any form element registered with CGI.pm.
(Also keeps track of colspans from the previous row so I don't hard code it. Makes maintainence easier.)
All screens functions can do "double duty" (form/error respones) so usually you can have a one to one match between screens and functions.
This would be a big pain to do without CGI or something else to handle HTML generation.
I think an approach like this works very well as interfaces get bigger.
I wrote a 10,000 line product/order management CGI interface for a fairly big (now defunct) dot com mixing and matching perl and html because CGI.pm's syntax was too cumbersome for me (I was too lazy).
While I hacked it out in a couple days, enhancements where a huge headache.
Below is a pretty trivial example but it doesn't take much longer to code it this way than to do it the down and dirty way and I think it is much cleaner. Everything is in one nice spot.
The following example is at http://smb.yso.net/cgi-bin/silly.pl
#!/usr/bin/perl
# silly.pl
# http://smb.yso.net/cgi-bin/silly.pl
use CGI::ParamError qw(:standard :html3);
my $q = new CGI::ParamError;
# REQUIRED FIELDS
# first_name last_name email
print header();
unless (param()){
display_form();
}else{
unless (param('first_name')=~/\w/){
param_error('first_name',"Sorry, I need to know your first nam
+e");
}
unless (param('last_name')=~/\w/){
param_error('last_name',"Sorry, I need to know your last name"
+);
}
unless (param('email')=~/^\w+\@\w+\.\w/){
param_error('email',"Sorry, param('email') doesn't seem like a
+ valid email");
}
if ($q->has_errors){
display_form();
}else{
# DO SOMETHING HERE
confirmation();
}
}
#########################################
sub display_form{
# show_error_messages() is part of my module, this is where the ve
+rbose messages are displayed
# Should be called AFTER fields are validated.
print start_html(-title=>'Silly Script'),
show_error_message(),
start_form,
"First Name",textfield(-name=>'first_name'),br,
"Last Name",textfield(-name=>'last_name'),br,
"Your Email",textfield(-name=>'email'),br,
submit,br,
end_form,
end_html;
}
#########################################
sub confirmation{
print start_html(-title=>'Silly Script'),
"Thanks for stopping by ",param('first_name')," ",param('last_
+name'),"!";
end_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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.