Mmmm, I look at that code and I'm really not sure what it's doing without really having to think about it. Maybe it's my aversion to for (;;) style loops but something more like this seems much more understandable to me:
my (%values, $key); foreach my $value ( split /[&;]/, $query_string ) { my ($name, $data) = split /=/, $value; if ( lc($name) eq 'o' ) { $key = $data; $values{$key} = undef unless exists $values{$key}; } elsif ( lc($name) eq 'd' and defined $key ) { # anything you need to do to $data happens here if ( ref $values{$key} eq 'ARRAY' ) { push @{$values{$key}}, $data; } elsif ( defined $values{$key} ) { $values{$key} = [ $values{$key}, $data ]; } else { $values{$key} = $data; } } }

It does throw away anything that isn't either an o or d param and any d that occurs before the first o is also thrown away but as your code also does that it seemed the thing to do.

And if you always use array refs then the second half of the d/o if statement becomes:

push @{$values{$key}}, $data;

which seems to me all the clearer.

Struan


In reply to Re: Reinventing wheels: query string parsing. by mr_stru
in thread Reinventing wheels: query string parsing. by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.