This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regenerated mktables.lst per Yves Orton's suggestion.
[perl5.git] / pod / perlfaq9.pod
index 947c769..8faf2fb 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq9 - Networking ($Revision: 1.7 $, $Date: 2002/01/28 04:17:27 $)
+perlfaq9 - Networking ($Revision: 6309 $)
 
 =head1 DESCRIPTION
 
@@ -11,20 +11,19 @@ and a few on the web.
 
 (Alan Flavell <flavell+www@a5.ph.gla.ac.uk> answers...)
 
-The Common Gateway Interface (CGI) specifies a software interface between 
-a program ("CGI script") and a web server (HTTPD). It is not specific 
-to Perl, and has its own FAQs and tutorials, and usenet group, 
-comp.infosystems.www.authoring.cgi 
+The Common Gateway Interface (CGI) specifies a software interface between
+a program ("CGI script") and a web server (HTTPD). It is not specific
+to Perl, and has its own FAQs and tutorials, and usenet group,
+comp.infosystems.www.authoring.cgi
 
-The original CGI specification is at: http://hoohoo.ncsa.uiuc.edu/cgi/
-
-Current best-practice RFC draft at: http://CGI-Spec.Golux.Com/
+The CGI specification is outlined in an informational RFC:
+http://www.ietf.org/rfc/rfc3875
 
 Other relevant documentation listed in: http://www.perl.org/CGI_MetaFAQ.html
 
-These Perl FAQs very selectively cover some CGI issues. However, Perl 
+These Perl FAQs very selectively cover some CGI issues. However, Perl
 programmers are strongly advised to use the CGI.pm module, to take care
-of the details for them. 
+of the details for them.
 
 The similarity between CGI response headers (defined in the CGI
 specification) and HTTP response headers (defined in the HTTP
@@ -53,7 +52,7 @@ Perl CGI scripts" guide at
 
        http://www.perl.org/troubleshooting_CGI.html
 
-If, after that, you can demonstrate that you've read the FAQs and that 
+If, after that, you can demonstrate that you've read the FAQs and that
 your problem isn't something simple that can be easily answered, you'll
 probably receive a courteous and useful reply to your question if you
 post it on comp.infosystems.www.authoring.cgi (if it's something to do
@@ -61,7 +60,7 @@ with HTTP or the CGI protocols).  Questions that appear to be Perl
 questions but are really CGI ones that are posted to comp.lang.perl.misc
 are not so well received.
 
-The useful FAQs, related documents, and troubleshooting guides are 
+The useful FAQs, related documents, and troubleshooting guides are
 listed in the CGI Meta FAQ:
 
        http://www.perl.org/CGI_MetaFAQ.html
@@ -150,17 +149,19 @@ on text like this:
 
 You can easily extract all sorts of URLs from HTML with
 C<HTML::SimpleLinkExtor> which handles anchors, images, objects,
-frames, and many other tags that can contain a URL.  If you need 
-anything more complex, you can create your own subclass of 
-C<HTML::LinkExtor> or C<HTML::Parser>.  You might even use 
+frames, and many other tags that can contain a URL.  If you need
+anything more complex, you can create your own subclass of
+C<HTML::LinkExtor> or C<HTML::Parser>.  You might even use
 C<HTML::SimpleLinkExtor> as an example for something specifically
 suited to your needs.
 
-Less complete solutions involving regular expressions can save 
+You can use URI::Find to extract URLs from an arbitrary text document.
+
+Less complete solutions involving regular expressions can save
 you a lot of processing time if you know that the input is simple.  One
 solution from Tom Christiansen runs 100 times faster than most
 module based approaches but only extracts URLs from anchors where the first
-attribute is HREF and there are no other attributes. 
+attribute is HREF and there are no other attributes.
 
         #!/usr/bin/perl -n00
         # qxurl - tchrist@perl.com
@@ -173,10 +174,17 @@ attribute is HREF and there are no other attributes.
 
 =head2 How do I download a file from the user's machine?  How do I open a file on another machine?
 
-In the context of an HTML form, you can use what's known as
-B<multipart/form-data> encoding.  The CGI.pm module (available from
-CPAN) supports this in the start_multipart_form() method, which isn't
-the same as the startform() method.
+In this case, download means to use the file upload feature of HTML
+forms.  You allow the web surfer to specify a file to send to your web
+server.  To you it looks like a download, and to the user it looks
+like an upload.  No matter what you call it, you do it with what's
+known as B<multipart/form-data> encoding.  The CGI.pm module (which
+comes with Perl as part of the Standard Library) supports this in the
+start_multipart_form() method, which isn't the same as the startform()
+method.
+
+See the section in the CGI.pm documentation on file uploads for code
+examples and details.
 
 =head2 How do I make a pop-up menu in HTML?
 
@@ -218,6 +226,10 @@ through proxies:
 
 =head2 How do I automate an HTML form submission?
 
+If you are doing something complex, such as moving through many pages
+and forms or a web site, you can use C<WWW::Mechanize>.  See its
+documentation for all the details.
+
 If you're submitting values using the GET method, create a URL and encode
 the form using the C<query_form> method:
 
@@ -241,19 +253,18 @@ the content appropriately.
 
 =head2 How do I decode or create those %-encodings on the web?
 
-
 If you are writing a CGI script, you should be using the CGI.pm module
 that comes with perl, or some other equivalent module.  The CGI module
 automatically decodes queries for you, and provides an escape()
 function to handle encoding.
 
-
 The best source of detailed information on URI encoding is RFC 2396.
 Basically, the following substitutions do it:
 
     s/([^\w()'*~!.-])/sprintf '%%%02x', ord $1/eg;   # encode
 
     s/%([A-Fa-f\d]{2})/chr hex $1/eg;                # decode
+       s/%([[:xdigit:]]{2})/chr hex $1/eg;          # same thing
 
 However, you should only apply them to individual URI components, not
 the entire URI, otherwise you'll lose information and generally mess
@@ -288,9 +299,9 @@ redirection is handled by the local web server.
       print redirect($url);
 
 
-But if coded directly, it could be as follows (the final "\n" is 
+But if coded directly, it could be as follows (the final "\n" is
 shown separately, for clarity), using either a complete URL or
-an absolute URLpath. 
+an absolute URLpath.
 
       print "Location: $url\n";   # CGI response header
       print "\n";                 # end of headers
@@ -298,8 +309,11 @@ an absolute URLpath.
 
 =head2 How do I put a password on my web pages?
 
-That depends.  You'll need to read the documentation for your web
-server, or perhaps check some of the other FAQs referenced above.
+To enable authentication for your web server, you need to configure
+your web server.  The configuration is different for different sorts
+of web servers--apache does it differently from iPlanet which does
+it differently from IIS.  Check your web server documentation for
+the details for your particular server.
 
 =head2 How do I edit my .htpasswd and .htgroup files with Perl?
 
@@ -307,7 +321,7 @@ The HTTPD::UserAdmin and HTTPD::GroupAdmin modules provide a
 consistent OO interface to these files, regardless of how they're
 stored.  Databases may be text, dbm, Berkeley DB or any database with
 a DBI compatible driver.  HTTPD::UserAdmin supports files used by the
-`Basic' and `Digest' authentication schemes.  Here's an example:
+"Basic" and "Digest" authentication schemes.  Here's an example:
 
     use HTTPD::UserAdmin ();
     HTTPD::UserAdmin
@@ -336,47 +350,62 @@ the Mail::Header module from CPAN (part of the MailTools package).
 
 =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!
-
-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).
+(contributed by brian d foy)
+
+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?
 
 You can't, at least, not in real time.  Bummer, eh?
 
 Without sending mail to the address and seeing whether there's a human
-on the other hand to answer you, you cannot determine whether a mail
+on the other end to answer you, you cannot determine whether a mail
 address is valid.  Even if you apply the mail header standard, you
 can have problems, because there are deliverable addresses that aren't
 RFC-822 (the mail header standard) compliant, and addresses that aren't
 deliverable which are compliant.
 
+You can use the Email::Valid or RFC::RFC822::Address which check
+the format of the address, although they cannot actually tell you
+if it is a deliverable address (i.e. that mail to the address
+will not bounce).  Modules like Mail::CheckUser and Mail::EXPN
+try to interact with the domain name system or particular
+mail servers to learn even more, but their methods do not
+work everywhere--especially for security conscious administrators.
+
 Many are tempted to try to eliminate many frequently-invalid
 mail addresses with a simple regex, such as
 C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/>.  It's a very bad idea.  However,
@@ -409,7 +438,7 @@ A related strategy that's less open to forgery is to give them a PIN
 (personal ID number).  Record the address and PIN (best that it be a
 random one) for later processing.  In the mail you send, ask them to
 include the PIN in their reply.  But if it bounces, or the message is
-included via a ``vacation'' script, it'll be there anyway.  So it's
+included via a "vacation" script, it'll be there anyway.  So it's
 best to ask them to mail back a slight alteration of the PIN, such as
 with the characters reversed, one added or subtracted to each digit, etc.
 
@@ -531,23 +560,23 @@ MIME::Lite also includes a method for sending these things.
 
     $msg->send;
 
-This defaults to using L<sendmail(1)> but can be customized to use
+This defaults to using L<sendmail> but can be customized to use
 SMTP via L<Net::SMTP>.
 
 =head2 How do I read mail?
 
 While you could use the Mail::Folder module from CPAN (part of the
-MailFolder package) or the Mail::Internet module from CPAN (also part
+MailFolder package) or the Mail::Internet module from CPAN (part
 of the MailTools package), often a module is overkill.  Here's a
 mail sorter.
 
     #!/usr/bin/perl
-    # bysub1 - simple sort by subject
+
     my(@msgs, @sub);
     my $msgno = -1;
     $/ = '';                    # paragraph reads
     while (<>) {
-        if (/^From/m) {
+        if (/^From /m) {
             /^Subject:\s*(?:Re:\s*)*(.*)/mi;
             $sub[++$msgno] = lc($1) || '';
         }
@@ -566,29 +595,37 @@ Or more succinctly,
     $msg[$msgno] .= $_;
     END { print @msg[ sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msg) ] }
 
-=head2 How do I find out my hostname/domainname/IP address?
+=head2 How do I find out my hostname, domainname, or IP address?
+X<hostname, domainname, IP address, host, domain, hostfqdn, inet_ntoa,
+gethostbyname, Socket, Net::Domain, Sys::Hostname>
 
-The normal way to find your own hostname is to call the C<`hostname`>
-program.  While sometimes expedient, this has some problems, such as
-not knowing whether you've got the canonical name or not.  It's one of
-those tradeoffs of convenience versus portability.
+(contributed by brian d foy)
 
-The Sys::Hostname module (part of the standard perl distribution) will
-give you the hostname after which you can find out the IP address
-(assuming you have working DNS) with a gethostbyname() call.
+The Net::Domain module, which is part of the standard distribution starting
+in perl5.7.3, can get you the fully qualified domain name (FQDN), the host
+name, or the domain name.
 
-    use Socket;
-    use Sys::Hostname;
-    my $host = hostname();
-    my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost'));
+       use Net::Domain qw(hostname hostfqdn hostdomain);
+
+       my $host = hostfqdn();
+
+The C<Sys::Hostname> module, included in the standard distribution since
+perl5.6, can also get the hostname.
+
+       use Sys::Hostname;
 
-Probably the simplest way to learn your DNS domain name is to grok
-it out of /etc/resolv.conf, at least under Unix.  Of course, this
-assumes several things about your resolv.conf configuration, including
-that it exists.
+       $host = hostname();
 
-(We still need a good DNS domain name-learning method for non-Unix
-systems.)
+To get the IP address, you can use the C<gethostbyname> built-in function
+to turn the name into a number. To turn that number into the dotted octet
+form (a.b.c.d) that most people expect, use the C<inet_ntoa> function
+from the <Socket> module, which also comes with perl.
+
+    use Socket;
+
+    my $address = inet_ntoa(
+       scalar gethostbyname( $host || 'localhost' )
+       );
 
 =head2 How do I fetch a news article or the active newsgroups?
 
@@ -605,15 +642,23 @@ available from CPAN) is more complex but can put as well as fetch.
 
 =head2 How can I do RPC in Perl?
 
-A DCE::RPC module is being developed (but is not yet available) and
-will be released as part of the DCE-Perl package (available from
-CPAN).  The rpcgen suite, available from CPAN/authors/id/JAKE/, is
-an RPC stub generator and includes an RPC::ONC module.
+(Contributed by brian d foy)
+
+Use one of the RPC modules you can find on CPAN (
+http://search.cpan.org/search?query=RPC&mode=all ).
+
+=head1 REVISION
+
+Revision: $Revision: 6309 $
+
+Date: $Date: 2006-05-18 07:44:45 +0200 (jeu, 18 mai 2006) $
+
+See L<perlfaq> for source control details and availability.
 
 =head1 AUTHOR AND COPYRIGHT
 
-Copyright (c) 1997-2002 Tom Christiansen and Nathan Torkington.
-All rights reserved.
+Copyright (c) 1997-2006 Tom Christiansen, Nathan Torkington, and
+other authors as noted. All rights reserved.
 
 This documentation is free; you can redistribute it and/or modify it
 under the same terms as Perl itself.