Results 1 to 3 of 3

Thread: Comparing Strings with unexpected results

  1. #1
    Join Date
    Aug 2003

    Comparing Strings with unexpected results

    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:


    $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",
    print "\n\n";
    print $query->start_html;
    print $query->end_html;


    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.
    Last edited by couchmonkey; 11-02-2004 at 11:36 AM.

  2. #2
    Join Date
    Mar 2004
    perhaps "\n" is on the end of your variable. I simple chomp() will remove it.


  3. #3
    Join Date
    Aug 2003


    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.
    Last edited by couchmonkey; 11-02-2004 at 03:00 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center