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

You know how every bad CGI script has a method for parsing GET and POST arguments? Split on equals, use a regex to restore URL-encoded characters to ASCII etc.

Let's assume I'm in a situation where I just can't, for reasons to annoying to go into here, install CGI.pm.

If I want to write a CGI script, what's the best way to grab the query string and put it into a hash?

Ovid's perl course has a great section on this: why use CGI.pm?. And I agree with everything he says.

But if I really can't, what should I do? Can I grab a section out of CGI.pm itself to use as a sub?



($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: What's the right way to do CGI wrong?
by polettix (Vicar) on Apr 23, 2007 at 00:29 UTC
    IIRC, many use CGI::Simple, which could be lighter on the reader and to those willing to just cut-and-paste something out of it.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: What's the right way to do CGI wrong?
by chromatic (Archbishop) on Apr 23, 2007 at 04:25 UTC
    Let's assume I'm in a situation where I just can't, for reasons to annoying to go into here, install CGI.pm.

    If it's not part of your Perl installation, your Perl installation is either so old as to have security problems (and it's been out of support for over a decade), or your Perl installation is so broken that you shouldn't trust it.

    (If you really can't fix things, like break-your-fingers-so-bad, then copying the guts of CGI::Simple probably is your best alternative.)

Re: What's the right way to do CGI wrong?
by Jenda (Abbot) on Apr 23, 2007 at 10:48 UTC

    You are only allowed to upload a single file to the host? If not, you can install CGI.pm. Just upload the CGI.pm and the CGI/Util.pm into a directory, point the script to that directory using use lib '/home/whatever/my/lib/; and you should be good to go.

Re: What's the right way to do CGI wrong?
by derby (Abbot) on Apr 23, 2007 at 12:27 UTC

    Start by looking at the parse_params method in CGI or _parse_params in CGI::Simple. For GETs, you pass $ENV{QUERY_STRING} to parse_params and for POSTs, first you have to read the data from STDIN and then, depending on the $ENV{CONTENT_TYPE}, you may pass the data to parse_params. By all means, do whatever you can to have CGI or CGI::Simple installed. If that's going to be a challenge you cannot overcome, the source for CGI::Simple is probably easier to grasp.

    -derby