perlfaq9.html
来自「perl教程」· HTML 代码 · 共 717 行 · 第 1/4 页
HTML
717 行
ALT = "A > B"></pre>
<pre>
<!-- <A comment> --></pre>
<pre>
<script>if (a<b && a>c)</script></pre>
<pre>
<# Just data #></pre>
<pre>
<![INCLUDE CDATA [ >>>>>>>>>>>> ]]></pre>
<p>If HTML comments include other tags, those solutions would also break
on text like this:</p>
<pre>
<!-- This section commented out.
<B>You can't see me!</B>
--></pre>
<p>
</p>
<h2><a name="how_do_i_extract_urls">How do I extract URLs?</a></h2>
<p>You can easily extract all sorts of URLs from HTML with
<code>HTML::SimpleLinkExtor</code> 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
<code>HTML::LinkExtor</code> or <code>HTML::Parser</code>. You might even use
<code>HTML::SimpleLinkExtor</code> as an example for something specifically
suited to your needs.</p>
<p>You can use URI::Find to extract URLs from an arbitrary text document.</p>
<p>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.</p>
<pre>
<span class="comment">#!/usr/bin/perl -n00</span>
<span class="comment"># qxurl - tchrist@perl.com</span>
<span class="keyword">print</span> <span class="string">"$2\n"</span> <span class="keyword">while</span> <span class="regex">m{
< \s*
A \s+ HREF \s* = \s* (["']) (.*?) \1
\s* >
}gsix</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_download_a_file_from_the_user_s_machine_how_do_i_open_a_file_on_another_machine">How do I download a file from the user's machine? How do I open a file on another machine?</a></h2>
<p>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 <strong>multipart/form-data</strong> encoding. The CGI.pm module (which
comes with Perl as part of the Standard Library) supports this in the
<code>start_multipart_form()</code> method, which isn't the same as the <code>startform()</code>
method.</p>
<p>See the section in the CGI.pm documentation on file uploads for code
examples and details.</p>
<p>
</p>
<h2><a name="how_do_i_make_a_popup_menu_in_html">How do I make a pop-up menu in HTML?</a></h2>
<p>Use the <strong><SELECT</strong> >> and <strong><OPTION</strong> >> 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.</p>
<p>
</p>
<h2><a name="how_do_i_fetch_an_html_file">How do I fetch an HTML file?</a></h2>
<p>One approach, if you have the lynx text-based HTML browser installed
on your system, is this:</p>
<pre>
<span class="variable">$html_code</span> <span class="operator">=</span> <span class="string">`lynx -source $url`</span><span class="operator">;</span>
<span class="variable">$text_data</span> <span class="operator">=</span> <span class="string">`lynx -dump $url`</span><span class="operator">;</span>
</pre>
<p>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:</p>
<pre>
<span class="comment"># simplest version</span>
<span class="keyword">use</span> <span class="variable">LWP::Simple</span><span class="operator">;</span>
<span class="variable">$content</span> <span class="operator">=</span> <span class="variable">get</span><span class="operator">(</span><span class="variable">$URL</span><span class="operator">);</span>
</pre>
<pre>
<span class="comment"># or print HTML from a URL</span>
<span class="keyword">use</span> <span class="variable">LWP::Simple</span><span class="operator">;</span>
<span class="variable">getprint</span> <span class="string">"http://www.linpro.no/lwp/"</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># or print ASCII from HTML from a URL</span>
<span class="comment"># also need HTML-Tree package from CPAN</span>
<span class="keyword">use</span> <span class="variable">LWP::Simple</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">HTML::Parser</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">HTML::FormatText</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$html</span><span class="operator">,</span> <span class="variable">$ascii</span><span class="operator">);</span>
<span class="variable">$html</span> <span class="operator">=</span> <span class="variable">get</span><span class="operator">(</span><span class="string">"http://www.perl.com/"</span><span class="operator">);</span>
<span class="keyword">defined</span> <span class="variable">$html</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Can't fetch HTML from http://www.perl.com/"</span><span class="operator">;</span>
<span class="variable">$ascii</span> <span class="operator">=</span> <span class="variable">HTML::FormatText</span><span class="operator">-></span><span class="variable">new</span><span class="operator">-></span><span class="keyword">format</span><span class="operator">(</span><span class="variable">parse_html</span><span class="operator">(</span><span class="variable">$html</span><span class="operator">));</span>
<span class="keyword">print</span> <span class="variable">$ascii</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_automate_an_html_form_submission">How do I automate an HTML form submission?</a></h2>
<p>If you are doing something complex, such as moving through many pages
and forms or a web site, you can use <code>WWW::Mechanize</code>. See its
documentation for all the details.</p>
<p>If you're submitting values using the GET method, create a URL and encode
the form using the <code>query_form</code> method:</p>
<pre>
<span class="keyword">use</span> <span class="variable">LWP::Simple</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">URI::URL</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$url</span> <span class="operator">=</span> <span class="variable">url</span><span class="operator">(</span><span class="string">'http://www.perl.com/cgi-bin/cpan_mod'</span><span class="operator">);</span>
<span class="variable">$url</span><span class="operator">-></span><span class="variable">query_form</span><span class="operator">(</span><span class="string">module</span> <span class="operator">=></span> <span class="string">'DB_File'</span><span class="operator">,</span> <span class="string">readme</span> <span class="operator">=></span> <span class="number">1</span><span class="operator">);</span>
<span class="variable">$content</span> <span class="operator">=</span> <span class="variable">get</span><span class="operator">(</span><span class="variable">$url</span><span class="operator">);</span>
</pre>
<p>If you're using the POST method, create your own user agent and encode
the content appropriately.</p>
<pre>
<span class="keyword">use</span> <span class="variable">HTTP::Request::Common</span> <span class="string">qw(POST)</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">LWP::UserAgent</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$ua</span> <span class="operator">=</span> <span class="variable">LWP::UserAgent</span><span class="operator">-></span><span class="variable">new</span><span class="operator">();</span>
<span class="keyword">my</span> <span class="variable">$req</span> <span class="operator">=</span> <span class="variable">POST</span> <span class="string">'http://www.perl.com/cgi-bin/cpan_mod'</span><span class="operator">,</span>
<span class="operator">[</span> <span class="string">module</span> <span class="operator">=></span> <span class="string">'DB_File'</span><span class="operator">,</span> <span class="string">readme</span> <span class="operator">=></span> <span class="number">1</span> <span class="operator">]</span><span class="operator">;</span>
<span class="variable">$content</span> <span class="operator">=</span> <span class="variable">$ua</span><span class="operator">-></span><span class="variable">request</span><span class="operator">(</span><span class="variable">$req</span><span class="operator">)-></span><span class="variable">as_string</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_decode_or_create_those__encodings_on_the_web">How do I decode or create those %-encodings on the web?</a></h2>
<p>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 <code>escape()</code>
function to handle encoding.</p>
<p>The best source of detailed information on URI encoding is RFC 2396.
Basically, the following substitutions do it:</p>
<pre>
<span class="regex">s/([^\w()'*~!.-])/sprintf '%%%02x', ord $1/eg</span><span class="operator">;</span> <span class="comment"># encode</span>
</pre>
<pre>
<span class="regex">s/%([A-Fa-f\d]{2})/chr hex $1/eg</span><span class="operator">;</span> <span class="comment"># decode</span>
<span class="regex">s/%([[:xdigit:]]{2})/chr hex $1/eg</span><span class="operator">;</span> <span class="comment"># same thing</span>
</pre>
<p>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.</p>
<p>RFC 2396 also contains a lot of other useful information, including a
regexp for breaking any arbitrary URI into components (Appendix B).</p>
<p>
</p>
<h2><a name="how_do_i_redirect_to_another_page">How do I redirect to another page?</a></h2>
<p>Specify the complete URL of the destination (even if it is on the same
server). This is one of the two different kinds of CGI "Location:"
responses which are defined in the CGI specification for a Parsed Headers
script. The other kind (an absolute URLpath) is resolved internally to
the server without any HTTP redirection. The CGI specifications do not
allow relative URLs in either case.</p>
<p>Use of CGI.pm is strongly recommended. This example shows redirection
with a complete URL. This redirection is handled by the web browser.</p>
<pre>
<span class="keyword">use</span> <span class="variable">CGI</span> <span class="string">qw/:standard/</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$url</span> <span class="operator">=</span> <span class="string">'http://www.cpan.org/'</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">redirect</span><span class="operator">(</span><span class="variable">$url</span><span class="operator">);</span>
</pre>
<p>This example shows a redirection with an absolute URLpath. This
redirection is handled by the local web server.</p>
<pre>
<span class="keyword">my</span> <span class="variable">$url</span> <span class="operator">=</span> <span class="string">'/CPAN/index.html'</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">redirect</span><span class="operator">(</span><span class="variable">$url</span><span class="operator">);</span>
</pre>
<p>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.</p>
<pre>
<span class="keyword">print</span> <span class="string">"Location: $url\n"</span><span class="operator">;</span> <span class="comment"># CGI response header</span>
<span class="keyword">print</span> <span class="string">"\n"</span><span class="operator">;</span> <span class="comment"># end of headers</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_put_a_password_on_my_web_pages">How do I put a password on my web pages?</a></h2>
<p>To enable authentication for your web server, you need to configure
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?