uri.html

来自「perl教程」· HTML 代码 · 共 912 行 · 第 1/3 页

HTML
912
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../displayToc.js"></script>
<script language="JavaScript" src="../tocParas.js"></script>
<script language="JavaScript" src="../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../scineplex.css">
<title>URI - Uniform Resource Identifiers</title>
<link rel="stylesheet" href="../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',1);</script>
<h1><a>URI - Uniform Resource Identifiers</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#constructors">CONSTRUCTORS</a></li>
	<li><a href="#common_methods">COMMON METHODS</a></li>
	<li><a href="#generic_methods">GENERIC METHODS</a></li>
	<li><a href="#server_methods">SERVER METHODS</a></li>
	<li><a href="#schemespecific_support">SCHEME-SPECIFIC SUPPORT</a></li>
	<li><a href="#configuration_variables">CONFIGURATION VARIABLES</a></li>
	<li><a href="#bugs">BUGS</a></li>
	<li><a href="#parsing_uris_with_regexp">PARSING URIs WITH REGEXP</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#copyright">COPYRIGHT</a></li>
	<li><a href="#authors___acknowledgments">AUTHORS / ACKNOWLEDGMENTS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>URI - Uniform Resource Identifiers (absolute and relative)</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
 <span class="variable">$u1</span> <span class="operator">=</span> <span class="variable">URI</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">"http://www.perl.com"</span><span class="operator">);</span>
 <span class="variable">$u2</span> <span class="operator">=</span> <span class="variable">URI</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">"foo"</span><span class="operator">,</span> <span class="string">"http"</span><span class="operator">);</span>
 <span class="variable">$u3</span> <span class="operator">=</span> <span class="variable">$u2</span><span class="operator">-&gt;</span><span class="keyword">abs</span><span class="operator">(</span><span class="variable">$u1</span><span class="operator">);</span>
 <span class="variable">$u4</span> <span class="operator">=</span> <span class="variable">$u3</span><span class="operator">-&gt;</span><span class="variable">clone</span><span class="operator">;</span>
 <span class="variable">$u5</span> <span class="operator">=</span> <span class="variable">URI</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">"HTTP://WWW.perl.com:80"</span><span class="operator">)-&gt;</span><span class="variable">canonical</span><span class="operator">;</span>
</pre>
<pre>
 <span class="variable">$str</span> <span class="operator">=</span> <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">as_string</span><span class="operator">;</span>
 <span class="variable">$str</span> <span class="operator">=</span> <span class="string">"$u"</span><span class="operator">;</span>
</pre>
<pre>
 <span class="variable">$scheme</span> <span class="operator">=</span> <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">scheme</span><span class="operator">;</span>
 <span class="variable">$opaque</span> <span class="operator">=</span> <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">opaque</span><span class="operator">;</span>
 <span class="variable">$path</span>   <span class="operator">=</span> <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">path</span><span class="operator">;</span>
 <span class="variable">$frag</span>   <span class="operator">=</span> <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">fragment</span><span class="operator">;</span>
</pre>
<pre>
 <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">scheme</span><span class="operator">(</span><span class="string">"ftp"</span><span class="operator">);</span>
 <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">host</span><span class="operator">(</span><span class="string">"ftp.perl.com"</span><span class="operator">);</span>
 <span class="variable">$u</span><span class="operator">-&gt;</span><span class="variable">path</span><span class="operator">(</span><span class="string">"cpan/"</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This module implements the <code>URI</code> class.  Objects of this class
represent &quot;Uniform Resource Identifier references&quot; as specified in RFC
2396 (and updated by RFC 2732).</p>
<p>A Uniform Resource Identifier is a compact string of characters that
identifies an abstract or physical resource.  A Uniform Resource
Identifier can be further classified as either a Uniform Resource Locator
(URL) or a Uniform Resource Name (URN).  The distinction between URL
and URN does not matter to the <code>URI</code> class interface. A
&quot;URI-reference&quot; is a URI that may have additional information attached
in the form of a fragment identifier.</p>
<p>An absolute URI reference consists of three parts:  a <em>scheme</em>, a
<em>scheme-specific part</em> and a <em>fragment</em> identifier.  A subset of URI
references share a common syntax for hierarchical namespaces.  For
these, the scheme-specific part is further broken down into
<em>authority</em>, <em>path</em> and <em>query</em> components.  These URIs can also
take the form of relative URI references, where the scheme (and
usually also the authority) component is missing, but implied by the
context of the URI reference.  The three forms of URI reference
syntax are summarized as follows:</p>
<pre>
  &lt;scheme&gt;:&lt;scheme-specific-part&gt;#&lt;fragment&gt;
  &lt;scheme&gt;://&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
  &lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;</pre>
<p>The components into which a URI reference can be divided depend on the
<em>scheme</em>.  The <code>URI</code> class provides methods to get and set the
individual components.  The methods available for a specific
<code>URI</code> object depend on the scheme.</p>
<p>
</p>
<hr />
<h1><a name="constructors">CONSTRUCTORS</a></h1>
<p>The following methods construct new <code>URI</code> objects:</p>
<dl>
<dt><strong><a name="item_new">$uri = URI-&gt;new( $str )</a></strong>

<dt><strong>$uri = URI-&gt;new( $str, $scheme )</strong>

<dd>
<p>Constructs a new URI object.  The string
representation of a URI is given as argument, together with an optional
scheme specification.  Common URI wrappers like &quot;&quot; and &lt;&gt;, as well as
leading and trailing white space, are automatically removed from
the $str argument before it is processed further.</p>
</dd>
<dd>
<p>The constructor determines the scheme, maps this to an appropriate
URI subclass, constructs a new object of that class and returns it.</p>
</dd>
<dd>
<p>The $scheme argument is only used when $str is a
relative URI.  It can be either a simple string that
denotes the scheme, a string containing an absolute URI reference, or
an absolute <code>URI</code> object.  If no $scheme is specified for a relative
URI $str, then $str is simply treated as a generic URI (no scheme-specific
methods available).</p>
</dd>
<dd>
<p>The set of characters available for building URI references is
restricted (see <a href="../lib/URI/Escape.html">the URI::Escape manpage</a>).  Characters outside this set are
automatically escaped by the URI constructor.</p>
</dd>
</li>
<dt><strong><a name="item_new_abs">$uri = URI-&gt;new_abs( $str, $base_uri )</a></strong>

<dd>
<p>Constructs a new absolute URI object.  The $str argument can
denote a relative or absolute URI.  If relative, then it is
absolutized using $base_uri as base. The $base_uri must be an absolute
URI.</p>
</dd>
</li>
<dt><strong>$uri = URI::file-&gt;new( $filename )</strong>

<dt><strong>$uri = URI::file-&gt;new( $filename, $os )</strong>

<dd>
<p>Constructs a new <em>file</em> URI from a file name.  See <a href="../lib/URI/file.html">the URI::file manpage</a>.</p>
</dd>
</li>
<dt><strong>$uri = URI::file-&gt;new_abs( $filename )</strong>

<dt><strong>$uri = URI::file-&gt;new_abs( $filename, $os )</strong>

<dd>
<p>Constructs a new absolute <em>file</em> URI from a file name.  See
<a href="../lib/URI/file.html">the URI::file manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_cwd">$uri = URI::file-&gt;cwd</a></strong>

<dd>
<p>Returns the current working directory as a <em>file</em> URI.  See
<a href="../lib/URI/file.html">the URI::file manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_clone">$uri-&gt;clone</a></strong>

<dd>
<p>Returns a copy of the $uri.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="common_methods">COMMON METHODS</a></h1>
<p>The methods described in this section are available for all <code>URI</code>
objects.</p>
<p>Methods that give access to components of a URI always return the
old value of the component.  The value returned is <a href="../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> if the
component was not present.  There is generally a difference between a
component that is empty (represented as <code>&quot;&quot;</code>) and a component that is
missing (represented as <a href="../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>).  If an accessor method is given an
argument, it updates the corresponding component in addition to
returning the old value of the component.  Passing an undefined
argument removes the component (if possible).  The description of
each accessor method indicates whether the component is passed as
an escaped or an unescaped string.  A component that can be further
divided into sub-parts are usually passed escaped, as unescaping might
change its semantics.</p>
<p>The common methods available for all URI are:</p>
<dl>
<dt><strong><a name="item_scheme">$uri-&gt;scheme</a></strong>

<dt><strong>$uri-&gt;scheme( $new_scheme )</strong>

<dd>
<p>Sets and returns the scheme part of the $uri.  If the $uri is
relative, then $uri-&gt;scheme returns <a href="../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.  If called with an
argument, it updates the scheme of $uri, possibly changing the
class of $uri, and returns the old scheme value.  The method croaks
if the new scheme name is illegal; a scheme name must begin with a
letter and must consist of only US-ASCII letters, numbers, and a few
special marks: &quot;.&quot;, &quot;+&quot;, &quot;-&quot;.  This restriction effectively means
that the scheme must be passed unescaped.  Passing an undefined
argument to the scheme method makes the URI relative (if possible).</p>
</dd>
<dd>
<p>Letter case does not matter for scheme names.  The string
returned by $uri-&gt;scheme is always lowercase.  If you want the scheme
just as it was written in the URI in its original case,
you can use the $uri-&gt;_scheme method instead.</p>
</dd>
</li>
<dt><strong><a name="item_opaque">$uri-&gt;opaque</a></strong>

<dt><strong>$uri-&gt;opaque( $new_opaque )</strong>

<dd>
<p>Sets and returns the scheme-specific part of the $uri
(everything between the scheme and the fragment)
as an escaped string.</p>
</dd>
</li>
<dt><strong><a name="item_path">$uri-&gt;path</a></strong>

<dt><strong>$uri-&gt;path( $new_path )</strong>

<dd>
<p>Sets and returns the same value as $uri-&gt;opaque unless the URI
supports the generic syntax for hierarchical namespaces.
In that case the generic method is overridden to set and return
the part of the URI between the <em>host name</em> and the <em>fragment</em>.</p>
</dd>
</li>
<dt><strong><a name="item_fragment">$uri-&gt;fragment</a></strong>

<dt><strong>$uri-&gt;fragment( $new_frag )</strong>

<dd>
<p>Returns the fragment identifier of a URI reference
as an escaped string.</p>
</dd>
</li>
<dt><strong><a name="item_as_string">$uri-&gt;as_string</a></strong>

<dd>
<p>Returns a URI object to a plain string.  URI objects are
also converted to plain strings automatically by overloading.  This
means that $uri objects can be used as plain strings in most Perl
constructs.</p>
</dd>
</li>
<dt><strong><a name="item_canonical">$uri-&gt;canonical</a></strong>

<dd>
<p>Returns a normalized version of the URI.  The rules
for normalization are scheme-dependent.  They usually involve
lowercasing the scheme and Internet host name components,
removing the explicit port specification if it matches the default port,
uppercasing all escape sequences, and unescaping octets that can be
better represented as plain characters.</p>
</dd>
<dd>
<p>For efficiency reasons, if the $uri is already in normalized form,
then a reference to it is returned instead of a copy.</p>
</dd>
</li>
<dt><strong><a name="item_eq">$uri-&gt;eq( $other_uri )</a></strong>

<dt><strong>URI::eq( $first_uri, $other_uri )</strong>

<dd>
<p>Tests whether two URI references are equal.  URI references
that normalize to the same string are considered equal.  The method
can also be used as a plain function which can also test two string
arguments.</p>
</dd>
<dd>
<p>If you need to test whether two <code>URI</code> object references denote the
same object, use the '==' operator.</p>
</dd>
</li>
<dt><strong><a name="item_abs">$uri-&gt;abs( $base_uri )</a></strong>

<dd>
<p>Returns an absolute URI reference.  If $uri is already
absolute, then a reference to it is simply returned.  If the $uri
is relative, then a new absolute URI is constructed by combining the
$uri and the $base_uri, and returned.</p>
</dd>
</li>
<dt><strong><a name="item_rel">$uri-&gt;rel( $base_uri )</a></strong>

<dd>
<p>Returns a relative URI reference if it is possible to
make one that denotes the same resource relative to $base_uri.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?