in reply to Form param checking

You might want to use CGI.pm as it was intended. The following example is taken from "CGI Programming with Perl, Second Edition":
#!/usr/bin/perl -wT use strict; use CGI; my $q = new CGI; print $q->header( "text/plain" ); print "These are the parameters I received:\n\n"; my( $name, $value ); foreach $name ( $q->param ) { print "$name:\n"; foreach $value ( $q->param( $name ) ) { print " $value\n"; } }
Which we would change as following to fit your example:
use CGI; my $q = new CGI; print $q->header( "text/html" ); if ( $q->param() ) { if( $q->param( $name ) ) { if( $q->param( $email ) ) { if( exists $emails{$q->param( $email )} ) { print "<center><b>Email already exists in database.</b +></center>\n"; } else { $emails{$q->param( $email )} = "$info"; print "<center><b>Your information was added to our sy +stem!</b></center>\n"; } } # you missed this closing brace else { print "Email was missing"; exit; } } else { print "Name was missing"; exit; } }

And last but not least, you might want to write your code so that you can see easily when you are missing an opening/closing code block brace.

/oliver/

PS: You might want to check the information you add to the emails hash for validity.

Replies are listed 'Best First'.
Re: Re: Form param checking
by Anonymous Monk on Dec 30, 2003 at 01:38 UTC
    I'm curious what you mean by
    You might want to use CGI.pm as it was intended.

    The only difference I see (besides fixing the missing brace) is using the object-oriented style rather than the functional style. Did I miss something?

      Well, if the only difference is using the OO style over the functional style, then i say use the functional style. Hands Down. The only time i use the OO style is when i absolutely must ... but until those moments, i use the functional style because it looks cleaner and requires less typing.

      I think neuroball was being a bit subjective by claiming that is how CGI.pm was intended to be used. Especially upon viewing this snippet of unchanged code:

      print "<center><b>Email already exists in database.</b></center>\n";
      which i would have changed to:
      print p({align=>'center'}, b('Email already exists in database'));
      As long as you use param(), header(), and upload(), (correctly) you are using CGI.pm correctly, in my humble opinion. CGI.pm surely subscribes to Perl's philosophy of There Is More Than One Way To Do It!

      I think the outline that Roger provided you with is an excellent place to start studying. You should always write out some kind of design of the possible scenarios ... be it pseudo-code or geometrical shapes with arrows ... before you just start hacking out some code. How can you solve the problem if you don't even understand what the problem is?

      But, do heed Ovid and Podmaster's suggestion to use the CPAN! The idea is that someone has already solved this problem for you, the catch is that you still have to understand what the problem is. That and having a inkling of howto RTFM. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)