perl5004delta.html

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

HTML
1,154 行
字号
what they will do.</p>
</dd>
<dd>
<p>The new conversions in Perl's <a href="../../lib/Pod/perlfunc.html#item_sprintf"><code>sprintf()</code></a> are:</p>
</dd>
<dd>
<pre>
   %i   a synonym for %d
   %p   a pointer (the address of the Perl value, in hexadecimal)
   %n   special: *stores* the number of characters output so far
        into the next variable in the parameter list</pre>
</dd>
<dd>
<p>The new flags that go between the <code>%</code> and the conversion are:</p>
</dd>
<dd>
<pre>
   #    prefix octal with &quot;0&quot;, hex with &quot;0x&quot;
   h    interpret integer as C type &quot;short&quot; or &quot;unsigned short&quot;
   V    interpret integer as Perl's standard integer type</pre>
</dd>
<dd>
<p>Also, where a number would appear in the flags, an asterisk (&quot;*&quot;) may
be used instead, in which case Perl uses the next item in the
parameter list as the given number (that is, as the field width or
precision).  If a field width obtained through &quot;*&quot; is negative, it has
the same effect as the '-' flag: left-justification.</p>
</dd>
<dd>
<p>See <a href="../../lib/Pod/perlfunc.html#sprintf">sprintf in the perlfunc manpage</a> for a complete list of conversion and flags.</p>
</dd>
</li>
<dt><strong><a name="item_keys_as_an_lvalue">keys as an lvalue</a></strong>

<dd>
<p>As an lvalue, <a href="../../lib/Pod/perlfunc.html#item_keys"><code>keys</code></a> allows you to increase the number of hash buckets
allocated for the given hash.  This can gain you a measure of efficiency if
you know the hash is going to get big.  (This is similar to pre-extending
an array by assigning a larger number to $#array.)  If you say</p>
</dd>
<dd>
<pre>
    <span class="keyword">keys</span> <span class="variable">%hash</span> <span class="operator">=</span> <span class="number">200</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>then <code>%hash</code> will have at least 200 buckets allocated for it.  These
buckets will be retained even if you do <code>%hash = ()</code>; use <code>undef
%hash</code> if you want to free the storage while <code>%hash</code> is still in scope.
You can't shrink the number of buckets allocated for the hash using
<a href="../../lib/Pod/perlfunc.html#item_keys"><code>keys</code></a> in this way (but you needn't worry about doing this by accident,
as trying has no effect).</p>
</dd>
</li>
<dt><strong><a name="item_my"><code>my()</code> in Control Structures</a></strong>

<dd>
<p>You can now use <a href="#item_my"><code>my()</code></a> (with or without the parentheses) in the control
expressions of control structures such as:</p>
</dd>
<dd>
<pre>
    <span class="keyword">while</span> <span class="operator">(</span><span class="keyword">defined</span><span class="operator">(</span><span class="keyword">my</span> <span class="variable">$line</span> <span class="operator">=</span> <span class="operator">&lt;&gt;))</span> <span class="operator">{</span>
        <span class="variable">$line</span> <span class="operator">=</span> <span class="keyword">lc</span> <span class="variable">$line</span><span class="operator">;</span>
    <span class="operator">}</span> <span class="keyword">continue</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="variable">$line</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
</dd>
<dd>
<pre>
    <span class="keyword">if</span> <span class="operator">((</span><span class="keyword">my</span> <span class="variable">$answer</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">STDIN</span><span class="operator">&gt;)</span> <span class="operator">=~</span> <span class="regex">/^y(es)?$/i</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="variable">user_agrees</span><span class="operator">();</span>
    <span class="operator">}</span> <span class="keyword">elsif</span> <span class="operator">(</span><span class="variable">$answer</span> <span class="operator">=~</span> <span class="regex">/^n(o)?$/i</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="variable">user_disagrees</span><span class="operator">();</span>
    <span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
        <span class="keyword">chomp</span> <span class="variable">$answer</span><span class="operator">;</span>
        <span class="keyword">die</span> <span class="string">"`$answer' is neither `yes' nor `no'"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
</dd>
<dd>
<p>Also, you can declare a foreach loop control variable as lexical by
preceding it with the word &quot;my&quot;.  For example, in:</p>
</dd>
<dd>
<pre>
    <span class="keyword">foreach</span> <span class="keyword">my</span> <span class="variable">$i</span> <span class="operator">(</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">3</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="variable">some_function</span><span class="operator">();</span>
    <span class="operator">}</span>
</pre>
</dd>
<dd>
<p>$i is a lexical variable, and the scope of $i extends to the end of
the loop, but not beyond it.</p>
</dd>
<dd>
<p>Note that you still cannot use <a href="#item_my"><code>my()</code></a> on global punctuation variables
such as $_ and the like.</p>
</dd>
</li>
<dt><strong><a name="item_pack"><code>pack()</code> and <a href="../../lib/Pod/perlfunc.html#item_unpack"><code>unpack()</code></a></a></strong>

<dd>
<p>A new format 'w' represents a BER compressed integer (as defined in
ASN.1).  Its format is a sequence of one or more bytes, each of which
provides seven bits of the total value, with the most significant
first.  Bit eight of each byte is set, except for the last byte, in
which bit eight is clear.</p>
</dd>
<dd>
<p>If 'p' or 'P' are given undef as values, they now generate a NULL
pointer.</p>
</dd>
<dd>
<p>Both <a href="#item_pack"><code>pack()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_unpack"><code>unpack()</code></a> now fail when their templates contain invalid
types.  (Invalid types used to be ignored.)</p>
</dd>
</li>
<dt><strong><a name="item_sysseek"><code>sysseek()</code></a></strong>

<dd>
<p>The new <a href="#item_sysseek"><code>sysseek()</code></a> operator is a variant of <a href="../../lib/Pod/perlfunc.html#item_seek"><code>seek()</code></a> that sets and gets the
file's system read/write position, using the <code>lseek(2)</code> system call.  It is
the only reliable way to seek before using <a href="../../lib/Pod/perlfunc.html#item_sysread"><code>sysread()</code></a> or syswrite().  Its
return value is the new position, or the undefined value on failure.</p>
</dd>
</li>
<dt><strong><a name="item_use">use VERSION</a></strong>

<dd>
<p>If the first argument to <a href="#item_use"><code>use</code></a> is a number, it is treated as a version
number instead of a module name.  If the version of the Perl interpreter
is less than VERSION, then an error message is printed and Perl exits
immediately.  Because <a href="#item_use"><code>use</code></a> occurs at compile time, this check happens
immediately during the compilation process, unlike <a href="../../lib/Pod/perlfunc.html#item_require"><code>require VERSION</code></a>,
which waits until runtime for the check.  This is often useful if you
need to check the current Perl version before <a href="#item_use"><code>use</code></a>ing library modules
which have changed in incompatible ways from older versions of Perl.
(We try not to do this more than we have to.)</p>
</dd>
</li>
<dt><strong>use Module VERSION LIST</strong>

<dd>
<p>If the VERSION argument is present between Module and LIST, then the
<a href="#item_use"><code>use</code></a> will call the VERSION method in class Module with the given
version as an argument.  The default VERSION method, inherited from
the UNIVERSAL class, croaks if the given version is larger than the
value of the variable $Module::VERSION.  (Note that there is not a
comma after VERSION!)</p>
</dd>
<dd>
<p>This version-checking mechanism is similar to the one currently used
in the Exporter module, but it is faster and can be used with modules
that don't use the Exporter.  It is the recommended method for new
code.</p>
</dd>
</li>
<dt><strong><a name="item_prototype"><code>prototype(FUNCTION)</code></a></strong>

<dd>
<p>Returns the prototype of a function as a string (or <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> if the
function has no prototype).  FUNCTION is a reference to or the name of the
function whose prototype you want to retrieve.
(Not actually new; just never documented before.)</p>
</dd>
</li>
<dt><strong><a name="item_srand">srand</a></strong>

<dd>
<p>The default seed for <a href="#item_srand"><code>srand</code></a>, which used to be <a href="../../lib/Pod/perlfunc.html#item_time"><code>time</code></a>, has been changed.
Now it's a heady mix of difficult-to-predict system-dependent values,
which should be sufficient for most everyday purposes.</p>
</dd>
<dd>
<p>Previous to version 5.004, calling <a href="../../lib/Pod/perlfunc.html#item_rand"><code>rand</code></a> without first calling <a href="#item_srand"><code>srand</code></a>
would yield the same sequence of random numbers on most or all machines.
Now, when perl sees that you're calling <a href="../../lib/Pod/perlfunc.html#item_rand"><code>rand</code></a> and haven't yet called
<a href="#item_srand"><code>srand</code></a>, it calls <a href="#item_srand"><code>srand</code></a> with the default seed. You should still call
<a href="#item_srand"><code>srand</code></a> manually if your code might ever be run on a pre-5.004 system,
of course, or if you want a seed other than the default.</p>
</dd>
</li>
<dt><strong><a name="item___">$_ as Default</a></strong>

<dd>
<p>Functions documented in the Camel to default to $_ now in
fact do, and all those that do are so documented in <a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_m_2f_2fgc_does_not_reset_search_position_on_failur"><a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//gc</code></a> does not reset search position on failure</a></strong>

<dd>
<p>The <a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//g</code></a> match iteration construct has always reset its target
string's search position (which is visible through the <a href="../../lib/Pod/perlfunc.html#item_pos"><code>pos</code></a> operator)
when a match fails; as a result, the next <a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//g</code></a> match after a failure
starts again at the beginning of the string.  With Perl 5.004, this
reset may be disabled by adding the &quot;c&quot; (for &quot;continue&quot;) modifier,
i.e. <a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//gc</code></a>.  This feature, in conjunction with the <code>\G</code> zero-width
assertion, makes it possible to chain matches together.  See <a href="../../lib/Pod/perlop.html">the perlop manpage</a>
and <a href="../../lib/Pod/perlre.html">the perlre manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_m_2f_2fx_ignores_whitespace_before__3f_2a_2b_7b_7d"><a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//x</code></a> ignores whitespace before ?*+{}</a></strong>

<dd>
<p>The <a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//x</code></a> construct has always been intended to ignore all unescaped
whitespace.  However, before Perl 5.004, whitespace had the effect of
escaping repeat modifiers like &quot;*&quot; or &quot;?&quot;; for example, <code>/a *b/x</code> was
(mis)interpreted as <code>/a\*b/x</code>.  This bug has been fixed in 5.004.</p>
</dd>
</li>
<dt><strong><a name="item_nested_sub_7b_7d_closures_work_now">nested <a href="../../lib/Pod/perlfunc.html#item_sub"><code>sub{}</code></a> closures work now</a></strong>

<dd>
<p>Prior to the 5.004 release, nested anonymous functions didn't work
right.  They do now.</p>
</dd>
</li>
<dt><strong><a name="item_formats_work_right_on_changing_lexicals">formats work right on changing lexicals</a></strong>

<dd>
<p>Just like anonymous functions that contain lexical variables
that change (like a lexical index variable for a <code>foreach</code> loop),
formats now work properly.  For example, this silently failed
before (printed only zeros), but is fine now:</p>
</dd>
<dd>
<pre>
    <span class="keyword">my</span> <span class="variable">$i</span><span class="operator">;</span>

⌨️ 快捷键说明

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