Forum Moderators: coopster & phranque

Message Too Old, No Replies

Lost in Perl Translation

how do I check for all caps

         

milocold

5:01 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Hi folks,

So I'm a perl noob, trying to decrypt some scary code at work. Here's whats current used for usernave validation:


# check username and password
foreach my $field (qw(order_username order_password)) {
next if $in{$field} =~ /\S+/;
($pf = $field) =~ s/^order_//;
$pf =~ s/\b(\w+)/\u$1\E/;
push(@errors, "The $pf field is missing or invalid.");
push(@todo, $field);
}

I've been search the web for a bit know trying to find out what "s/\b(\w+)/\u$1\E/" entails as far as validating.

What I need is for order_username validation to check for everthing it's currently looking for as well as set a restriction on the field to be uppercase only. If the user enters lower case it'll push the errors as normal

The order_password can be either upper or lower case. So I figure I'll need to break foreach into just a for order_username and another for order_password.

Can someone either point in the right direction or just bust me the solution?

Thank you in advance,

Justin S.

KevinADC

6:44 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



the way you have it, $pf will alwys be pushed in the @errors array.

this line:

$pf =~ s/\b(\w+)/\u$1\E/;

just isn't correct, it would have to be with an uppercase 'U' for the \E operator to work:

$pf =~ s/\b(\w+)/\U$1\E/;

but since there are no \b (word boundaries) in the string it would probably just be better to use the 'uc' operator to change the string to all uppercase:

$pf = uc $pf;

but you still have to use a condition to check for $pf and push it into the @error array if necessary. But I am not sure what condition or conditions would cause that except if there in no $pf defined:

push(@errors, "The $pf field is missing or invalid.") if!$pf;

milocold

6:55 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Thanks for the response! I'll use UC $pf. As for the push, I didn't write that and I'm confident in perl so I'm gonna just leave it for a bit.

Thanks again!

KevinADC

8:56 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



perl operators are case sensitive. UC $pf will not work. Make sure you use lower-case: $pf = uc $pf

Glad to hear you have confidence in perl, but this line:

push(@errors, "The $pf field is missing or invalid.");

will always push $pf (and the message) into the @error array. Maybe that is what you want, but I'm doubting that.

WWMike

10:02 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



I'm an old-schooler so I prefer the more readable:

if ( $x eq uc $x ) {

# ALL CAPS

} else {

# NOT ALL CAPS

}

KevinADC

10:29 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



more readable compared to what?

This is assingment:

$pf = uc $pf;

this is checking string equality:

if ($x eq uc $x)