perlguts.html
来自「perl教程」· HTML 代码 · 共 790 行 · 第 1/5 页
HTML
790 行
<span class="variable">foo</span><span class="operator">(</span><span class="variable">$x</span><span class="operator">);</span>
</pre>
<p>So to repeat always use <code>SvOK()</code> to check whether an sv is defined.</p>
<p>Also you have to be careful when using <code>&PL_sv_undef</code> as a value in
AVs or HVs (see <a href="#avs__hvs_and_undefined_values">AVs, HVs and undefined values</a>).</p>
<p>There are also the two values <code>PL_sv_yes</code> and <code>PL_sv_no</code>, which contain
boolean TRUE and FALSE values, respectively. Like <code>PL_sv_undef</code>, their
addresses can be used whenever an <code>SV*</code> is needed.</p>
<p>Do not be fooled into thinking that <code>(SV *) 0</code> is the same as <code>&PL_sv_undef</code>.
Take this code:</p>
<pre>
<span class="variable">SV</span><span class="operator">*</span> <span class="variable">sv</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">SV</span><span class="operator">*)</span> <span class="number">0</span><span class="operator">;</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">I</span><span class="operator">-</span><span class="variable">am</span><span class="operator">-</span><span class="variable">to</span><span class="operator">-</span><span class="keyword">return</span><span class="operator">-</span><span class="variable">a</span><span class="operator">-</span><span class="variable">real</span><span class="operator">-</span><span class="variable">value</span><span class="operator">)</span> <span class="operator">{</span>
<span class="variable">sv</span> <span class="operator">=</span> <span class="variable">sv_2mortal</span><span class="operator">(</span><span class="variable">newSViv</span><span class="operator">(</span><span class="number">42</span><span class="operator">));</span>
<span class="operator">}</span>
<span class="variable">sv_setsv</span><span class="operator">(</span><span class="variable">ST</span><span class="operator">(</span><span class="number">0</span><span class="operator">),</span> <span class="variable">sv</span><span class="operator">);</span>
</pre>
<p>This code tries to return a new SV (which contains the value 42) if it should
return a real value, or undef otherwise. Instead it has returned a NULL
pointer which, somewhere down the line, will cause a segmentation violation,
bus error, or just weird results. Change the zero to <code>&PL_sv_undef</code> in the
first line and all will be well.</p>
<p>To free an SV that you've created, call <code>SvREFCNT_dec(SV*)</code>. Normally this
call is not necessary (see <a href="#reference_counts_and_mortality">Reference Counts and Mortality</a>).</p>
<p>
</p>
<h2><a name="offsets">Offsets</a></h2>
<p>Perl provides the function <code>sv_chop</code> to efficiently remove characters
from the beginning of a string; you give it an SV and a pointer to
somewhere inside the PV, and it discards everything before the
pointer. The efficiency comes by means of a little hack: instead of
actually removing the characters, <code>sv_chop</code> sets the flag <code>OOK</code>
(offset OK) to signal to other functions that the offset hack is in
effect, and it puts the number of bytes chopped off into the IV field
of the SV. It then moves the PV pointer (called <code>SvPVX</code>) forward that
many bytes, and adjusts <code>SvCUR</code> and <code>SvLEN</code>.</p>
<p>Hence, at this point, the start of the buffer that we allocated lives
at <code>SvPVX(sv) - SvIV(sv)</code> in memory and the PV pointer is pointing
into the middle of this allocated storage.</p>
<p>This is best demonstrated by example:</p>
<pre>
% ./perl -Ilib -MDevel::Peek -le '$a="12345"; $a=~s/.//; Dump($a)'
SV = PVIV(0x8128450) at 0x81340f0
REFCNT = 1
FLAGS = (POK,OOK,pPOK)
IV = 1 (OFFSET)
PV = 0x8135781 ( "1" . ) "2345"\0
CUR = 4
LEN = 5</pre>
<p>Here the number of bytes chopped off (1) is put into IV, and
<code>Devel::Peek::Dump</code> helpfully reminds us that this is an offset. The
portion of the string between the "real" and the "fake" beginnings is
shown in parentheses, and the values of <code>SvCUR</code> and <code>SvLEN</code> reflect
the fake beginning, not the real one.</p>
<p>Something similar to the offset hack is performed on AVs to enable
efficient shifting and splicing off the beginning of the array; while
<code>AvARRAY</code> points to the first element in the array that is visible from
Perl, <code>AvALLOC</code> points to the real start of the C array. These are
usually the same, but a <a href="../../lib/Pod/perlfunc.html#item_shift"><code>shift</code></a> operation can be carried out by
increasing <code>AvARRAY</code> by one and decreasing <code>AvFILL</code> and <code>AvLEN</code>.
Again, the location of the real start of the C array only comes into
play when freeing the array. See <code>av_shift</code> in <em>av.c</em>.</p>
<p>
</p>
<h2><a name="what_s_really_stored_in_an_sv">What's Really Stored in an SV?</a></h2>
<p>Recall that the usual method of determining the type of scalar you have is
to use <code>Sv*OK</code> macros. Because a scalar can be both a number and a string,
usually these macros will always return TRUE and calling the <code>Sv*V</code>
macros will do the appropriate conversion of string to integer/double or
integer/double to string.</p>
<p>If you <em>really</em> need to know if you have an integer, double, or string
pointer in an SV, you can use the following three macros instead:</p>
<pre>
SvIOKp(SV*)
SvNOKp(SV*)
SvPOKp(SV*)</pre>
<p>These will tell you if you truly have an integer, double, or string pointer
stored in your SV. The "p" stands for private.</p>
<p>The are various ways in which the private and public flags may differ.
For example, a tied SV may have a valid underlying value in the IV slot
(so SvIOKp is true), but the data should be accessed via the FETCH
routine rather than directly, so SvIOK is false. Another is when
numeric conversion has occurred and precision has been lost: only the
private flag is set on 'lossy' values. So when an NV is converted to an
IV with loss, SvIOKp, SvNOKp and SvNOK will be set, while SvIOK wont be.</p>
<p>In general, though, it's best to use the <code>Sv*V</code> macros.</p>
<p>
</p>
<h2><a name="working_with_avs">Working with AVs</a></h2>
<p>There are two ways to create and load an AV. The first method creates an
empty AV:</p>
<pre>
<span class="variable">AV</span><span class="operator">*</span> <span class="variable">newAV</span><span class="operator">();</span>
</pre>
<p>The second method both creates the AV and initially populates it with SVs:</p>
<pre>
<span class="variable">AV</span><span class="operator">*</span> <span class="variable">av_make</span><span class="operator">(</span><span class="variable">I32</span> <span class="variable">num</span><span class="operator">,</span> <span class="variable">SV</span> <span class="operator">**</span><span class="variable">ptr</span><span class="operator">);</span>
</pre>
<p>The second argument points to an array containing <code>num</code> <code>SV*</code>'s. Once the
AV has been created, the SVs can be destroyed, if so desired.</p>
<p>Once the AV has been created, the following operations are possible on AVs:</p>
<pre>
<span class="variable">void</span> <span class="variable">av_push</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*,</span> <span class="variable">SV</span><span class="operator">*);</span>
<span class="variable">SV</span><span class="operator">*</span> <span class="variable">av_pop</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*);</span>
<span class="variable">SV</span><span class="operator">*</span> <span class="variable">av_shift</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*);</span>
<span class="variable">void</span> <span class="variable">av_unshift</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*,</span> <span class="variable">I32</span> <span class="variable">num</span><span class="operator">);</span>
</pre>
<p>These should be familiar operations, with the exception of <code>av_unshift</code>.
This routine adds <code>num</code> elements at the front of the array with the <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>
value. You must then use <code>av_store</code> (described below) to assign values
to these new elements.</p>
<p>Here are some other functions:</p>
<pre>
<span class="variable">I32</span> <span class="variable">av_len</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*);</span>
<span class="variable">SV</span><span class="operator">**</span> <span class="variable">av_fetch</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*,</span> <span class="variable">I32</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">SV</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="variable">I32</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>
</pre>
<p>The <code>av_len</code> function returns the highest index value in array (just
like $#array in Perl). If the array is empty, -1 is returned. The
<code>av_fetch</code> function returns the value at index <code>key</code>, but if <code>lval</code>
is non-zero, then <code>av_fetch</code> will store an undef value at that index.
The <code>av_store</code> function stores the value <code>val</code> at index <code>key</code>, and does
not increment the reference count of <code>val</code>. Thus the caller is responsible
for taking care of that, and if <code>av_store</code> returns NULL, the caller will
have to decrement the reference count to avoid a memory leak. Note that
<code>av_fetch</code> and <code>av_store</code> both return <code>SV**</code>'s, not <code>SV*</code>'s as their
return value.</p>
<pre>
<span class="variable">void</span> <span class="variable">av_clear</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*);</span>
<span class="variable">void</span> <span class="variable">av_undef</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*);</span>
<span class="variable">void</span> <span class="variable">av_extend</span><span class="operator">(</span><span class="variable">AV</span><span class="operator">*,</span> <span class="variable">I32</span> <span class="variable">key</span><span class="operator">);</span>
</pre>
<p>The <code>av_clear</code> function deletes all the elements in the AV* array, but
does not actually delete the array itself. The <code>av_undef</code> function will
delete all the elements in the array plus the array itself. The
<code>av_extend</code> function extends the array so that it contains at least <code>key+1</code>
elements. If <code>key+1</code> is less than the currently allocated length of the array,
then nothing is done.</p>
<p>If you know the name of an array variable, you can get a pointer to its AV
by using the following:</p>
<pre>
<span class="variable">AV</span><span class="operator">*</span> <span class="variable">get_av</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>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 array access functions on tied arrays.</p>
<p>
</p>
<h2><a name="working_with_hvs">Working with HVs</a></h2>
<p>To create an HV, you use the following routine:</p>
<pre>
<span class="variable">HV</span><span class="operator">*</span> <span class="variable">newHV</span><span class="operator">();</span>
</pre>
<p>Once the HV has been created, the following operations are possible on HVs:</p>
<pre>
<span class="variable">SV</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="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">val</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_fetch</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">lval</span><span class="operator">);</span>
</pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?