perldata.html
来自「perl教程」· HTML 代码 · 共 764 行 · 第 1/5 页
HTML
764 行
singular or plural. Some words in English work this way, like "fish"
and "sheep".</p>
<p>In a reciprocal fashion, an operation provides either a scalar or a
list context to each of its arguments. For example, if you say</p>
<pre>
int( <STDIN> )</pre>
<p>the integer operation provides scalar context for the <>
operator, which responds by reading one line from STDIN and passing it
back to the integer operation, which will then find the integer value
of that line and return that. If, on the other hand, you say</p>
<pre>
sort( <STDIN> )</pre>
<p>then the sort operation provides list context for <>, which
will proceed to read every line available up to the end of file, and
pass that list of lines back to the sort routine, which will then
sort those lines and return them as a list to whatever the context
of the sort was.</p>
<p>Assignment is a little bit special in that it uses its left argument
to determine the context for the right argument. Assignment to a
scalar evaluates the right-hand side in scalar context, while
assignment to an array or hash evaluates the righthand side in list
context. Assignment to a list (or slice, which is just a list
anyway) also evaluates the righthand side in list context.</p>
<p>When you use the <code>use warnings</code> pragma or Perl's <strong>-w</strong> command-line
option, you may see warnings
about useless uses of constants or functions in "void context".
Void context just means the value has been discarded, such as a
statement containing only <code>"fred";</code> or <a href="../../lib/Pod/perlfunc.html#item_getpwuid"><code>getpwuid(0);</code></a>. It still
counts as scalar context for functions that care whether or not
they're being called in list context.</p>
<p>User-defined subroutines may choose to care whether they are being
called in a void, scalar, or list context. Most subroutines do not
need to bother, though. That's because both scalars and lists are
automatically interpolated into lists. See <a href="../../lib/Pod/perlfunc.html#wantarray">wantarray in the perlfunc manpage</a>
for how you would dynamically discern your function's calling
context.</p>
<p>
</p>
<h2><a name="scalar_values">Scalar values</a></h2>
<p>All data in Perl is a scalar, an array of scalars, or a hash of
scalars. A scalar may contain one single value in any of three
different flavors: a number, a string, or a reference. In general,
conversion from one form to another is transparent. Although a
scalar may not directly hold multiple values, it may contain a
reference to an array or hash which in turn contains multiple values.</p>
<p>Scalars aren't necessarily one thing or another. There's no place
to declare a scalar variable to be of type "string", type "number",
type "reference", or anything else. Because of the automatic
conversion of scalars, operations that return scalars don't need
to care (and in fact, cannot care) whether their caller is looking
for a string, a number, or a reference. Perl is a contextually
polymorphic language whose scalars can be strings, numbers, or
references (which includes objects). Although strings and numbers
are considered pretty much the same thing for nearly all purposes,
references are strongly-typed, uncastable pointers with builtin
reference-counting and destructor invocation.</p>
<p>A scalar value is interpreted as TRUE in the Boolean sense if it is not
the null string or the number 0 (or its string equivalent, "0"). The
Boolean context is just a special kind of scalar context where no
conversion to a string or a number is ever performed.</p>
<p>There are actually two varieties of null strings (sometimes referred
to as "empty" strings), a defined one and an undefined one. The
defined version is just a string of length zero, such as <code>""</code>.
The undefined version is the value that indicates that there is
no real value for something, such as when there was an error, or
at end of file, or when you refer to an uninitialized variable or
element of an array or hash. Although in early versions of Perl,
an undefined scalar could become defined when first used in a
place expecting a defined value, this no longer happens except for
rare cases of autovivification as explained in <a href="../../lib/Pod/perlref.html">the perlref manpage</a>. You can
use the <a href="../../lib/Pod/perlfunc.html#item_defined"><code>defined()</code></a> operator to determine whether a scalar value is
defined (this has no meaning on arrays or hashes), and the <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef()</code></a>
operator to produce an undefined value.</p>
<p>To find out whether a given string is a valid non-zero number, it's
sometimes enough to test it against both numeric 0 and also lexical
"0" (although this will cause noises if warnings are on). That's
because strings that aren't numbers count as 0, just as they do in <strong>awk</strong>:</p>
<pre>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$str</span> <span class="operator">==</span> <span class="number">0</span> <span class="operator">&&</span> <span class="variable">$str</span> <span class="keyword">ne</span> <span class="string">"0"</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">warn</span> <span class="string">"That doesn't look like a number"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>That method may be best because otherwise you won't treat IEEE
notations like <code>NaN</code> or <code>Infinity</code> properly. At other times, you
might prefer to determine whether string data can be used numerically
by calling the POSIX::strtod() function or by inspecting your string
with a regular expression (as documented in <a href="../../lib/Pod/perlre.html">the perlre manpage</a>).</p>
<pre>
<span class="keyword">warn</span> <span class="string">"has nondigits"</span> <span class="keyword">if</span> <span class="regex">/\D/</span><span class="operator">;</span>
<span class="keyword">warn</span> <span class="string">"not a natural number"</span> <span class="keyword">unless</span> <span class="regex">/^\d+$/</span><span class="operator">;</span> <span class="comment"># rejects -3</span>
<span class="keyword">warn</span> <span class="string">"not an integer"</span> <span class="keyword">unless</span> <span class="regex">/^-?\d+$/</span><span class="operator">;</span> <span class="comment"># rejects +3</span>
<span class="keyword">warn</span> <span class="string">"not an integer"</span> <span class="keyword">unless</span> <span class="regex">/^[+-]?\d+$/</span><span class="operator">;</span>
<span class="keyword">warn</span> <span class="string">"not a decimal number"</span> <span class="keyword">unless</span> <span class="regex">/^-?\d+\.?\d*$/</span><span class="operator">;</span> <span class="comment"># rejects .2</span>
<span class="keyword">warn</span> <span class="string">"not a decimal number"</span> <span class="keyword">unless</span> <span class="regex">/^-?(?:\d+(?:\.\d*)?|\.\d+)$/</span><span class="operator">;</span>
<span class="keyword">warn</span> <span class="string">"not a C float"</span>
<span class="keyword">unless</span> <span class="regex">/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/</span><span class="operator">;</span>
</pre>
<p>The length of an array is a scalar value. You may find the length
of array @days by evaluating <code>$#days</code>, as in <strong>csh</strong>. However, this
isn't the length of the array; it's the subscript of the last element,
which is a different value since there is ordinarily a 0th element.
Assigning to <code>$#days</code> actually changes the length of the array.
Shortening an array this way destroys intervening values. Lengthening
an array that was previously shortened does not recover values
that were in those elements. (It used to do so in Perl 4, but we
had to break this to make sure destructors were called when expected.)</p>
<p>You can also gain some minuscule measure of efficiency by pre-extending
an array that is going to get big. You can also extend an array
by assigning to an element that is off the end of the array. You
can truncate an array down to nothing by assigning the null list
() to it. The following are equivalent:</p>
<pre>
<span class="variable">@whatever</span> <span class="operator">=</span> <span class="operator">();</span>
<span class="variable">$#whatever</span> <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span><span class="operator">;</span>
</pre>
<p>If you evaluate an array in scalar context, it returns the length
of the array. (Note that this is not true of lists, which return
the last value, like the C comma operator, nor of built-in functions,
which return whatever they feel like returning.) The following is
always true:</p>
<pre>
<span class="keyword">scalar</span><span class="operator">(</span><span class="variable">@whatever</span><span class="operator">)</span> <span class="operator">==</span> <span class="variable">$#whatever</span> <span class="operator">-</span> <span class="variable">$[</span> <span class="operator">+</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<p>Version 5 of Perl changed the semantics of <a href="../../lib/Pod/perlvar.html#item___"><code>$[</code></a>: files that don't set
the value of <a href="../../lib/Pod/perlvar.html#item___"><code>$[</code></a> no longer need to worry about whether another
file changed its value. (In other words, use of <a href="../../lib/Pod/perlvar.html#item___"><code>$[</code></a> is deprecated.)
So in general you can assume that</p>
<pre>
<span class="keyword">scalar</span><span class="operator">(</span><span class="variable">@whatever</span><span class="operator">)</span> <span class="operator">==</span> <span class="variable">$#whatever</span> <span class="operator">+</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<p>Some programmers choose to use an explicit conversion so as to
leave nothing to doubt:</p>
<pre>
<span class="variable">$element_count</span> <span class="operator">=</span> <span class="keyword">scalar</span><span class="operator">(</span><span class="variable">@whatever</span><span class="operator">);</span>
</pre>
<p>If you evaluate a hash in scalar context, it returns false if the
hash is empty. If there are any key/value pairs, it returns true;
more precisely, the value returned is a string consisting of the
number of used buckets and the number of allocated buckets, separated
by a slash. This is pretty much useful only to find out whether
Perl's internal hashing algorithm is performing poorly on your data
set. For example, you stick 10,000 things in a hash, but evaluating
%HASH in scalar context reveals <code>"1/16"</code>, which means only one out
of sixteen buckets has been touched, and presumably contains all
10,000 of your items. This isn't supposed to happen.</p>
<p>You can preallocate space for a hash by assigning to the <a href="../../lib/Pod/perlfunc.html#item_keys"><code>keys()</code></a> function.
This rounds up the allocated buckets to the next power of two:</p>
<pre>
<span class="keyword">keys</span><span class="operator">(</span><span class="variable">%users</span><span class="operator">)</span> <span class="operator">=</span> <span class="number">1000</span><span class="operator">;</span> <span class="comment"># allocate 1024 buckets</span>
</pre>
<p>
</p>
<h2><a name="scalar_value_constructors">Scalar value constructors</a></h2>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?