cvs commit: perlfaq perlfaq9.pod

[email protected] (brian d foy) 21 Jan 2005 12:14:13 -0000
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
cvsuser     05/01/21 04:14:12

  Modified:    .        perlfaq9.pod
  Log:
  * How do I decode a CGI form?
  
     + I'm completely replacing this answer.  The previous version
     was pretty aggressive and ranted about cargo-culting.  It also
     digressed into a discussion of HTTP methods.
  
     + The new answer showcases CGI.pm, and gives a couple examples.
     I'd like people to use it because it's easier, not because we
     decreed it's use.
  
     + I also removed the reference to cgi-lib.pl.  It is 2005 now :)
  
  Revision  Changes    Path
  1.19      +36 -29    perlfaq/perlfaq9.pod
  
  Index: perlfaq9.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq9.pod,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- perlfaq9.pod	3 Jan 2005 18:43:37 -0000	1.18
  +++ perlfaq9.pod	21 Jan 2005 12:14:12 -0000	1.19
  @@ -1,6 +1,6 @@
   =head1 NAME
   
  -perlfaq9 - Networking ($Revision: 1.18 $, $Date: 2005/01/03 18:43:37 $)
  +perlfaq9 - Networking ($Revision: 1.19 $, $Date: 2005/01/21 12:14:12 $)
   
   =head1 DESCRIPTION
   
  @@ -352,35 +352,42 @@
   
   =head2 How do I decode a CGI form?
   
  -You use a standard module, probably CGI.pm.  Under no circumstances
  -should you attempt to do so by hand!
  +(contributed by brian d foy)
   
  -You'll see a lot of CGI programs that blindly read from STDIN the number
  -of bytes equal to CONTENT_LENGTH for POSTs, or grab QUERY_STRING for
  -decoding GETs.  These programs are very poorly written.  They only work
  -sometimes.  They typically forget to check the return value of the read()
  -system call, which is a cardinal sin.  They don't handle HEAD requests.
  -They don't handle multipart forms used for file uploads.  They don't deal
  -with GET/POST combinations where query fields are in more than one place.
  -They don't deal with keywords in the query string.
  -
  -In short, they're bad hacks.  Resist them at all costs.  Please do not be
  -tempted to reinvent the wheel.  Instead, use the CGI.pm or CGI_Lite.pm
  -(available from CPAN), or if you're trapped in the module-free land
  -of perl1 .. perl4, you might look into cgi-lib.pl (available from
  -http://cgi-lib.stanford.edu/cgi-lib/ ).
  -
  -Make sure you know whether to use a GET or a POST in your form.
  -GETs should only be used for something that doesn't update the server.
  -Otherwise you can get mangled databases and repeated feedback mail
  -messages.  The fancy word for this is ``idempotency''.  This simply
  -means that there should be no difference between making a GET request
  -for a particular URL once or multiple times.  This is because the
  -HTTP protocol definition says that a GET request may be cached by the
  -browser, or server, or an intervening proxy.  POST requests cannot be
  -cached, because each request is independent and matters.  Typically,
  -POST requests change or depend on state on the server (query or update
  -a database, send mail, or purchase a computer).
  +Use the CGI.pm module that comes with Perl.  It's quick,
  +it's easy, and it actually does quite a bit of work to
  +ensure things happen correctly.  It handles GET, POST, and
  +HEAD requests, multipart forms, multivalued fields, query
  +string and message body combinations, and many other things
  +you probably don't want to think about.
  +
  +It doesn't get much easier: the CGI module automatically
  +parses the input and makes each value available through the
  +C<param()> function.
  +
  +	use CGI qw(:standard);
  +	
  +	my $total = param( "price" ) + param( "shipping" );
  +	
  +	my @items = param( "item ); # multiple values, same field name
  +	
  +If you want an object-oriented approach, CGI.pm can do that too.
  +
  +	use CGI;
  +	
  +	my $cgi = CGI->new();
  +	
  +	my $total = $cgi->param( "price" ) + $cgi->param( "shipping" );
  +	
  +	my @items = $cgi->param( "item" );
  +
  +You might also try CGI::Minimal which is a lightweight version
  +of the same thing.  Other CGI::* modules on CPAN might work better
  +for you, too.
  +
  +Many people try to write their own decoder (or copy one from
  +another program) and then run into one of the many "gotchas"
  +of the task.  It's much easier and less hassle to use CGI.pm.
   
   =head2 How do I check a valid mail address?