in reply to A Matter of Style in CGI

No criticism will be considered too minor.

Around here, that's a challenge. :)

A couple of comments:

  1. Get into the habit of taint-checking, via -T. You'll find straightway that you need to de-taint param("sort"), since somebody could sneak in something that would corrupt the SQL statement you're building.
  2. use strict; will keep you out of trouble, as well. (And it would have pointed out one of the problems below.) use strict; sooner.
  3. Insteading of saying "Perl (vs. 5)" in a comment, write
    require 5.0;
    Then Perl will enforce the version requirement for you.
  4. You need to think through your error handling. In particular, what do you want the user experience to be if either you fail to connect to the database or if your query fails to execute?
  5. Add error checking to $sth->execute();
  6. What do you intend to do with $style? Never mind. I looked, but didn't at first see where you were using it.

The rest looks O.K. at a casual glance. You might want to look into using one of the available templating mechanisms (e.g., HTML::Template) so that you can edit your HTML separate from program logic.

Replies are listed 'Best First'.
Re^2: A Matter of Style in CGI
by Aristotle (Chancellor) on Sep 12, 2002 at 18:30 UTC

    He does use strict; - it just comes after the comments..

    (I was bitten by that in my first glance too as I tend to put the strict line up top below the shebang. I'd put it on the shebang line if that were possible..)

    I also much prefer Template Toolkit 2 to HTML::Template - to each his own, so I thought I'd point out the alternative.

    Makeshifts last the longest.

      I'd put it on the shebang line if that were possible..
      perldoc perlrun
      #!/usr/bin/perl -Mstrict -wT

      update:

      Ooooooooooooooooooooh .... *crash*n*burn* ;)(i'd say i was about due for one of these ~ didn't try it)

      ____________________________________________________
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        No, you cannot do that - The -M option cannot be used in shebang lines as it will generate an error. This option is intended for use only on the command line.

        For example:

        rob@kathmandu:~$ cat test.perl #!/usr/bin/perl -Mstrict -Tw print "foo!\n"; rob@kathmandu:~$ ./test.perl Too late for "-Mstrict" option at ./test.perl line 1.