On Mon, Dec 23, 2002 at 10:35:59AM -0600, David Gilden wrote:
> PERL 5.6
> OSX, Jaguar
>
> Goal, to check against two different passwords.
>
>
> I think my problem is the "or" is short 'circuited'
> is there a way for an IF statement to test for both
> values, so that either $secret_word or $secret_word_guest
> will be tested, (I am not looking for AND)
Only and's short-circuit. Or's test every argument by necessity.
> What should happen is that if $secret_word OR $secret_word_guest does not Match $qs
> Then it should print 'Fail'. It needs to check both!
It works exactly like you said. However, the way you're going about
testing the arguments seems a bit logically backwards, and that might be
messing you up. See comments:
#!/usr/bin/perl
my $qs = 'c';
my $secret_word = 'a';
my $secret_word_guest = 'b';
if ($qs !~ /$secret_word_guest|$secret_word/) {
# either the word didn't contain 'a' or it didn't contain 'b'.
# ONLY words that contain 'a' and 'b' will get through.
print "fail\n";
}
else{
# $qs contained neither 'a' nor 'b'.
print "go, ok\n";
}
This doesn't look like what you're trying to do. Additionally, your two
examples (with eq and =~, respectively) are not equivalent. Consider the
following examples:
my $test = 'a';
$test eq 'a';
# true
$test =~ /a/;
#true
$test = 'ab';
$test eq 'a';
# false -- test isn't "a"
$test eq 'b';
# false -- test isn't "b"
$test =~ /a/;
# true -- $test contains 'a'
$test =~ /b/;
# true -- $test contains 'b'
$test = 'wakka';
$test eq 'a';
# false
$test =~ /a/;
# true
Equality tests and regex matches are NOT the same thing. You can force
a regex to try to match the beginning and end of a string with ^ and $.
$test = 'wakka';
$test =~/^a$/;
# false, just like the 'eq' example
As for your example, I'd recommend cleaning it up like so:
#!/usr/bin/perl
my $qs = 'c';
my $secret_word = 'a';
my $secret_word_guest = 'b';
if ($qs =~ /$secret_word_guest|$secret_word/) {
print "pass\n";
}
else{
print "fail\n";
}
If $qs is 'c', this prints "fail". If it's 'a', it prints "pass"; if
it's 'b', it prints "pass". If it's "ab", "abba", etc, it also prints
"pass".
> Dave
Hope that wasn't too confusing to be of some help.
As was mentioned earlier, though, if you're checking a query string, you
really should 'use CGI;' and check against individual parameter values
in the query string.
perldoc CGI
--
Michael
[EMAIL PROTECTED]
http://www.jedimike.net/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]