📄 ch20_05.htm
字号:
<html><head><title>The URI Module (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch20_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="part9.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">20.5. The URI Module</h2><a name="INDEX-2626" /><a name="INDEX-2627" /><p><a name="INDEX-2628" /><a name="INDEX-2629" />TheURI module contains functions and modules to specify and convertURIs. (URLs are a type of URI.) In addition to the URL module itself,there are also: URI::URL, URI::Escape, and URI::Heuristic. Of primaryimportance to many LWP applications is the URI::URL class, whichcreates the objects used by LWP::UserAgent to determine protocols,server locations, and resource names.</p><p><a name="INDEX-2630" /><a name="INDEX-2631" /><a name="INDEX-2632" /><a name="INDEX-2633" />TheURI::Escape module replaces unsafe characters in URL strings withtheir appropriate escape sequences. URI::Heuristic providesconvenience methods for creating proper URLs out of short strings andincomplete addresses.</p><a name="perlnut2-CHP-20-SECT-5.1" /><div class="sect2"><h3 class="sect2">20.5.1. URI</h3><p>The URI module is a successor to URI::URL and was written by GisleAas. While not clearly stated in the LWP documentation, you shoulduse the URI module whenever possible, since URI.pm has essentiallydeprecated URI::URL.</p><p>The URI module implements the URI class. Objects created from the URIclass represent Uniform Resource Identifiers (URIs). With the URImodule, you can identify the key parts of a URI: scheme,scheme-specific parts, and fragment identifiers, which may bereferred to respectfully as authority, path, and query components.For example, as shown in the URI module documentation:</p><blockquote><pre class="code"><scheme>:<scheme-specific-part>#<fragment><scheme>://<authority><path>?<query>#<fragment><path>?<query>#<fragment></pre></blockquote><p>You can break down <em class="emphasis">http://www.oreilly.com/somefile.html</em> as:</p><blockquote><pre class="code">scheme: httpauthority: www.oreilly.compath: /somefile.html</pre></blockquote><p>In the case of relative URIs, you can use the URI module to deal withonly the query component of a URI. With the URI module, you can parsethe above URI as follows:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse URI;my $url = 'http://www.oreilly.com/somefile.html';my $u1 = URI->new($url);print "scheme: ", $u1->scheme, "\n";print "authority: ", $u1->authority, "\n";print "path: ", $u1->path, "\n";</pre></blockquote><a name="perlnut2-CHP-20-SECT-5.1.1" /><div class="sect3"><h3 class="sect3">20.5.1.1. URI methods</h3><p>The following methods give you access to components of a URI. Thesemethods will return a string, unless the URI component is invalid, inwhich case <tt class="literal">undef</tt> is returned. Bear in mind that anempty string ("") is not equivalentto an undefined value.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>new($<em class="replaceable">uri</em>, [$<em class="replaceable">scheme</em>])</pre><p>Constructor. <tt class="literal">$</tt><em class="replaceable">uri</em> isgiven as an argument with the optional<tt class="literal">$</tt><em class="replaceable"><tt>scheme</tt></em>.<tt class="literal">new</tt> removes additional whitespace, double quotes,and arrows from the URL.<tt class="literal">$</tt><em class="replaceable"><tt>scheme</tt></em> is used onlywhen <tt class="literal">$</tt><em class="replaceable"><tt>str</tt></em> is a relativeURI; it is a simple string that denotes the scheme or an absolute URIobject. <tt class="literal">$</tt><em class="replaceable"><tt>str</tt></em> will betreated like a generic URI if<tt class="literal">$</tt><em class="replaceable"><tt>scheme</tt></em> isn't defined.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>URI::file->new($<em class="replaceable">file</em>, [$<em class="replaceable">os</em>])</pre><p>Constructs a new file URI from a filename.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new_abs</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>URI::file->new_abs($<em class="replaceable">file</em>, [$<em class="replaceable">os</em>])</pre><p>Constructs a new absolute file URI from a filename.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>abs</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>abs($<em class="replaceable">base_uri</em>)</pre><p>Returns an absolute URI reference. If<tt class="literal">$</tt><em class="replaceable"><tt>uri</tt></em> is alreadyabsolute, then a reference to<tt class="literal">$</tt><em class="replaceable"><tt>uri</tt></em> is returned.<tt class="literal">abs</tt> returns a new absolute URI that contains<tt class="literal">$</tt><em class="replaceable"><tt>uri</tt></em> and<tt class="literal">$</tt><em class="replaceable"><tt>base_uri</tt></em> if<tt class="literal">$</tt><em class="replaceable"><tt>uri</tt></em> is relative.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>as_string</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>as_string</pre><p>Returns a URI object as a plain string.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>authority</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>authority([$<em class="replaceable">auth</em>])</pre><p>Sets and gets the authority component of the<tt class="literal">$</tt><em class="replaceable"><tt>uri</tt></em>. This componentwill be escaped.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>canonical</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>canonical</pre><p>Returns a normalized version of the URI. This includes lowercasingthe scheme and hostname components, as well as removing an explicitport specification (if it mtaches the default port).<tt class="literal">canonical</tt> will return the original<tt class="literal">$</tt><em class="replaceable"><tt>uri</tt></em> if<tt class="literal">$</tt><em class="replaceable"><tt>uri</tt></em> was already in thecorrect form.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>clone</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>clone</pre><p>Returns a copy of the URI.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>cwd</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>URI::file->cwd</pre><p>Returns the current working directory as a file URI.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>default_port</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>default_port()</pre><p>Returns the default port of the URI scheme that<tt class="literal">$</tt><em class="replaceable"><tt>uri </tt></em>belongs to. Youcannot change the default port for a scheme.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>eq</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>eq()</pre><p>Compares two URIs.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>fragment</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>fragment([$<em class="replaceable">new_frag</em>])</pre><p>Returns the fragment identifier of a URI reference as an escapedstring.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>host</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>host([$<em class="replaceable">new_host</em>])</pre><p>Sets and gets the unescaped hostname. To specify a different port:</p><blockquote><pre class="code">$<em class="replaceable">new_host</em> = "hostname:port_number"</pre></blockquote></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>host_port</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>host_port([$<em class="replaceable">new_host_port</em>])</pre><p>Sets and gets the host and port as a single unit. Hostname and portare colon-separated.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new_abs</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -