Click to See Complete Forum and Search --> : Comparing Strings with unexpected results


couchmonkey
11-02-2004, 10:24 AM
I'm working on a bit of an emergency project here, and I'm very much a Perl novice, so I apologize if this is obvious:

I'm retrieving a variable from a cookie, and then trying to check if it's value is equal to the string "yes". But I found through testing that in fact the string that I'm getting from the cookie is "greater than" yes concatonated with ascii 032 and "less than" yes concatonated with ascii 033.

I'm wondering if there's something obvious that I just don't know about Perl here, like some special trailing character that is saved in strings. Or maybe this is an artifact from the way we're saving the cookies (the cookie is encrypted with CGI::Blowfish).

Here's a snippet:

# SET COOKIE SNIPPET

$seen_message = "yes";

#There are more variables than $seen_message, i'm just simplifying
#to make it more readable
$info{SeenMsg} = $seen_message;
my $cookie = cookie::encrypt(\%info, $cookiename);
print $query->header(
-refresh=>"1; URL=$seturl",
-cookie=>$cookie
);
print "\n\n";
print $query->start_html;
print $query->end_html;
exit;
}


# RETRIEVE COOKIE SNIPPET

my %cookies = fetch CGI::Cookie;

if ($query->cookie($cookiename)) {

$infox = cookie::decode($query->cookie($cookiename));

$seen_message = $infox->{SeenMsg};

my $char;
my $char2;
$char = chr(32);
$char2 = chr(33);

# This following if statement is true when I previously set
# SeenMsg in the cookie to "yes"
if(($seen_message gt ("yes" . $char)) &&
($seen_message lt ("yes" . $char2)))
{
$seen_message = "The message is yes";
}
else {
$seen_message = "Actually it's " . $seen_message;
}


Edit: Added snippet from setting the cookie.

silent11
11-02-2004, 01:21 PM
perhaps "\n" is on the end of your variable. I simple chomp() will remove it.

-Will

couchmonkey
11-02-2004, 01:46 PM
Okay, this gave me some ideas. I tried chop()ing the variable containing the word "yes" from the cookie. I had to chop it six times before the "s" was chopped off. I also noticed while spelunking through our code that the encryption requires blocks of 8 bytes to encrypt.

So I'm guessing maybe those extra bytes are being tacked on.

Now, does anyone know a convenient way to get rid of those extra bytes? Or maybe I should try something with regular expressions?

Edit: Never mind, I answered my own question. :) I switched the comparison to a regular expression and it works perfectly now. Thanks Will, your comment about chomp() gave me all the motivation I needed to figure out the rest.