Hi Anadi. Anadi Taylor wrote: > Hi all, > I have to start by thanking you all for your help so far - its been > invaluable. If it were'nt for you guys and gals i would have pulled > my hair out by now !!!!! > > OK - here is the thing: I have written some code and it works fine > (yahoooo), but i was wondering if there is a more 'PERL' way of doing > things as this code looks bulky to me !!!! > > the code id: > > ## check to see if username or email already exist > > $errmesseml = "False"; > $errmessuser = "False"; > $emailuserfound = "False"; > > if($dbemail eq $email) > { > $errmesseml = "True"; > } > > if($dbusername eq $membusrname) > { > $errmessuser = "True"; > } > > ## create readable error message > > $errmessage = ""; > > if (($errmesseml eq "True") and ($errmessuser eq "True")) > { > $errmessage = "Both the email address and the username have already > been used"; > $emailuserfound = "True"; > > } > > if (($errmesseml eq "True") and ($errmessuser ne "True")) > { > $errmessage = "The email address has already been used"; > $emailuserfound = "True"; > } > > if (($errmesseml ne "True") and ($errmessuser eq "True")) > { > $errmessage = "The username has already been used"; > $emailuserfound = "True"; > } > > > > I have tried to use lines like: > > if ($errmesseml and !($errmessuser)) { do something } > > but it doesnt work !!!! A bit of a bummer really - any ideas ?????
Well, there's an unless ($condition) statement, which is the same as if (not $condition), but there's no need to use a specific string for TRUE. It's a bit like C where zero is FALSE, but Perl allows the empty string as FALSE as well. Before that though, the first thing is to: use strict; use warnings; which will find a lot of problems for you before yu get to testing. With 'strict' you then have to declare all the variables that you're using with: my ($var, @array, %hash); The following is a lot tidier, but doesn't doe anything in a particularly Perlish way. All you see here can be done very similarly in C. use strict; use warnings; my ($dbemail, $email, $errmesseml); my ($dbusername, $membusrname, $errmessuser); my ($errmessage, $emailuserfound); $errmesseml = ($dbemail eq $email); $errmessuser = ($dbusername eq $membusrname); $emailuserfound = ($errmesseml or $errmessuser); ## create readable error message if ($emailuserfound) { if (not $errmessuser) { $errmessage = "The email address has already been used"; } elsif (not $errmesseml) { $errmessage = "The username has already been used"; } else { $errmessage = "Both the email address and the username have already been used"; } } As for an idiomatic solution, how about this: use strict; use warnings; my ($dbemail, $email, $errmesseml); my ($dbusername, $membusrname, $errmessuser); my ($errmessage, $emailuserfound); $errmessage = ( "", "The email address has already been used", "The username has already been used", "Both the email address and the username have already been used") [do { my $i = ($errmesseml = $dbemail eq $email); $i += 2 * ($errmessuser = $dbusername eq $membusrname); }]; $emailuserfound = not not $errmessage; (No don't use this, unless you're feeling perverse!) HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]