symŽ has asked for the wisdom of the Perl Monks concerning the following question:

I am wondering if anyone has seen this behavior before. Occasionally my $hash{variables} will be set to %00 Not sure why..I am certainly not setting it and nothing in the program would create that value on purpose. Any ideas? 5.005_03 built for i386-freebsd

Replies are listed 'Best First'.
(Ovid) Re: hashes filling up with funk
by Ovid (Cardinal) on May 01, 2001 at 21:48 UTC
    symŽ wrote:
    Occasionally my $hash{variables} will be set to %00 Not sure why..

    Um, yes. I've seen this problem many times. It usually has something to do with a line like $hash{variables}='%00';

    Okay, that was out of line. Could you post some code? suaveant mentioned the possibility of a CGI script and I have seen programs behave similar to what you are describing. It's when the use the code downloaded from Elizabeth Castro's Web site companion for the awful book "Perl and CGI for the World Wide Web." In her CGI parsing code, she uses the following substitution:

    $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;

    Notice the space between the first two character classes? No hex characters are going to substituted. Amusingly, she's had this broken code snippet on her site for well over a year.

    I don't know if this is your problem, since your question is impossible to answer (one of my variables keeps getting set to '3'. Why?). If my answer is too far off base, post some code!

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: hashes filling up with funk
by suaveant (Parson) on May 01, 2001 at 21:20 UTC
    We might have some idea if we had any idea what the code you are using looks like... right now your question is very vague...

    Is this a cgi? What's the code that populates the hash?
                    - Ant

Re: hashes filling up with funk
by symŽ (Acolyte) on May 01, 2001 at 22:06 UTC
    Sorry, yes here is some code.. its a CGI . At first I didn't think it was the code because the code is old and this just started to happen recently. Thanks!
    sub CookieCutter { my $raw_dough = "@_"; my ($cookie, $key, $value); undef %Cookies; foreach $cookie (split / /, $raw_dough) { if (not ($key, $value) = ($cookie =~ /^([^\=]+)\=([^\;\s]+)/)) + { if (($key) = ($cookie =~ /^([^\=]+)\=[\;\s+]/)) { $value = ''; } } $Cookies{ $key } = $value; } }

      what's that?? manually??

      look at CGI.pm, it will handle all the fuss for you..

      not mentioning that the code in itself is a bit ugly and pointless

      Update: so it's probably due to the cookie value being escaped, and since you're not using CGI.pm, are you doing this?

      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      on the cookie value to un-escape it??


      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/
        Heh yea, I know... CGI.pm would replace that code nicely. But I don't have that luxury unfortunately... I am dealing with legacy code at work and even more legacy coders and their petty politics. Hopefully that routine has an error in it so I can pass the buck. ;)
      If you use CGI.pm, here's how to get your cookies (straight from the POD!):
      %cookies = fetch CGI::Cookie;

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Are you sure this piece of code is the problem, and not the code that sets the cookie? Can you log $cookie to make sure it's good before chewing on it?
        totally, thats a good point. I'll do that. Although the code is still suspect! :) I guess that what I was really trying to ask about is, Could %00 'null' could be populated on a lower level for some reason