in reply to Pushing w/ an associative array?

Partly to join the chorus, I have to say "USE CGI.PM"!!! You don't want to do this on your own. In fact, you have a bug in the small snippet that you have posted -- well, you might have corrected for the bug elsewhere, but I have seen this so many times that I'm willing to wager a large sum of money that you haven't: you don't check to ensure that the length of data read from STDIN is the same as $ENV{'CONTENT_LENGTH'}

What happens if the user hits the "stop" button on their browser? What if they have a power failure? What if the Web server screws up? For any of these and other conditions, the data that you read may be corrupted and you need to verify that it's not by checking its length. Pardon me for seeming pedantic, but a good programmer looks both ways before crossing a one-way street.

If you'd like to learn more about the pitfalls of this, you can check out this link where I disect "hand-rolled" CGI parsers. I go into quite a bit of detail and by the time you're done reading it, you should have a good idea why writing your own is likely to have problems. Or, at the very least, you'll have a better idea of how to write your own (but I wouldn't recommend it -- you won't catch all of the browser anomolies).

I am curious about one thing, though. Why would you want to write your own version? Were you unaware of the alternatives or were you concerned about performance? If the latter, you can also check out CGI::Lite.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

  • Comment on (Ovid -- bug in your hand-rolled CGI code) Re: Pushing w/ an associative array?

Replies are listed 'Best First'.
Re: (Ovid -- bug in your hand-rolled CGI code) Re: Pushing w/ an associative array?
by Anonymous Monk on Dec 27, 2000 at 21:37 UTC
    How can I read the form input with CGI.pm, then? I still need to read in each variable name and put it into an array for sorting...

      Calling param with no arguments will return a list of all of the CGI arguments. You can therefore do something like this:

      use CGI qw(:standard); my %form; foreach (param) { my @args = param($_); if (@args == 1) { $form{$_} = $args[0]; } else { $form{$_} = [@args]; } }
      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

        This doesnt seem to provide the variable name, though; the only thing I'm getting is the value of the variable. Can I get both with CGI.pm?