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

I'm trying to build a script that reads the ?foo=bar type information from the end of a url, parses out the values, thern displays a page that is custom to the member whoes vlaues were entered in the url. Is there any chance somebody can give me a complete script that does this?

Replies are listed 'Best First'.
Re: the search string and me
by jdtoronto (Prior) on Sep 12, 2003 at 02:44 UTC
    Gee, give us enough money and we will do anything! But seriously, look at the CGI module on CPAN. That is what it does, and in part, why it deserves our esteem and affection

    jdtoronto

Re: the search string and me
by jonadab (Parson) on Sep 12, 2003 at 04:12 UTC

    Sure, sounds easy enough...

    #!/usr/bin/perl -T -- -*- cperl -*- $==$ENV{'CONTENT_LENGTH'};for(split/&/,($=>0)?(map{read(STDIN,$.,$_)} $.):($ENV{'QUERY_STRING'})){s;\+; ;g;($:,$,)=map{s;%(..);pack("c",hex $1);ge;$_}split(/=/,$_);$_{$:}=$,;}$\="<html>$/<head><title>CGI home" ."work</title></head><body><table><tbody>".(join$/,map{"<tr><td>$_</" ."td><td>$_{$_}</td></tr>"}keys%_)."</tbody></table></body></html>"; $_="Content-type: text/html$/$/";print;

    With a little more work I could probably whip out a version that uses closures, DBD::mysql, and possibly ACME::Bleach


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

      I realise this was slightly obfuscated because of the nature of the question, but it's way beyond my understanding. I wonder if an explanation could be provided for those of us who watch in awe...

        I wonder if an explanation could be provided for those of us who watch in awe...

        Awe? You should look in the Obfuscation section some time and see truly obfuscated code. But basically this is what it does: first, it sets $= to an environment variable that (at least under Apache) indicates the length of the POSTed content, if any. Then, using the trinary conditional operator (foo ? bar : baz) it either reads the POSTed content from standard input (if there is any) or otherwise grabs the GET query string from another environment variable. Either way, it's now got whatever input is coming from the browser, which it then splits on ampersands (the standard CGI field separator), feeding the resulting list into the foreach loop. (I spelled foreach as for for brevity, a standard Perl Golf technique.) So, each time through the foreach loop you've got what amounts to the part of a CGI query between ampersands (or between the beginning and the first amp, or between the last and the end). The body of the loop undoes the character encoding that CGI does and splits it into a name/value pair. (CGI separates the name from the value with =.) The name and value get assigned to $: and $, respectively, but that's just temporary, since the very next statement uses these variables as a key/value pair which it stores in the %_ hash. I'll let you look up the special variables $\ and $/ if you don't know; the former is used for its special magic, and the latter is used for its default value. Anyway, the map turns the list of keys into a list of table rows with the keys and their respective values interpolated, and the whole thing gets sent to the browser. HTH.HAND.


        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: the search string and me
by LordWeber (Monk) on Sep 12, 2003 at 05:38 UTC
Re: the search string and me
by Cody Pendant (Prior) on Sep 12, 2003 at 04:47 UTC
    Did you say "member"?

    If you want to read some stuff from the query string, that's very simple. But "member" implies some kind of security system, where people log in with a username and password?

    If so you're in a whole other area.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
A reply falls below the community's threshold of quality. You may see it by logging in.