fix some misinformation in perlfunc.pod
[p5sagit/p5-mst-13.2.git] / pod / perlfaq9.pod
index 9927152..9676380 100644 (file)
@@ -1,13 +1,13 @@
 =head1 NAME
 
-perlfaq9 - Networking ($Revision: 1.20 $, $Date: 1998/06/22 18:31:09 $)
+perlfaq9 - Networking ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $)
 
 =head1 DESCRIPTION
 
 This section deals with questions related to networking, the internet,
 and a few on the web.
 
-=head2 My CGI script runs from the command line but not the browser.   (500 Server Error)
+=head2 My CGI script runs from the command line but not the browser.  (500 Server Error)
 
 If you can demonstrate that you've read the following FAQs and that
 your problem isn't something simple that can be easily answered, you'll
@@ -20,10 +20,10 @@ may not be so well received.
 The useful FAQs and related documents are:
 
     CGI FAQ
-        http://www.webthing.com/page.cgi/cgifaq
+        http://www.webthing.com/tutorials/cgifaq.html
 
     Web FAQ
-    http://www.boutell.com/faq/
+        http://www.boutell.com/faq/
 
     WWW Security FAQ
         http://www.w3.org/Security/Faq/
@@ -76,15 +76,16 @@ stamp prepended.
 
 =head2 How do I remove HTML from a string?
 
-The most correct way (albeit not the fastest) is to use HTML::Parse
-from CPAN (part of the libwww-perl distribution, which is a must-have
-module for all web hackers).
+The most correct way (albeit not the fastest) is to use HTML::Parser
+from CPAN.  Another mostly correct
+way is to use HTML::FormatText which not only removes HTML but also
+attempts to do a little simple formatting of the resulting plain text.
 
 Many folks attempt a simple-minded regular expression approach, like
-C<s/E<lt>.*?E<gt>//g>, but that fails in many cases because the tags
+C<< s/<.*?>//g >>, but that fails in many cases because the tags
 may continue over line breaks, they may contain quoted angle-brackets,
-or HTML comment may be present.  Plus folks forget to convert
-entities, like C<&lt;> for example.
+or HTML comment may be present.  Plus, folks forget to convert
+entities--like C<&lt;> for example.
 
 Here's one "simple-minded" approach, that works for most files:
 
@@ -101,7 +102,7 @@ a solution:
 
     <IMG SRC = "foo.gif" ALT = "A > B">
 
-    <IMG SRC = "foo.gif" 
+    <IMG SRC = "foo.gif"
         ALT = "A > B">
 
     <!-- <A comment> -->
@@ -132,12 +133,11 @@ A quick but imperfect approach is
     }gsix;
 
 This version does not adjust relative URLs, understand alternate
-bases, deal with HTML comments, deal with HREF and NAME attributes in
-the same tag, or accept URLs themselves as arguments.  It also runs
-about 100x faster than a more "complete" solution using the LWP suite
-of modules, such as the
-http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz
-program.
+bases, deal with HTML comments, deal with HREF and NAME attributes
+in the same tag, understand extra qualifiers like TARGET, or accept
+URLs themselves as arguments.  It also runs about 100x faster than a
+more "complete" solution using the LWP suite of modules, such as the
+http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz program.
 
 =head2 How do I download a file from the user's machine?  How do I open a file on another machine?
 
@@ -148,7 +148,7 @@ the same as the startform() method.
 
 =head2 How do I make a pop-up menu in HTML?
 
-Use the B<E<lt>SELECTE<gt>> and B<E<lt>OPTIONE<gt>> tags.  The CGI.pm
+Use the B<< <SELECT> >> and B<< <OPTION> >> tags.  The CGI.pm
 module (available from CPAN) supports this widget, as well as many
 others, including some that it cleverly synthesizes on its own.
 
@@ -160,8 +160,9 @@ on your system, is this:
     $html_code = `lynx -source $url`;
     $text_data = `lynx -dump $url`;
 
-The libwww-perl (LWP) modules from CPAN provide a more powerful way to
-do this.  They work through proxies, and don't require lynx:
+The libwww-perl (LWP) modules from CPAN provide a more powerful way
+to do this.  They don't require lynx, but like lynx, can still work
+through proxies:
 
     # simplest version
     use LWP::Simple;
@@ -169,11 +170,12 @@ do this.  They work through proxies, and don't require lynx:
 
     # or print HTML from a URL
     use LWP::Simple;
-    getprint "http://www.sn.no/libwww-perl/";
+    getprint "http://www.linpro.no/lwp/";
 
     # or print ASCII from HTML from a URL
+    # also need HTML-Tree package from CPAN
     use LWP::Simple;
-    use HTML::Parse;
+    use HTML::Parser;
     use HTML::FormatText;
     my ($html, $ascii);
     $html = get("http://www.perl.com/");
@@ -207,27 +209,35 @@ the content appropriately.
 
 =head2 How do I decode or create those %-encodings on the web?
 
-Here's an example of decoding:
 
-    $string = "http://altavista.digital.com/cgi-bin/query?pg=q&what=news&fmt=.&q=%2Bcgi-bin+%2Bperl.exe";
-    $string =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge;
+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.
 
-Encoding is a bit harder, because you can't just blindly change
-all the non-alphanumunder character (C<\W>) into their hex escapes.
-It's important that characters with special meaning like C</> and C<?>
-I<not> be translated.  Probably the easiest way to get this right is
-to avoid reinventing the wheel and just use the URI::Escape module,
-which is part of the libwww-perl package (LWP) available from CPAN.
+
+The best source of detailed information on URI encoding is RFC 2396.
+Basically, the following substitutions do it:
+
+    s/([^\w()'*~!.-])/sprintf '%%%02x', $1/eg;   # encode
+
+    s/%([A-Fa-f\d]{2})/chr hex $1/eg;            # decode
+
+However, you should only apply them to individual URI components, not
+the entire URI, otherwise you'll lose information and generally mess
+things up.  If that didn't explain it, don't worry.  Just go read
+section 2 of the RFC, it's probably the best explanation there is.
+
+RFC 2396 also contains a lot of other useful information, including a
+regexp for breaking any arbitrary URI into components (Appendix B).
 
 =head2 How do I redirect to another page?
 
-Instead of sending back a C<Content-Type> as the headers of your
-reply, send back a C<Location:> header.  Officially this should be a
-C<URI:> header, so the CGI.pm module (available from CPAN) sends back
-both:
+According to RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1", the
+preferred method is to send a C<Location:> header instead of a
+C<Content-Type:> header:
 
     Location: http://www.domain.com/newpage
-    URI: http://www.domain.com/newpage
 
 Note that relative URLs in these headers can cause strange effects
 because of "optimizations" that servers do.
@@ -236,9 +246,21 @@ because of "optimizations" that servers do.
     print "Location: $url\n\n";
     exit;
 
-To be correct to the spec, each of those C<"\n">
-should really each be C<"\015\012">, but unless you're
-stuck on MacOS, you probably won't notice.
+To target a particular frame in a frameset, include the "Window-target:"
+in the header.
+
+    print <<EOF;
+    Location: http://www.domain.com/newpage
+    Window-target: <FrameName>
+
+    EOF
+
+To be correct to the spec, each of those virtual newlines should
+really be physical C<"\015\012"> sequences by the time your message is
+received by the client browser.  Except for NPH scripts, though, that
+local newline should get translated by your server into standard form,
+so you shouldn't have a problem here, even if you are stuck on MacOS.
+Everybody else probably won't even notice.
 
 =head2 How do I put a password on my web pages?
 
@@ -261,9 +283,9 @@ DBI compatible driver.  HTTPD::UserAdmin supports files used by the
 =head2 How do I make sure users can't enter values into a form that cause my CGI script to do bad things?
 
 Read the CGI security FAQ, at
-http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html, and the
+http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html , and the
 Perl/CGI FAQ at
-http://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html.
+http://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html .
 
 In brief: use tainting (see L<perlsec>), which makes sure that data
 from outside your script (eg, CGI parameters) are never used in
@@ -274,7 +296,7 @@ command and arguments as a list, which prevents shell globbing.
 =head2 How do I parse a mail header?
 
 For a quick-and-dirty solution, try this solution derived
-from page 222 of the 2nd edition of "Programming Perl":
+from L<perlfunc/split>:
 
     $/ = '';
     $header = <MSG>;
@@ -303,7 +325,7 @@ 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://www.bio.cam.ac.uk/web/form.html).
+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.
@@ -329,11 +351,11 @@ RFC-822 (the mail header standard) compliant, and addresses that aren't
 deliverable which are compliant.
 
 Many are tempted to try to eliminate many frequently-invalid
-mail addresses with a simple regexp, such as
-C</^[\w.-]+\@([\w.-]\.)+\w+$/>.  It's a very bad idea.  However,
+mail addresses with a simple regex, such as
+C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/>.  It's a very bad idea.  However,
 this also throws out many valid ones, and says nothing about
-potential deliverability, so is not suggested.  Instead, see
-http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz ,
+potential deliverability, so it is not suggested.  Instead, see
+http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz,
 which actually checks against the full RFC spec (except for nested
 comments), looks for addresses you may not wish to accept mail to
 (say, Bill Clinton or your postmaster), and then makes sure that the
@@ -366,13 +388,18 @@ with the characters reversed, one added or subtracted to each digit, etc.
 
 =head2 How do I decode a MIME/BASE64 string?
 
-The MIME-tools package (available from CPAN) handles this and a lot
-more.  Decoding BASE64 becomes as simple as:
+The MIME-Base64 package (available from CPAN) handles this as well as
+the MIME/QP encoding.  Decoding BASE64 becomes as simple as:
 
-    use MIME::base64;
+    use MIME::Base64;
     $decoded = decode_base64($encoded);
 
-A more direct approach is to use the unpack() function's "u"
+The MIME-Tools package (available from CPAN) supports extraction with
+decoding of BASE64 encoded attachments and content directly from email
+messages.
+
+If the string to decode is short (less than 84 bytes long)
+a more direct approach is to use the unpack() function's "u"
 format after minor transliterations:
 
     tr#A-Za-z0-9+/##cd;                   # remove non-base64 chars
@@ -382,12 +409,12 @@ format after minor transliterations:
 
 =head2 How do I return the user's mail address?
 
-On systems that support getpwuid, the $E<lt> variable and the
+On systems that support getpwuid, the $< variable, and the
 Sys::Hostname module (which is part of the standard perl distribution),
 you can probably try using something like this:
 
     use Sys::Hostname;
-    $address = sprintf('%s@%s', getpwuid($<), hostname);
+    $address = sprintf('%s@%s', scalar getpwuid($<), hostname);
 
 Company policies on mail address can mean that this generates addresses
 that the company's mail system will not accept, so you should ask for
@@ -411,7 +438,8 @@ Use the C<sendmail> program directly:
     To: Final Destination <you\@otherhost>
     Subject: A relevant subject line
 
-    Body of the message goes here, in as many lines as you like.
+    Body of the message goes here after the blank line
+    in as many lines as you like.
     EOF
     close(SENDMAIL)     or warn "sendmail didn't close nicely";
 
@@ -422,7 +450,12 @@ the message into the queue.  This last option means your message won't
 be immediately delivered, so leave it out if you want immediate
 delivery.
 
-Or use the CPAN module Mail::Mailer:
+Alternate, less convenient approaches include calling mail (sometimes
+called mailx) directly or simply opening up port 25 have having an
+intimate conversation between just you and the remote SMTP daemon,
+probably sendmail.
+
+Or you might be able use the CPAN module Mail::Mailer:
 
     use Mail::Mailer;
 
@@ -437,35 +470,51 @@ Or use the CPAN module Mail::Mailer:
 
 The Mail::Internet module uses Net::SMTP which is less Unix-centric than
 Mail::Mailer, but less reliable.  Avoid raw SMTP commands.  There
-are many reasons to use a mail transport agent like sendmail.  These 
+are many reasons to use a mail transport agent like sendmail.  These
 include queueing, MX records, and security.
 
+=head2 How do I use MIME to make an attachment to a mail message?
+
+This answer is extracted directly from the MIME::Lite documentation.
+Create a multipart message (i.e., one with attachments).
+
+    use MIME::Lite;
+
+    ### Create a new multipart message:
+    $msg = MIME::Lite->new(
+                 From    =>'me@myhost.com',
+                 To      =>'you@yourhost.com',
+                 Cc      =>'some@other.com, some@more.com',
+                 Subject =>'A message with 2 parts...',
+                 Type    =>'multipart/mixed'
+                 );
+
+    ### Add parts (each "attach" has same arguments as "new"):
+    $msg->attach(Type     =>'TEXT',
+                 Data     =>"Here's the GIF file you wanted"
+                 );
+    $msg->attach(Type     =>'image/gif',
+                 Path     =>'aaa000123.gif',
+                 Filename =>'logo.gif'
+                 );
+
+    $text = $msg->as_string;
+
+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
+SMTP via L<Net::SMTP>.
+
 =head2 How do I read mail?
 
-Use the Mail::Folder module from CPAN
-(part of the MailFolder package) or the Mail::Internet module from
-CPAN (also part of the MailTools package).
-
-   # sending mail
-    use Mail::Internet;
-    use Mail::Header;
-    # say which mail host to use
-    $ENV{SMTPHOSTS} = 'mail.frii.com';
-    # create headers
-    $header = new Mail::Header;
-    $header->add('From', 'gnat@frii.com');
-    $header->add('Subject', 'Testing');
-    $header->add('To', 'gnat@frii.com');
-    # create body
-    $body = 'This is a test, ignore';
-    # create mail object
-    $mail = new Mail::Internet(undef, Header => $header, Body => \[$body]);
-    # send it
-    $mail->smtpsend or die;
-
-Often a module is overkill, though.  Here's a mail sorter.
-
-    #!/usr/bin/perl 
+While you could use the Mail::Folder module from CPAN (part of the
+MailFolder package) or the Mail::Internet module from CPAN (also 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;
@@ -476,12 +525,12 @@ Often a module is overkill, though.  Here's a mail sorter.
             $sub[++$msgno] = lc($1) || '';
         }
         $msgs[$msgno] .= $_;
-    } 
+    }
     for my $i (sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msgs)) {
         print $msgs[$i];
     }
 
-Or more succinctly, 
+Or more succinctly,
 
     #!/usr/bin/perl -n00
     # bysub2 - awkish sort-by-subject
@@ -504,7 +553,7 @@ give you the hostname after which you can find out the IP address
     use Socket;
     use Sys::Hostname;
     my $host = hostname();
-    my $addr = inet_ntoa(scalar(gethostbyname($name)) || 'localhost');
+    my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost'));
 
 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
@@ -517,7 +566,7 @@ systems.)
 =head2 How do I fetch a news article or the active newsgroups?
 
 Use the Net::NNTP or News::NNTPClient modules, both available from CPAN.
-This can make tasks like fetching the newsgroup list as simple as:
+This can make tasks like fetching the newsgroup list as simple as
 
     perl -MNews::NNTPClient
       -e 'print News::NNTPClient->new->list("newsgroups")'
@@ -529,13 +578,14 @@ 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
+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).  No ONC::RPC module is known.
+CPAN).  The rpcgen suite, available from CPAN/authors/id/JAKE/, is
+an RPC stub generator and includes an RPC::ONC module.
 
 =head1 AUTHOR AND COPYRIGHT
 
-Copyright (c) 1997, 1998 Tom Christiansen and Nathan Torkington.
+Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington.
 All rights reserved.
 
 When included as part of the Standard Version of Perl, or as part of