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

I'm processing user input from three possible sources...http cookies, POST data, and GET data.

I'll likely end up with three different name-value strings (in the standard "name=value&name=value" form). I want to access all this data through a CGI object, *but* I'd like GET data to take precedence over POST data, which should take precedence over cookie data, and I can't be sure of the results of just joining them and passing the result to new CGI($str).

So basically, I want to Thus if I end up with these values:

$cookie = "user=john&pass=bleh&sort=descending"; $post = "pg=2&col=3&row=4"; $get = "pg=1&sort=ascending";
I need to merge them into this:
$str = "user=john&pass=bleh&pg=1&col=3&row=4&sort=ascending";
Is there an obvious and/or more efficient way to do this that doesnt require breaking everything down into hashes and processing it all several times (because these strings may get very large)?

Addendum: Basically, I know i could split into an array on '&', and then split each element on '=' into hash keys/values, and do cookie, post, get (so newer vals overwrite existing vals), but is there a better way?

__________
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
- Terry Pratchett

Replies are listed 'Best First'.
Re: merging multiple name-value pairs (with precedence)
by davidrw (Prior) on Feb 09, 2006 at 14:53 UTC
    Addendum: Basically, I know i could split into an array on '&', and then split each element on '=' into hash keys/values, and do cookie, post, get (so newer vals overwrite existing vals), but is there a better way?
    yep! :) See the "MIXING POST AND URL PARAMETERS" section of the CGI docs .. It says that param() will always return the POST'd data, and that you can simply use url_param() for the GET data .. So you can just do cookie, param, url_param ..
      well, i'm getting this data in a roundabout way, so CGI may not be able to see it as "POST" data or "GET" data, it will just see it as two name-value strings. so i sort of need to process all this data as just three seperate strings.

      (i'm working within the limitations of an existing system, unfortunately)

      __________
      Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
      - Terry Pratchett

Re: merging multiple name-value pairs (with precedence)
by blokhead (Monsignor) on Feb 09, 2006 at 15:14 UTC
    I can't find whether this is documented in the perldoc, but when CGI sees a param that is multi-valued, it appears that the values retain their ordering from the query string. So as long as all your fields are single-valued, you can just join the three query strings (in the order GET, POST, cookie) and pass it to CGI. When you then call param in scalar context, you will get the first appearance of that param in the combined query string.

    blokhead

      ++, that's exactly what i needed to do!

      embarrassing thing is, i read the CGI docs over about 3 times, and just sort of missed that bit about multiple values. anyway, thanks blokhead!

      __________
      Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
      - Terry Pratchett