http://www.perlmonks.org?node_id=441152

Parrot assembler code to parse a CGI query string into a hash of query parameters. Does not support multiple values for parameters. Just enough to get you started if you want to try writing CGI in low-level Parrot.
# use like this: input string, output Hash PMC $P1 = get_query_params('foo=bar&baz=boo') $S1 = $P1['foo'] print $S1
.sub hex2decimal prototyped .param string hex .local int result .local int position .local string digits .local int len result = 0 position = 0 digits = '0123456789ABCDEF' upcase hex len = length hex p_hex_loop: if position == len goto p_hex_over result *= 16 $S1 = hex[position] $I1 = index digits, $S1 result += $I1 inc position goto p_hex_loop p_hex_over: .pcc_begin_return .return result .pcc_end_return .end .sub url_decode prototyped .param string theURL .local int position # + becomes space p_unesc_url_space: position = index theURL, '+' if position == -1 goto p_unesc_url_others theURL[position] = ' ' goto p_unesc_url_space p_unesc_url_others: position = index theURL, '%', position if position == -1 goto p_unesc_url_over $I1 = position + 1 $S1 = substr theURL, $I1, 2 $I1 = hex2decimal($S1) $S1 = chr $I1 theURL = substr position, 3, $S1 inc position goto p_unesc_url_others p_unesc_url_over: .pcc_begin_return .return theURL .pcc_end_return .end .sub get_query_params prototyped .param string query .local pmc result result = new .Hash .local int start .local int end .local int len .local string key .local string value start = 0 parse: index end, query, '=', start if end == -1 goto ret len = end - start substr key, query, start, len start = end + 1 index end, query, '&', start if end > -1 goto more_data substr value, query, start value = url_decode(value) result[key] = value goto ret more_data: len = end - start substr value, query, start, len value = url_decode(value) result[key] = value start = end + 1 goto parse ret: .pcc_begin_return .return result .pcc_end_return .end

Replies are listed 'Best First'.
Re: [Parrot] parsing CGI query string
by Thilosophy (Curate) on Mar 21, 2005 at 09:31 UTC
    Just found out that above snippet does not URL-decode the parameter names (only the values). I'll leave that as an exercise to the reader (it is just a single line that has to be added).
Re: [Parrot] parsing CGI query string
by chip (Curate) on Jun 14, 2005 at 09:21 UTC
    Nice. Missing validity check on the index operation in hex2decimal though. (Funny thing, I don't see any decimals there.)

    Interestingly, I think you've found a bug in the PIR syntax. It should not be legal to say $P = opcode... when the first operand of the given opcode is "inout", just "in". Your code "theURL = substr position, 3, $S1" confused the heck out of me at first.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

[Parrot] parsing CGI query string (update for Parrot 0.4)
by Thilosophy (Curate) on Jan 08, 2006 at 02:35 UTC
    The original snippet does not work anymore, because of the deprecated 'prototyped' keyword. Below is an updated version, that can also be found online.
    .sub hex2decimal .param string hex .local int result .local int position .local string digits .local int len result = 0 position = 0 digits = '0123456789ABCDEF' upcase hex len = length hex p_hex_loop: if position == len goto p_hex_over result *= 16 $S1 = hex[position] $I1 = index digits, $S1 result += $I1 inc position goto p_hex_loop p_hex_over: .return(result) .end .sub url_decode .param string theURL .local int position # + becomes space p_unesc_url_space: position = index theURL, '+' if position == -1 goto p_unesc_url_others_0 theURL[position] = ' ' goto p_unesc_url_space p_unesc_url_others_0: position = 0 p_unesc_url_others: position = index theURL, '%', position if position == -1 goto p_unesc_url_over $I1 = position + 1 $S1 = substr theURL, $I1, 2 $I1 = hex2decimal($S1) $S1 = chr $I1 theURL = substr position, 3, $S1 inc position goto p_unesc_url_others p_unesc_url_over: .return(theURL) .end .sub get_query_params .param string query .local pmc result result = new .Hash .local int start .local int end .local int len .local string key .local string value start = 0 parse: index end, query, '=', start if end == -1 goto ret len = end - start substr key, query, start, len key = url_decode(key) start = end + 1 index end, query, '&', start if end > -1 goto more_data substr value, query, start value = url_decode(value) result[key] = value goto ret more_data: len = end - start substr value, query, start, len value = url_decode(value) result[key] = value start = end + 1 goto parse ret: .return(result) .end