in reply to Expression, but is it regular?
First of all, the "or" in the "if" statement will never trigger because "if" always tests for "truth." In other words, the "if" is saying "if the regular expression is true OR the string " " is true." Unfortunately, " "is NEVER true in Perl, so if your regex doesn't match the if is skipped entirely.if ($in{username} =~ /^(\[a-zA-Z]{2})/ || ' ') { $dirtype = $1 || 'other'; }
Second of all, IF the regular expression DOES match (triggering the code between the brackets and populating $1 at the same time) THEN in your case $1 is always guaranteed to be true (because it will contain two A-Za-z letters. Letters always evaluate to "true." Because the first expression in your short-circuit operation ($1) is always true, your program will never get to the 'other' value.
This is why OeufMayo resorted to a ternary operator, which is just a short-hand way to write another if/then statement. Here it is rewritten for clarity:
This way the logic is all straigthened out.my $dirtype; if ( $in{username} =~ /^([a-zA-Z]{2})$/ ) { $dirtype = $1; } else { $dirtype = 'other'; }
And then there's the backslash in your regex. :-D
Gary Blackburn
Trained Killer
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Expression, but is it regular?
by Rhandom (Curate) on May 08, 2001 at 18:29 UTC | |
|
Re: Re: Expression, but is it regular?
by web-yogini (Novice) on May 12, 2001 at 05:22 UTC |