in reply to Why use HTML instead of CGI? (code, discussion)

I'm inclined to disagree with both approaches show here, if that's any help. As someone else pointed out, the html is in there even when written in CGI.pm notation.

It's not that perl is perl, as you put it: i think the rule should be more abstract: 'programming is programming and presentation is presentation'. the scripts should deal with data, and if a public face is involved then leave the designers to design it and just hand them the data later. It's good politics, apart from anything else.

If it's any comfort, i go through this sort of audit/replace/reject all the time, and it's much more painful when the person who wrote the idiot spaghetti was you and it was only six months ago and you thought it was kind of cool at the time :(

  • Comment on Re: Why use HTML instead of CGI? (codediscussion)

Replies are listed 'Best First'.
Re: Re: Why use HTML instead of CGI? (codediscussion)
by shotgunefx (Parson) on May 09, 2001 at 18:26 UTC
    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; }
        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 ;)