perlguts.html

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

HTML
790 行
字号
<p>The <code>klen</code> parameter is the length of the key being passed in (Note that
you cannot pass 0 in as a value of <code>klen</code> to tell Perl to measure the
length of the key).  The <code>val</code> argument contains the SV pointer to the
scalar being stored, and <code>hash</code> is the precomputed hash value (zero if
you want <code>hv_store</code> to calculate it for you).  The <code>lval</code> parameter
indicates whether this fetch is actually a part of a store operation, in
which case a new undefined value will be added to the HV with the supplied
key and <code>hv_fetch</code> will return as if the value had already existed.</p>
<p>Remember that <code>hv_store</code> and <code>hv_fetch</code> return <code>SV**</code>'s and not just
<code>SV*</code>.  To access the scalar value, you must first dereference the return
value.  However, you should check to make sure that the return value is
not NULL before dereferencing it.</p>
<p>These two functions check if a hash table entry exists, and deletes it.</p>
<pre>
    <span class="variable">bool</span>  <span class="variable">hv_exists</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*,</span> <span class="variable">const</span> <span class="variable">char</span><span class="operator">*</span> <span class="variable">key</span><span class="operator">,</span> <span class="variable">U32</span> <span class="variable">klen</span><span class="operator">);</span>
    <span class="variable">SV</span><span class="operator">*</span>   <span class="variable">hv_delete</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*,</span> <span class="variable">const</span> <span class="variable">char</span><span class="operator">*</span> <span class="variable">key</span><span class="operator">,</span> <span class="variable">U32</span> <span class="variable">klen</span><span class="operator">,</span> <span class="variable">I32</span> <span class="variable">flags</span><span class="operator">);</span>
</pre>
<p>If <a href="../../lib/Pod/perlfunc.html#item_flags"><code>flags</code></a> does not include the <code>G_DISCARD</code> flag then <code>hv_delete</code> will
create and return a mortal copy of the deleted value.</p>
<p>And more miscellaneous functions:</p>
<pre>
    <span class="variable">void</span>   <span class="variable">hv_clear</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*);</span>
    <span class="variable">void</span>   <span class="variable">hv_undef</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*);</span>
</pre>
<p>Like their AV counterparts, <code>hv_clear</code> deletes all the entries in the hash
table but does not actually delete the hash table.  The <code>hv_undef</code> deletes
both the entries and the hash table itself.</p>
<p>Perl keeps the actual data in linked list of structures with a typedef of HE.
These contain the actual key and value pointers (plus extra administrative
overhead).  The key is a string pointer; the value is an <code>SV*</code>.  However,
once you have an <code>HE*</code>, to get the actual key and value, use the routines
specified below.</p>
<pre>
    <span class="variable">I32</span>    <span class="variable">hv_iterinit</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*);</span>
            <span class="regex">/* Prepares starting point to traverse hash table */</span>
    <span class="variable">HE</span><span class="operator">*</span>    <span class="variable">hv_iternext</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*);</span>
            <span class="regex">/* Get the next entry, and return a pointer to a
               structure that has both the key and value */</span>
    <span class="variable">char</span><span class="operator">*</span>  <span class="variable">hv_iterkey</span><span class="operator">(</span><span class="variable">HE</span><span class="operator">*</span> <span class="variable">entry</span><span class="operator">,</span> <span class="variable">I32</span><span class="operator">*</span> <span class="variable">retlen</span><span class="operator">);</span>
            <span class="regex">/* Get the key from an HE structure and also return
               the length of the key string */</span>
    <span class="variable">SV</span><span class="operator">*</span>    <span class="variable">hv_iterval</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*,</span> <span class="variable">HE</span><span class="operator">*</span> <span class="variable">entry</span><span class="operator">);</span>
            <span class="regex">/* Return an SV pointer to the value of the HE
               structure */</span>
    <span class="variable">SV</span><span class="operator">*</span>    <span class="variable">hv_iternextsv</span><span class="operator">(</span><span class="variable">HV</span><span class="operator">*,</span> <span class="variable">char</span><span class="operator">**</span> <span class="variable">key</span><span class="operator">,</span> <span class="variable">I32</span><span class="operator">*</span> <span class="variable">retlen</span><span class="operator">);</span>
            <span class="regex">/* This convenience routine combines hv_iternext,
               hv_iterkey, and hv_iterval.  The key and retlen
               arguments are return values for the key and its
               length.  The value is returned in the SV* argument */</span>
</pre>
<p>If you know the name of a hash variable, you can get a pointer to its HV
by using the following:</p>
<pre>
    <span class="variable">HV</span><span class="operator">*</span>  <span class="variable">get_hv</span><span class="operator">(</span><span class="string">"package::varname"</span><span class="operator">,</span> <span class="variable">FALSE</span><span class="operator">);</span>
</pre>
<p>This returns NULL if the variable does not exist.</p>
<p>The hash algorithm is defined in the <code>PERL_HASH(hash, key, klen)</code> macro:</p>
<pre>
    <span class="variable">hash</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
    <span class="keyword">while</span> <span class="operator">(</span><span class="variable">klen</span><span class="operator">--)</span>
        <span class="variable">hash</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">hash</span> <span class="operator">*</span> <span class="number">33</span><span class="operator">)</span> <span class="operator">+</span> <span class="variable">*key</span><span class="operator">++;</span>
    <span class="variable">hash</span> <span class="operator">=</span> <span class="variable">hash</span> <span class="operator">+</span> <span class="operator">(</span><span class="variable">hash</span> <span class="operator">&gt;&gt;</span> <span class="number">5</span><span class="operator">);</span>                  <span class="regex">/* after 5.6 */</span>
</pre>
<p>The last step was added in version 5.6 to improve distribution of
lower bits in the resulting hash value.</p>
<p>See <a href="#understanding_the_magic_of_tied_hashes_and_arrays">Understanding the Magic of Tied Hashes and Arrays</a> for more
information on how to use the hash access functions on tied hashes.</p>
<p>
</p>
<h2><a name="hash_api_extensions">Hash API Extensions</a></h2>
<p>Beginning with version 5.004, the following functions are also supported:</p>
<pre>
    <span class="variable">HE</span><span class="operator">*</span>     <span class="variable">hv_fetch_ent</span>  <span class="operator">(</span><span class="variable">HV</span><span class="operator">*</span> <span class="variable">tb</span><span class="operator">,</span> <span class="variable">SV</span><span class="operator">*</span> <span class="variable">key</span><span class="operator">,</span> <span class="variable">I32</span> <span class="variable">lval</span><span class="operator">,</span> <span class="variable">U32</span> <span class="variable">hash</span><span class="operator">);</span>
    <span class="variable">HE</span><span class="operator">*</span>     <span class="variable">hv_store_ent</span>  <span class="operator">(</span><span class="variable">HV</span><span class="operator">*</span> <span class="variable">tb</span><span class="operator">,</span> <span class="variable">SV</span><span class="operator">*</span> <span class="variable">key</span><span class="operator">,</span> <span class="variable">SV</span><span class="operator">*</span> <span class="variable">val</span><span class="operator">,</span> <span class="variable">U32</span> <span class="variable">hash</span><span class="operator">);</span>
</pre>
<pre>
    <span class="variable">bool</span>    <span class="variable">hv_exists_ent</span> <span class="operator">(</span><span class="variable">HV</span><span class="operator">*</span> <span class="variable">tb</span><span class="operator">,</span> <span class="variable">SV</span><span class="operator">*</span> <span class="variable">key</span><span class="operator">,</span> <span class="variable">U32</span> <span class="variable">hash</span><span class="operator">);</span>
    <span class="variable">SV</span><span class="operator">*</span>     <span class="variable">hv_delete_ent</span> <span class="operator">(</span><span class="variable">HV</span><span class="operator">*</span> <span class="variable">tb</span><span class="operator">,</span> <span class="variable">SV</span><span class="operator">*</span> <span class="variable">key</span><span class="operator">,</span> <span class="variable">I32</span> <span class="variable">flags</span><span class="operator">,</span> <span class="variable">U32</span> <span class="variable">hash</span><span class="operator">);</span>
</pre>
<pre>
    <span class="variable">SV</span><span class="operator">*</span>     <span class="variable">hv_iterkeysv</span>  <span class="operator">(</span><span class="variable">HE</span><span class="operator">*</span> <span class="variable">entry</span><span class="operator">);</span>
</pre>
<p>Note that these functions take <code>SV*</code> keys, which simplifies writing
of extension code that deals with hash structures.  These functions
also allow passing of <code>SV*</code> keys to <a href="../../lib/Pod/perlfunc.html#item_tie"><code>tie</code></a> functions without forcing
you to stringify the keys (unlike the previous set of functions).</p>
<p>They also return and accept whole hash entries (<code>HE*</code>), making their
use more efficient (since the hash number for a particular string
doesn't have to be recomputed every time).  See <a href="../../lib/Pod/perlapi.html">the perlapi manpage</a> for detailed
descriptions.</p>
<p>The following macros must always be used to access the contents of hash
entries.  Note that the arguments to these macros must be simple
variables, since they may get evaluated more than once.  See
<a href="../../lib/Pod/perlapi.html">the perlapi manpage</a> for detailed descriptions of these macros.</p>
<pre>
    HePV(HE* he, STRLEN len)
    HeVAL(HE* he)
    HeHASH(HE* he)
    HeSVKEY(HE* he)
    HeSVKEY_force(HE* he)
    HeSVKEY_set(HE* he, SV* sv)</pre>
<p>These two lower level macros are defined, but must only be used when
dealing with keys that are not <code>SV*</code>s:</p>
<pre>
    HeKEY(HE* he)
    HeKLEN(HE* he)</pre>
<p>Note that both <code>hv_store</code> and <code>hv_store_ent</code> do not increment the
reference count of the stored <code>val</code>, which is the caller's responsibility.
If these functions return a NULL value, the caller will usually have to
decrement the reference count of <code>val</code> to avoid a memory leak.</p>
<p>
</p>
<h2><a name="avs__hvs_and_undefined_values">AVs, HVs and undefined values</a></h2>
<p>Sometimes you have to store undefined values in AVs or HVs. Although
this may be a rare case, it can be tricky. That's because you're
used to using <code>&amp;PL_sv_undef</code> if you need an undefined SV.</p>
<p>For example, intuition tells you that this XS code:</p>
<pre>
    <span class="variable">AV</span> <span class="variable">*av</span> <span class="operator">=</span> <span class="variable">newAV</span><span class="operator">();</span>
    <span class="variable">av_store</span><span class="operator">(</span> <span class="variable">av</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="variable">PL_sv_undef</span> <span class="operator">);</span>
</pre>
<p>is equivalent to this Perl code:</p>
<pre>
    <span class="keyword">my</span> <span class="variable">@av</span><span class="operator">;</span>
    <span class="variable">$av</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span> <span class="operator">=</span> <span class="keyword">undef</span><span class="operator">;</span>
</pre>
<p>Unfortunately, this isn't true. AVs use <code>&amp;PL_sv_undef</code> as a marker
for indicating that an array element has not yet been initialized.
Thus, <code>exists $av[0]</code> would be true for the above Perl code, but
false for the array generated by the XS code.</p>
<p>Other problems can occur when storing <code>&amp;PL_sv_undef</code> in HVs:</p>
<pre>
    <span class="variable">hv_store</span><span class="operator">(</span> <span class="variable">hv</span><span class="operator">,</span> <span class="string">"key"</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="variable">PL_sv_undef</span><span class="operator">,</span> <span class="number">0</span> <span class="operator">);</span>
</pre>
<p>This will indeed make the value <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>, but if you try to modify
the value of <code>key</code>, you'll get the following error:</p>
<pre>
    Modification of non-creatable hash value attempted</pre>
<p>In perl 5.8.0, <code>&amp;PL_sv_undef</code> was also used to mark placeholders
in restricted hashes. This caused such hash entries not to appear
when iterating over the hash or when checking for the keys
with the <code>hv_exists</code> function.</p>
<p>You can run into similar problems when you store <code>&amp;PL_sv_true</code> or
<code>&amp;PL_sv_false</code> into AVs or HVs. Trying to modify such elements
will give you the following error:</p>
<pre>
    Modification of a read-only value attempted</pre>
<p>To make a long story short, you can use the special variables
<code>&amp;PL_sv_undef</code>, <code>&amp;PL_sv_true</code> and <code>&amp;PL_sv_false</code> with AVs and
HVs, but you have to make sure you know what you're doing.</p>
<p>Generally, if you want to store an undefined value in an AV
or HV, you should not use <code>&amp;PL_sv_undef</code>, but rather create a
new undefined value using the <code>newSV</code> function, for example:</p>
<pre>
    <span class="variable">av_store</span><span class="operator">(</span> <span class="variable">av</span><span class="operator">,</span> <span class="number">42</span><span class="operator">,</span> <span class="variable">newSV</span><span class="operator">(</span><span class="number">0</span><span class="operator">)</span> <span class="operator">);</span>
    <span class="variable">hv_store</span><span class="operator">(</span> <span class="variable">hv</span><span class="operator">,</span> <span class="string">"foo"</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="variable">newSV</span><span class="operator">(</span><span class="number">0</span><span class="operator">),</span> <span class="number">0</span> <span class="operator">);</span>
</pre>
<p>

⌨️ 快捷键说明

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