in reply to Re: Why use HTML instead of CGI? (codediscussion)
in thread Why use HTML instead of CGI? (code, discussion)

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; }

Replies are listed 'Best First'.
Slightly OT
by Tuna (Friar) on May 14, 2001 at 15:58 UTC
      Good point. This was as the name implied a silly demonstration I threw together for the sake of this post.

      Probably unwise of me to post such an "iffy" example with newbies around.

      -Not that I don't have a ton to learn still ;)