in reply to Changing the Value Assigned To A Hash Key

A couple of thoughts:

unless ($first_name_check == "1") {

If == is numeric comparison, why are you giving it a string? Do either $first_name_check eq "1" or $first_name_check == 1. Not that it's going to change anything in how your program works, but do it for consistency.

Also, as cowboy notes, instead of $app{"first_name"} = ""; you should probably use undef $app{"first_name"}

Another thing: instead of "First Name: ".$first_name_check, I would use "First Name: $first_name_check" directly.

Replies are listed 'Best First'.
Re^2: Changing the Value Assigned To A Hash Key
by gnurob (Initiate) on Feb 08, 2005 at 21:05 UTC

    I had wondered if == "1" was the best technique. This is alllmost my first ever Perl script.

    A number of quick functions are used to check user data against input criteria (for checking telephone numbers, postal codes, etc.). The code below is for checking a persons first or last name; one of many checks on this form. All we want to do is verify that more than two characters of an acceptable type are used.

    sub check_name { my $name = $_[0]; if ($name =~ /^[A-Z][A-Za-z\-']{2,40}/s) { return 1; } else { return "Friendly message..."; } }

    I wanted to return true or false, but wasn't sure how to incorporate an error message anywhere in the many scripts that call this function. It seemed OK to return a true of OK and a string warning if bad. Running with that idea, the "($first_name_check == "1")" test seemed to be the way to go.

    It crossed my mind to move the all error messages to a seperate file and return error codes greater than one as index references in the seperate error message file. I haven't seen any good examples of accomplishing this task and would certainly appreciate suggestions on where to go look/read. (All I have is an old Perl cookbook that just seems wrong...

    Switch undef around. Thanks for both your suggestions

    # was $app{"first_name"} = ""; switch to next line undef $app{"first_name"};
      Your code doesn't check if two characters are used; it checks if one of [A-Z] is used plus two to fourty of [A-Za-z\-'].

      If you really want at least two characters and the first one must be in [A-Z], use this instead: [A-Z][A-Za-z\-']+. It does the trick :-)

      As for returning the warning string, I don't think that's a good idea, because both 1 and "Friendly message..." evaluate to a true value; hence, the caller won't be able to tell the difference easily. Return 0 or undef in case there's something wrong. This way, you can do something like if ($first_name_check) {.

      The Cookbook sure is a good book, but I'd advise you to buy the Learning Perl. Trust me, it'll be worth the money and you'll enjoy the reading :-)