peek.html

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

HTML
533 行
字号
          RV = 0xbab08
        SV = IV(0xbe860)
          REFCNT = 2
          FLAGS = (IOK,pIOK)
          IV = 42</pre>
<p>Starting from the top, this says <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a> is an SV.  The scalar is an RV, a
reference.  It has the <code>ROK</code> flag set, meaning it is a reference.  Because
ROK is set we have an RV item rather than an IV or PV.  Notice that Dump
follows the reference and shows us what <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a> was referencing.  We see the
same <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> that we found in the previous example.</p>
<p>Note that the value of <code>RV</code> coincides with the numbers we see when we
stringify $b. The addresses inside <code>RV()</code> and <code>IV()</code> are addresses of
<code>X***</code> structure which holds the current state of an <code>SV</code>. This
address may change during lifetime of an SV.</p>
<p>
</p>
<h2><a name="a_reference_to_an_array">A reference to an array</a></h2>
<p>This shows what a reference to an array looks like.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="operator">[</span><span class="number">42</span><span class="operator">]</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">;</span>
</pre>
<p>The output:</p>
<pre>
        SV = RV(0xf041c)
          REFCNT = 1
          FLAGS = (ROK)
          RV = 0xb2850
        SV = PVAV(0xbd448)
          REFCNT = 1
          FLAGS = ()
          IV = 0
          NV = 0
          ARRAY = 0xb2048
          ALLOC = 0xb2048
          FILL = 0
          MAX = 0
          ARYLEN = 0x0
          FLAGS = (REAL)
        Elt No. 0 0xb5658
        SV = IV(0xbe860)
          REFCNT = 1
          FLAGS = (IOK,pIOK)
          IV = 42</pre>
<p>This says <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> is an SV and that it is an RV.  That RV points to
another SV which is a PVAV, an array.  The array has one element,
element zero, which is another SV. The field <code>FILL</code> above indicates
the last element in the array, similar to <code>$#$a</code>.</p>
<p>If <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> pointed to an array of two elements then we would see the
following.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span> <span class="string">'Dump'</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="operator">[</span><span class="number">42</span><span class="operator">,</span><span class="number">24</span><span class="operator">]</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">;</span>
</pre>
<p>The output:</p>
<pre>
        SV = RV(0xf041c)
          REFCNT = 1
          FLAGS = (ROK)
          RV = 0xb2850
        SV = PVAV(0xbd448)
          REFCNT = 1
          FLAGS = ()
          IV = 0
          NV = 0
          ARRAY = 0xb2048
          ALLOC = 0xb2048
          FILL = 0
          MAX = 0
          ARYLEN = 0x0
          FLAGS = (REAL)
        Elt No. 0  0xb5658
        SV = IV(0xbe860)
          REFCNT = 1
          FLAGS = (IOK,pIOK)
          IV = 42
        Elt No. 1  0xb5680
        SV = IV(0xbe818)
          REFCNT = 1
          FLAGS = (IOK,pIOK)
          IV = 24</pre>
<p>Note that <code>Dump</code> will not report <em>all</em> the elements in the array,
only several first (depending on how deep it already went into the
report tree).</p>
<p>
</p>
<h2><a name="a_reference_to_a_hash">A reference to a hash</a></h2>
<p>The following shows the raw form of a reference to a hash.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="operator">{</span><span class="string">hello</span><span class="operator">=&gt;</span><span class="number">42</span><span class="operator">}</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">;</span>
</pre>
<p>The output:</p>
<pre>
        SV = RV(0x8177858) at 0x816a618
          REFCNT = 1
          FLAGS = (ROK)
          RV = 0x814fc10
          SV = PVHV(0x8167768) at 0x814fc10
            REFCNT = 1
            FLAGS = (SHAREKEYS)
            IV = 1
            NV = 0
            ARRAY = 0x816c5b8  (0:7, 1:1)
            hash quality = 100.0%
            KEYS = 1
            FILL = 1
            MAX = 7
            RITER = -1
            EITER = 0x0
            Elt &quot;hello&quot; HASH = 0xc8fd181b
            SV = IV(0x816c030) at 0x814fcf4
              REFCNT = 1
              FLAGS = (IOK,pIOK)
              IV = 42</pre>
<p>This shows <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> is a reference pointing to an SV.  That SV is a PVHV, a
hash. Fields RITER and EITER are used by <a href="../../lib/Pod/perlfunc.html#item_each"><code>each</code></a>.</p>
<p>The &quot;quality&quot; of a hash is defined as the total number of comparisons needed
to access every element once, relative to the expected number needed for a
random hash. The value can go over 100%.</p>
<p>The total number of comparisons is equal to the sum of the squares of the
number of entries in each bucket.  For a random hash of <code>&lt;n</code>&gt; keys into
<code>&lt;k</code>&gt; buckets, the expected value is:</p>
<pre>
                n + n(n-1)/2k</pre>
<p>
</p>
<h2><a name="dumping_a_large_array_or_hash">Dumping a large array or hash</a></h2>
<p>The <code>Dump()</code> function, by default, dumps up to 4 elements from a
toplevel array or hash.  This number can be increased by supplying a
second argument to the function.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="operator">[</span><span class="number">10</span><span class="operator">,</span><span class="number">11</span><span class="operator">,</span><span class="number">12</span><span class="operator">,</span><span class="number">13</span><span class="operator">,</span><span class="number">14</span><span class="operator">]</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">;</span>
</pre>
<p>Notice that <code>Dump()</code> prints only elements 10 through 13 in the above code.
The following code will print all of the elements.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span> <span class="string">'Dump'</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="operator">[</span><span class="number">10</span><span class="operator">,</span><span class="number">11</span><span class="operator">,</span><span class="number">12</span><span class="operator">,</span><span class="number">13</span><span class="operator">,</span><span class="number">14</span><span class="operator">]</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">,</span> <span class="number">5</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="a_reference_to_an_sv_which_holds_a_c_pointer">A reference to an SV which holds a C pointer</a></h2>
<p>This is what you really need to know as an XS programmer, of course.  When
an XSUB returns a pointer to a C structure that pointer is stored in an SV
and a reference to that SV is placed on the XSUB stack.  So the output from
an XSUB which uses something like the T_PTROBJ map might look something like
this:</p>
<pre>
        SV = RV(0xf381c)
          REFCNT = 1
          FLAGS = (ROK)
          RV = 0xb8ad8
        SV = PVMG(0xbb3c8)
          REFCNT = 1
          FLAGS = (OBJECT,IOK,pIOK)
          IV = 729160
          NV = 0
          PV = 0
          STASH = 0xc1d10       &quot;CookBookB::Opaque&quot;</pre>
<p>This shows that we have an SV which is an RV.  That RV points at another
SV.  In this case that second SV is a PVMG, a blessed scalar.  Because it is
blessed it has the <code>OBJECT</code> flag set.  Note that an SV which holds a C
pointer also has the <code>IOK</code> flag set.  The <code>STASH</code> is set to the package
name which this SV was blessed into.</p>
<p>The output from an XSUB which uses something like the T_PTRREF map, which
doesn't bless the object, might look something like this:</p>
<pre>
        SV = RV(0xf381c)
          REFCNT = 1
          FLAGS = (ROK)
          RV = 0xb8ad8
        SV = PVMG(0xbb3c8)
          REFCNT = 1
          FLAGS = (IOK,pIOK)
          IV = 729160
          NV = 0
          PV = 0</pre>
<p>
</p>
<h2><a name="a_reference_to_a_subroutine">A reference to a subroutine</a></h2>
<p>Looks like this:</p>
<pre>
        SV = RV(0x798ec)
          REFCNT = 1
          FLAGS = (TEMP,ROK)
          RV = 0x1d453c
        SV = PVCV(0x1c768c)
          REFCNT = 2
          FLAGS = ()
          IV = 0
          NV = 0
          COMP_STASH = 0x31068  &quot;main&quot;
          START = 0xb20e0
          ROOT = 0xbece0
          XSUB = 0x0
          XSUBANY = 0
          GVGV::GV = 0x1d44e8   &quot;MY&quot; :: &quot;top_targets&quot;
          FILE = &quot;(eval 5)&quot;
          DEPTH = 0
          PADLIST = 0x1c9338</pre>
<p>This shows that</p>
<ul>
<li>
<p>the subroutine is not an XSUB (since <code>START</code> and <code>ROOT</code> are
non-zero, and <code>XSUB</code> is zero);</p>
</li>
<li>
<p>that it was compiled in the package <code>main</code>;</p>
</li>
<li>
<p>under the name <code>MY::top_targets</code>;</p>
</li>
<li>
<p>inside a 5th eval in the program;</p>
</li>
<li>
<p>it is not currently executed (see <code>DEPTH</code>);</p>
</li>
<li>
<p>it has no prototype (<code>PROTOTYPE</code> field is missing).</p>
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="exports">EXPORTS</a></h1>
<p><code>Dump</code>, <code>mstat</code>, <code>DeadCode</code>, <code>DumpArray</code>, <code>DumpWithOP</code> and
<code>DumpProg</code>, <code>fill_mstats</code>, <code>mstats_fillhash</code>, <code>mstats2hash</code> by
default. Additionally available <code>SvREFCNT</code>, <code>SvREFCNT_inc</code> and
<code>SvREFCNT_dec</code>.</p>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>Readers have been known to skip important parts of <a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>, causing much
frustration for all.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Ilya Zakharevich	<a href="mailto:ilya@math.ohio-state.edu">ilya@math.ohio-state.edu</a></p>
<p>Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.</p>
<p>Author of this software makes no claim whatsoever about suitability,
reliability, edability, editability or usability of this product, and
should not be kept liable for any damage resulting from the use of
it. If you can use it, you are in luck, if not, I should not be kept
responsible. Keep a handy copy of your backup tape at hand.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>, and <a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>, again.</p>

</body>

</html>

⌨️ 快捷键说明

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