X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/a05e48459ffcecb33d49ae011c57836e103f2f4f..6b8a2794cd62dd8d195b1d5c2699448cfd2be2c8:/pod/perlfaq9.pod diff --git a/pod/perlfaq9.pod b/pod/perlfaq9.pod index 2649372..fa9ef11 100644 --- a/pod/perlfaq9.pod +++ b/pod/perlfaq9.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq9 - Networking ($Revision: 1.26 $, $Date: 2005/11/21 17:43:13 $) +perlfaq9 - Networking =head1 DESCRIPTION @@ -22,7 +22,7 @@ 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 -programmers are strongly advised to use the CGI.pm module, to take care +programmers are strongly advised to use the C module, to take care of the details for them. The similarity between CGI response headers (defined in the CGI @@ -41,9 +41,9 @@ transaction response headers; the HTTP specification calls for records to be terminated with carriage-return and line-feed, i.e ASCII \015\012 written in binary mode. -Using CGI.pm gives excellent platform independence, including EBCDIC -systems. CGI.pm selects an appropriate newline representation -($CGI::CRLF) and sets binmode as appropriate. +Using C gives excellent platform independence, including EBCDIC +systems. C selects an appropriate newline representation +(C<$CGI::CRLF>) and sets binmode as appropriate. =head2 My CGI script runs from the command line but not the browser. (500 Server Error) @@ -65,33 +65,32 @@ listed in the CGI Meta FAQ: http://www.perl.org/CGI_MetaFAQ.html - =head2 How can I get better error messages from a CGI program? -Use the CGI::Carp module. It replaces C and C, plus the -normal Carp modules C, C, and C functions with +Use the C module. It replaces C and C, plus the +normal C modules C, C, and C functions with more verbose and safer versions. It still sends them to the normal server error log. - use CGI::Carp; - warn "This is a complaint"; - die "But this one is serious"; + use CGI::Carp; + warn "This is a complaint"; + die "But this one is serious"; -The following use of CGI::Carp also redirects errors to a file of your choice, -placed in a BEGIN block to catch compile-time warnings as well: +The following use of C also redirects errors to a file of your choice, +placed in a C block to catch compile-time warnings as well: - BEGIN { - use CGI::Carp qw(carpout); - open(LOG, ">>/var/local/cgi-logs/mycgi-log") - or die "Unable to append to mycgi-log: $!\n"; - carpout(*LOG); - } + BEGIN { + use CGI::Carp qw(carpout); + open(LOG, ">>/var/local/cgi-logs/mycgi-log") + or die "Unable to append to mycgi-log: $!\n"; + carpout(*LOG); + } You can even arrange for fatal errors to go back to the client browser, which is nice for your own debugging, but might confuse the end user. - use CGI::Carp qw(fatalsToBrowser); - die "Bad error here"; + use CGI::Carp qw(fatalsToBrowser); + die "Bad error here"; Even if the error happens before you get the HTTP header out, the module will try to take care of this to avoid the dreaded server 500 errors. @@ -101,9 +100,9 @@ stamp prepended. =head2 How do I remove HTML from a string? -The most correct way (albeit not the fastest) is to use HTML::Parser +The most correct way (albeit not the fastest) is to use C from CPAN. Another mostly correct -way is to use HTML::FormatText which not only removes HTML but also +way is to use C 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 @@ -114,8 +113,8 @@ entities--like C<<> for example. Here's one "simple-minded" approach, that works for most files: - #!/usr/bin/perl -p0777 - s/<(?:[^>'"]*|(['"]).*?\1)*>//gs + #!/usr/bin/perl -p0777 + s/<(?:[^>'"]*|(['"]).*?\1)*>//gs If you want a more complete solution, see the 3-stage striphtml program in @@ -125,25 +124,25 @@ http://www.cpan.org/authors/Tom_Christiansen/scripts/striphtml.gz Here are some tricky cases that you should think about when picking a solution: - A > B + A > B - A > B - + - + - <# Just data #> + <# Just data #> - >>>>>>>>>>> ]]> + >>>>>>>>>>> ]]> If HTML comments include other tags, those solutions would also break on text like this: - + =head2 How do I extract URLs? @@ -155,7 +154,7 @@ C or C. You might even use C as an example for something specifically suited to your needs. -You can use URI::Find to extract URLs from an arbitrary text document. +You can use C 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 @@ -163,14 +162,13 @@ 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. - #!/usr/bin/perl -n00 - # qxurl - tchrist@perl.com - print "$2\n" while m{ - < \s* - A \s+ HREF \s* = \s* (["']) (.*?) \1 - \s* > - }gsix; - + #!/usr/bin/perl -n00 + # qxurl - tchrist@perl.com + print "$2\n" while m{ + < \s* + A \s+ HREF \s* = \s* (["']) (.*?) \1 + \s* > + }gsix; =head2 How do I download a file from the user's machine? How do I open a file on another machine? @@ -178,51 +176,58 @@ 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 encoding. The CGI.pm module (which +known as B encoding. The C 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() +C method, which isn't the same as the C method. -See the section in the CGI.pm documentation on file uploads for code +See the section in the C documentation on file uploads for code examples and details. -=head2 How do I make a pop-up menu in HTML? +=head2 How do I make an HTML pop-up menu with Perl? + +(contributed by brian d foy) + +The C module (which comes with Perl) has functions to create +the HTML form widgets. See the C documentation for more +examples. + + use CGI qw/:standard/; + print header, + start_html('Favorite Animals'), -Use the B<<