📄 perlfaq9.1
字号:
.PP.Vb 3\& <!\-\- This section commented out.\& <B>You can\*(Aqt see me!</B>\& \-\->.Ve.Sh "How do I extract URLs?".IX Subsection "How do I extract URLs?"You can easily extract all sorts of URLs from \s-1HTML\s0 with\&\f(CW\*(C`HTML::SimpleLinkExtor\*(C'\fR which handles anchors, images, objects,frames, and many other tags that can contain a \s-1URL\s0. If you needanything more complex, you can create your own subclass of\&\f(CW\*(C`HTML::LinkExtor\*(C'\fR or \f(CW\*(C`HTML::Parser\*(C'\fR. You might even use\&\f(CW\*(C`HTML::SimpleLinkExtor\*(C'\fR as an example for something specificallysuited to your needs..PPYou can use URI::Find to extract URLs from an arbitrary text document..PPLess complete solutions involving regular expressions can saveyou a lot of processing time if you know that the input is simple. Onesolution from Tom Christiansen runs 100 times faster than mostmodule based approaches but only extracts URLs from anchors where the firstattribute is \s-1HREF\s0 and there are no other attributes..PP.Vb 7\& #!/usr/bin/perl \-n00\& # qxurl \- tchrist@perl.com\& print "$2\en" while m{\& < \es*\& A \es+ HREF \es* = \es* (["\*(Aq]) (.*?) \e1\& \es* >\& }gsix;.Ve.Sh "How do I download a file from the user's machine? How do I open a file on another machine?".IX Subsection "How do I download a file from the user's machine? How do I open a file on another machine?"In this case, download means to use the file upload feature of \s-1HTML\s0forms. You allow the web surfer to specify a file to send to your webserver. To you it looks like a download, and to the user it lookslike an upload. No matter what you call it, you do it with what'sknown as \fBmultipart/form\-data\fR encoding. The \s-1CGI\s0.pm module (whichcomes with Perl as part of the Standard Library) supports this in the\&\fIstart_multipart_form()\fR method, which isn't the same as the \fIstartform()\fRmethod..PPSee the section in the \s-1CGI\s0.pm documentation on file uploads for codeexamples and details..Sh "How do I make an \s-1HTML\s0 pop-up menu with Perl?".IX Subsection "How do I make an HTML pop-up menu with Perl?"(contributed by brian d foy).PPThe \s-1CGI\s0.pm module (which comes with Perl) has functions to createthe \s-1HTML\s0 form widgets. See the \s-1CGI\s0.pm documentation for moreexamples..PP.Vb 3\& use CGI qw/:standard/;\& print header,\& start_html(\*(AqFavorite Animals\*(Aq),\&\& start_form,\& "What\*(Aqs your favorite animal? ",\& popup_menu(\& \-name => \*(Aqanimal\*(Aq,\& \-values => [ qw( Llama Alpaca Camel Ram ) ]\& ),\& submit,\&\& end_form,\& end_html;.Ve.Sh "How do I fetch an \s-1HTML\s0 file?".IX Subsection "How do I fetch an HTML file?"One approach, if you have the lynx text-based \s-1HTML\s0 browser installedon your system, is this:.PP.Vb 2\& $html_code = \`lynx \-source $url\`;\& $text_data = \`lynx \-dump $url\`;.Ve.PPThe libwww-perl (\s-1LWP\s0) modules from \s-1CPAN\s0 provide a more powerful wayto do this. They don't require lynx, but like lynx, can still workthrough proxies:.PP.Vb 3\& # simplest version\& use LWP::Simple;\& $content = get($URL);\&\& # or print HTML from a URL\& use LWP::Simple;\& 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::Parser;\& use HTML::FormatText;\& my ($html, $ascii);\& $html = get("http://www.perl.com/");\& defined $html\& or die "Can\*(Aqt fetch HTML from http://www.perl.com/";\& $ascii = HTML::FormatText\->new\->format(parse_html($html));\& print $ascii;.Ve.Sh "How do I automate an \s-1HTML\s0 form submission?".IX Subsection "How do I automate an HTML form submission?"If you are doing something complex, such as moving through many pagesand forms or a web site, you can use \f(CW\*(C`WWW::Mechanize\*(C'\fR. See itsdocumentation for all the details..PPIf you're submitting values using the \s-1GET\s0 method, create a \s-1URL\s0 and encodethe form using the \f(CW\*(C`query_form\*(C'\fR method:.PP.Vb 2\& use LWP::Simple;\& use URI::URL;\&\& my $url = url(\*(Aqhttp://www.perl.com/cgi\-bin/cpan_mod\*(Aq);\& $url\->query_form(module => \*(AqDB_File\*(Aq, readme => 1);\& $content = get($url);.Ve.PPIf you're using the \s-1POST\s0 method, create your own user agent and encodethe content appropriately..PP.Vb 2\& use HTTP::Request::Common qw(POST);\& use LWP::UserAgent;\&\& $ua = LWP::UserAgent\->new();\& my $req = POST \*(Aqhttp://www.perl.com/cgi\-bin/cpan_mod\*(Aq,\& [ module => \*(AqDB_File\*(Aq, readme => 1 ];\& $content = $ua\->request($req)\->as_string;.Ve.Sh "How do I decode or create those %\-encodings on the web?".IX Subsection "How do I decode or create those %-encodings on the web?"If you are writing a \s-1CGI\s0 script, you should be using the \s-1CGI\s0.pm modulethat comes with perl, or some other equivalent module. The \s-1CGI\s0 moduleautomatically decodes queries for you, and provides an \fIescape()\fRfunction to handle encoding..PPThe best source of detailed information on \s-1URI\s0 encoding is \s-1RFC\s0 2396.Basically, the following substitutions do it:.PP.Vb 1\& s/([^\ew()\*(Aq*~!.\-])/sprintf \*(Aq%%%02x\*(Aq, ord $1/eg; # encode\&\& s/%([A\-Fa\-f\ed]{2})/chr hex $1/eg; # decode\& s/%([[:xdigit:]]{2})/chr hex $1/eg; # same thing.Ve.PPHowever, you should only apply them to individual \s-1URI\s0 components, notthe entire \s-1URI\s0, otherwise you'll lose information and generally messthings up. If that didn't explain it, don't worry. Just go readsection 2 of the \s-1RFC\s0, it's probably the best explanation there is..PP\&\s-1RFC\s0 2396 also contains a lot of other useful information, including aregexp for breaking any arbitrary \s-1URI\s0 into components (Appendix B)..Sh "How do I redirect to another page?".IX Subsection "How do I redirect to another page?"Specify the complete \s-1URL\s0 of the destination (even if it is on the sameserver). This is one of the two different kinds of \s-1CGI\s0 \*(L"Location:\*(R"responses which are defined in the \s-1CGI\s0 specification for a Parsed Headersscript. The other kind (an absolute URLpath) is resolved internally tothe server without any \s-1HTTP\s0 redirection. The \s-1CGI\s0 specifications do notallow relative URLs in either case..PPUse of \s-1CGI\s0.pm is strongly recommended. This example shows redirectionwith a complete \s-1URL\s0. This redirection is handled by the web browser..PP.Vb 1\& use CGI qw/:standard/;\&\& my $url = \*(Aqhttp://www.cpan.org/\*(Aq;\& print redirect($url);.Ve.PPThis example shows a redirection with an absolute URLpath. Thisredirection is handled by the local web server..PP.Vb 2\& my $url = \*(Aq/CPAN/index.html\*(Aq;\& print redirect($url);.Ve.PPBut if coded directly, it could be as follows (the final \*(L"\en\*(R" isshown separately, for clarity), using either a complete \s-1URL\s0 oran absolute URLpath..PP.Vb 2\& print "Location: $url\en"; # CGI response header\& print "\en"; # end of headers.Ve.Sh "How do I put a password on my web pages?".IX Subsection "How do I put a password on my web pages?"To enable authentication for your web server, you need to configureyour web server. The configuration is different for different sortsof web servers\*(--apache does it differently from iPlanet which doesit differently from \s-1IIS\s0. Check your web server documentation forthe details for your particular server..Sh "How do I edit my .htpasswd and .htgroup files with Perl?".IX Subsection "How do I edit my .htpasswd and .htgroup files with Perl?"The HTTPD::UserAdmin and HTTPD::GroupAdmin modules provide aconsistent \s-1OO\s0 interface to these files, regardless of how they'restored. Databases may be text, dbm, Berkeley \s-1DB\s0 or any database witha \s-1DBI\s0 compatible driver. HTTPD::UserAdmin supports files used by the\&\*(L"Basic\*(R" and \*(L"Digest\*(R" authentication schemes. Here's an example:.PP.Vb 4\& use HTTPD::UserAdmin ();\& HTTPD::UserAdmin\& \->new(DB => "/foo/.htpasswd")\& \->add($username => $password);.Ve.Sh "How do I make sure users can't enter values into a form that cause my \s-1CGI\s0 script to do bad things?".IX Subsection "How do I make sure users can't enter values into a form that cause my CGI script to do bad things?"See the security references listed in the \s-1CGI\s0 Meta \s-1FAQ\s0.PP.Vb 1\& http://www.perl.org/CGI_MetaFAQ.html.Ve.Sh "How do I parse a mail header?".IX Subsection "How do I parse a mail header?"For a quick-and-dirty solution, try this solution derivedfrom \*(L"split\*(R" in perlfunc:.PP.Vb 4\& $/ = \*(Aq\*(Aq;\& $header = <MSG>;\& $header =~ s/\en\es+/ /g; # merge continuation lines\& %head = ( UNIX_FROM_LINE, split /^([\-\ew]+):\es*/m, $header );.Ve.PPThat solution doesn't do well if, for example, you're trying tomaintain all the Received lines. A more complete approach is to usethe Mail::Header module from \s-1CPAN\s0 (part of the MailTools package)..Sh "How do I decode a \s-1CGI\s0 form?".IX Subsection "How do I decode a CGI form?"(contributed by brian d foy).PPUse the \s-1CGI\s0.pm module that comes with Perl. It's quick,it's easy, and it actually does quite a bit of work toensure things happen correctly. It handles \s-1GET\s0, \s-1POST\s0, and\&\s-1HEAD\s0 requests, multipart forms, multivalued fields, querystring and message body combinations, and many other thingsyou probably don't want to think about..PPIt doesn't get much easier: the \s-1CGI\s0 module automaticallyparses the input and makes each value available through the\&\f(CW\*(C`param()\*(C'\fR function..PP.Vb 1\& use CGI qw(:standard);\&\& my $total = param( \*(Aqprice\*(Aq ) + param( \*(Aqshipping\*(Aq );\&\& my @items = param( \*(Aqitem\*(Aq ); # multiple values, same field name.Ve.PPIf you want an object-oriented approach, \s-1CGI\s0.pm can do that too..PP.Vb 1\& use CGI;\&\& my $cgi = CGI\->new();\&\& my $total = $cgi\->param( \*(Aqprice\*(Aq ) + $cgi\->param( \*(Aqshipping\*(Aq );\&\& my @items = $cgi\->param( \*(Aqitem\*(Aq );.Ve.PPYou might also try CGI::Minimal which is a lightweight versionof the same thing. Other CGI::* modules on \s-1CPAN\s0 might work betterfor you, too..PPMany people try to write their own decoder (or copy one fromanother program) and then run into one of the many \*(L"gotchas\*(R"of the task. It's much easier and less hassle to use \s-1CGI\s0.pm..Sh "How do I check a valid mail address?".IX Subsection "How do I check a valid mail address?"(partly contributed by Aaron Sherman).PPThis isn't as simple a question as it sounds. There are two parts:.PPa) How do I verify that an email address is correctly formatted?.PPb) How do I verify that an email address targets a valid recipient?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -