cei has asked for the wisdom of the Perl Monks concerning the following question:

This one is probably something really simple, but I can't seem to quite get my head around it.

I've got a form and I'd like to have some of the fields be required.

I'm using CGI.pm and maping all the form variables into a namespace called Beta.

I was going to try something like this:

($errorcodes .= "<li>First Name") if !($Beta::FirstName); ($errorcodes .= "<li>Last Name") if !($Beta::LastName); ($errorcodes .= "<li>Email Address") if !($Beta::Email);
When I thought, gee, a hash might be good for this. So I tried:

my ($key, $value); my %required = { FirstName => 'First Name', LastName => 'Last Name', Email => 'Email' }; while ( ($key, $value) = each %required) { ($errorcodes .= "<li>$value") if !($page->param("$key")); }
I had to move out of the $Beta:: namespace, because it didn't like $Beta::$key.

Anyway, I also traced $key = $value, and it's giving me HASH(0x1012b848) =

So obviously I'm getting references instead of values. But I'm new to hashes, and I'm not sure where to go from here.

Any suggestions?

Replies are listed 'Best First'.
Re: Simple Hash Q.
by merlyn (Sage) on Nov 26, 2000 at 08:54 UTC
    Any suggestions?
    Yes. Turn on warnings, turn on strict. You're not initializing a hash. You've used curlies where you meant parens. You also don't need the quotes on "$key".

    -- Randal L. Schwartz, Perl hacker

      Warnings were on. Strict was used. The curlies vs. parens were the culprit, and I thought I was copying that code out of the perldocs. They aren't too explicit on using {} with $ and () with %.

      *shrug*

      Thanks.

        Weird, when I try to do something like that with my code using warnings, I get this under 5.6 and 5.00503:
        Reference found where even-sized list expected at test line 1.
        So it's very strange that warnings/strict would not have indicated that this was the problem to begin with.
Re: Simple Hash Q.
by extremely (Priest) on Nov 27, 2000 at 08:45 UTC
    You know, rather than pulling everything into a namespace, you can just stick it all in a hash too. my (%phash)= $q->Vars; makes %phash a hash of all the parameters. Messing with namespaces when I didn't have to was an early CGI nightmare of mine...

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Simple Hash Q.
by damian1301 (Curate) on Nov 27, 2000 at 03:06 UTC
    Well, I might be wrong on this but I though up something like this...

    $message = ""; $errmsg = "<p>Field 'first' must be filled in.</p>\n" ; if ($FirstName eq "") { $message = $errmsg ; $err = 1 ; } $errmsg = "<p>Field 'last' must be filled in.</p>\n" ; if ($LastName eq "") { $message = $errmsg ; $err = 1 ; } $errmsg = "<p>Please enter a valid email address</p>\n" ; if ($Email !~ /.+\@.+\..+/) { # i had alot of trouble with this. $message = $errmsg ; $err = 1 ; } if ($err) { &error; } sub error{ print $errmsg; }


    Wanna be perl hacker.
    Dave AKA damian
      When I do things like that, I tend to put the errors in an array instead. Makes things more readable:
      foreach (@required) { push(@errors, "$_ is a required field") unless $q->param($_); } push(@errors, "E-mail address is invalid") unless Valid($q->param('ema +il')); push(@errors, "The moon is full") if $moon->isFull; if (@errors) { &Error(@errors); } else { &Success("You rule."); }