X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq9.pod;h=ce0cf072419da3f6825e798bfbb9c5a9a7bfb935;hb=5b7d14ffe3988c7f70fd9dc76be9c44168d17a62;hp=3bf862f3ebda1c028deb1d6e53baa891e43834ef;hpb=24f1ba9b2223fc835b4bd6620a0213351e693b23;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq9.pod b/pod/perlfaq9.pod index 3bf862f..ce0cf07 100644 --- a/pod/perlfaq9.pod +++ b/pod/perlfaq9.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq9 - Networking ($Revision: 1.2 $, $Date: 2001/09/28 06:40:07 $) +perlfaq9 - Networking =head1 DESCRIPTION @@ -11,20 +11,19 @@ and a few on the web. (Alan Flavell 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 @@ -48,7 +47,12 @@ systems. CGI.pm selects an appropriate newline representation =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 FAQs and that +Several things could be wrong. You can go through the "Troubleshooting +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 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 @@ -56,12 +60,11 @@ 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 - =head2 How can I get better error messages from a CGI program? Use the CGI::Carp module. It replaces C and C, plus the @@ -69,25 +72,25 @@ normal Carp 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: - 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. @@ -110,147 +113,190 @@ 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 -http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz +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? -A quick but imperfect approach is +You can easily extract all sorts of URLs from HTML with +C 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 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. + +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. + + #!/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? -This version does not adjust relative URLs, understand alternate -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. +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 +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. -=head2 How do I download a file from the user's machine? How do I open a file on another machine? +See the section in the CGI.pm documentation on file uploads for code +examples and details. -In the context of an HTML form, you can use what's known as -B 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. +=head2 How do I make an HTML pop-up menu with Perl? -=head2 How do I make a pop-up menu in HTML? +(contributed by brian d foy) -Use the B<<