universal.html

来自「perl教程」· HTML 代码 · 共 181 行

HTML
181
字号
<?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>UNIVERSAL - base class for ALL classes</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>UNIVERSAL - base class for ALL classes</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="#exports">EXPORTS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>UNIVERSAL - base class for ALL classes (blessed references)</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="variable">$is_io</span> <span class="operator">=</span> <span class="variable">$fd</span><span class="operator">-&gt;</span><span class="variable">isa</span><span class="operator">(</span><span class="string">"IO::Handle"</span><span class="operator">);</span>
    <span class="variable">$is_io</span> <span class="operator">=</span> <span class="variable">Class</span><span class="operator">-&gt;</span><span class="variable">isa</span><span class="operator">(</span><span class="string">"IO::Handle"</span><span class="operator">);</span>
</pre>
<pre>
    <span class="variable">$sub</span> <span class="operator">=</span> <span class="variable">$obj</span><span class="operator">-&gt;</span><span class="variable">can</span><span class="operator">(</span><span class="string">"print"</span><span class="operator">);</span>
    <span class="variable">$sub</span> <span class="operator">=</span> <span class="variable">Class</span><span class="operator">-&gt;</span><span class="variable">can</span><span class="operator">(</span><span class="string">"print"</span><span class="operator">);</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">UNIVERSAL</span> <span class="string">qw( isa can VERSION )</span><span class="operator">;</span>
    <span class="variable">$yes</span> <span class="operator">=</span> <span class="variable">isa</span> <span class="variable">$ref</span><span class="operator">,</span> <span class="string">"HASH"</span> <span class="operator">;</span>
    <span class="variable">$sub</span> <span class="operator">=</span> <span class="variable">can</span> <span class="variable">$ref</span><span class="operator">,</span> <span class="string">"fandango"</span> <span class="operator">;</span>
    <span class="variable">$ver</span> <span class="operator">=</span> <span class="variable">VERSION</span> <span class="variable">$obj</span> <span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><code>UNIVERSAL</code> is the base class which all bless references will inherit from,
see <a href="../lib/Pod/perlobj.html">the perlobj manpage</a>.</p>
<p><code>UNIVERSAL</code> provides the following methods and functions:</p>
<dl>
<dt><strong><a name="item_isa"><code>$obj-&gt;isa( TYPE )</code></a></strong>

<dt><strong><code>CLASS-&gt;isa( TYPE )</code></strong>

<dt><strong><code>isa( VAL, TYPE )</code></strong>

<dd>
<p>Where</p>
</dd>
<dl>
<dt><strong><a name="item_type"><code>TYPE</code></a></strong>

<dd>
<p>is a package name</p>
</dd>
</li>
<dt><strong><a name="item__obj"><code>$obj</code></a></strong>

<dd>
<p>is a blessed reference or a string containing a package name</p>
</dd>
</li>
<dt><strong><a name="item_class"><code>CLASS</code></a></strong>

<dd>
<p>is a package name</p>
</dd>
</li>
<dt><strong><a name="item_val"><code>VAL</code></a></strong>

<dd>
<p>is any of the above or an unblessed reference</p>
</dd>
</li>
</dl>
<p>When used as an instance or class method (<a href="#item_isa"><code>$obj-&gt;isa( TYPE )</code></a>),
<a href="#item_isa"><code>isa</code></a> returns <em>true</em> if $obj is blessed into package <a href="#item_type"><code>TYPE</code></a> or
inherits from package <a href="#item_type"><code>TYPE</code></a>.</p>
<p>When used as a class method (<a href="#item_isa"><code>CLASS-&gt;isa( TYPE )</code></a>: sometimes
referred to as a static method), <a href="#item_isa"><code>isa</code></a> returns <em>true</em> if <a href="#item_class"><code>CLASS</code></a>
inherits from (or is itself) the name of the package <a href="#item_type"><code>TYPE</code></a> or
inherits from package <a href="#item_type"><code>TYPE</code></a>.</p>
<p>When used as a function, like</p>
<pre>
   <span class="keyword">use</span> <span class="variable">UNIVERSAL</span> <span class="string">qw( isa )</span> <span class="operator">;</span>
   <span class="variable">$yes</span> <span class="operator">=</span> <span class="variable">isa</span> <span class="variable">$h</span><span class="operator">,</span> <span class="string">"HASH"</span><span class="operator">;</span>
   <span class="variable">$yes</span> <span class="operator">=</span> <span class="variable">isa</span> <span class="string">"Foo"</span><span class="operator">,</span> <span class="string">"Bar"</span><span class="operator">;</span>
</pre>
<p>or</p>
<pre>
   <span class="keyword">require</span> <span class="variable">UNIVERSAL</span> <span class="operator">;</span>
   <span class="variable">$yes</span> <span class="operator">=</span> <span class="variable">UNIVERSAL::isa</span> <span class="variable">$a</span><span class="operator">,</span> <span class="string">"ARRAY"</span><span class="operator">;</span>
</pre>
<p><a href="#item_isa"><code>isa</code></a> returns <em>true</em> in the same cases as above and also if <a href="#item_val"><code>VAL</code></a> is an
unblessed reference to a perl variable of type <a href="#item_type"><code>TYPE</code></a>, such as &quot;HASH&quot;,
&quot;ARRAY&quot;, or &quot;Regexp&quot;.</p>
<dt><strong><a name="item_can"><code>$obj-&gt;can( METHOD )</code></a></strong>

<dt><strong><code>CLASS-&gt;can( METHOD )</code></strong>

<dt><strong><code>can( VAL, METHOD )</code></strong>

<dd>
<p><a href="#item_can"><code>can</code></a> checks if the object or class has a method called <code>METHOD</code>. If it does
then a reference to the sub is returned. If it does not then <em>undef</em> is
returned.  This includes methods inherited or imported by <a href="#item__obj"><code>$obj</code></a>, <a href="#item_class"><code>CLASS</code></a>, or
<a href="#item_val"><code>VAL</code></a>.</p>
</dd>
<dd>
<p><a href="#item_can"><code>can</code></a> cannot know whether an object will be able to provide a method
through AUTOLOAD, so a return value of <em>undef</em> does not necessarily mean
the object will not be able to handle the method call. To get around
this some module authors use a forward declaration (see <a href="../lib/Pod/perlsub.html">the perlsub manpage</a>)
for methods they will handle via AUTOLOAD. For such 'dummy' subs, <a href="#item_can"><code>can</code></a>
will still return a code reference, which, when called, will fall through
to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef
will cause an error.</p>
</dd>
<dd>
<p><a href="#item_can"><code>can</code></a> can be called as a class (static) method, an object method, or a
function.</p>
</dd>
<dd>
<p>When used as a function, if <a href="#item_val"><code>VAL</code></a> is a blessed reference or package name which
has a method called <code>METHOD</code>, <a href="#item_can"><code>can</code></a> returns a reference to the subroutine.
If <a href="#item_val"><code>VAL</code></a> is not a blessed reference, or if it does not have a method
<code>METHOD</code>, <em>undef</em> is returned.</p>
</dd>
</li>
<dt><strong><a name="item_version"><code>VERSION ( [ REQUIRE ] )</code></a></strong>

<dd>
<p><a href="#item_version"><code>VERSION</code></a> will return the value of the variable <code>$VERSION</code> in the
package the object is blessed into. If <code>REQUIRE</code> is given then
it will do a comparison and die if the package version is not
greater than or equal to <code>REQUIRE</code>.</p>
</dd>
<dd>
<p><a href="#item_version"><code>VERSION</code></a> can be called as either a class (static) method, an object
method or a function.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="exports">EXPORTS</a></h1>
<p>None by default.</p>
<p>You may request the import of all three functions (<a href="#item_isa"><code>isa</code></a>, <a href="#item_can"><code>can</code></a>, and
<a href="#item_version"><code>VERSION</code></a>), however it isn't usually necessary to do so.  Perl magically
makes these functions act as methods on all objects.  The one exception is
<a href="#item_isa"><code>isa</code></a>, which is useful as a function when operating on non-blessed
references.</p>

</body>

</html>

⌨️ 快捷键说明

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