perldata.html
来自「perl教程」· HTML 代码 · 共 764 行 · 第 1/5 页
HTML
764 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perldata - Perl data types</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perldata - Perl data types</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#variable_names">Variable names</a></li>
<li><a href="#context">Context</a></li>
<li><a href="#scalar_values">Scalar values</a></li>
<li><a href="#scalar_value_constructors">Scalar value constructors</a></li>
<ul>
<li><a href="#version_strings">Version Strings</a></li>
<li><a href="#special_literals">Special Literals</a></li>
<li><a href="#barewords">Barewords</a></li>
<li><a href="#array_joining_delimiter">Array Joining Delimiter</a></li>
</ul>
<li><a href="#list_value_constructors">List value constructors</a></li>
<li><a href="#subscripts">Subscripts</a></li>
<li><a href="#slices">Slices</a></li>
<li><a href="#typeglobs_and_filehandles">Typeglobs and Filehandles</a></li>
</ul>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perldata - Perl data types</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>
</p>
<h2><a name="variable_names">Variable names</a></h2>
<p>Perl has three built-in data types: scalars, arrays of scalars, and
associative arrays of scalars, known as "hashes". A scalar is a
single string (of any size, limited only by the available memory),
number, or a reference to something (which will be discussed
in <a href="../../lib/Pod/perlref.html">the perlref manpage</a>). Normal arrays are ordered lists of scalars indexed
by number, starting with 0. Hashes are unordered collections of scalar
values indexed by their associated string key.</p>
<p>Values are usually referred to by name, or through a named reference.
The first character of the name tells you to what sort of data
structure it refers. The rest of the name tells you the particular
value to which it refers. Usually this name is a single <em>identifier</em>,
that is, a string beginning with a letter or underscore, and
containing letters, underscores, and digits. In some cases, it may
be a chain of identifiers, separated by <code>::</code> (or by the slightly
archaic <code>'</code>); all but the last are interpreted as names of packages,
to locate the namespace in which to look up the final identifier
(see <a href="../../lib/Pod/perlmod.html#packages">Packages in the perlmod manpage</a> for details). It's possible to substitute
for a simple identifier, an expression that produces a reference
to the value at runtime. This is described in more detail below
and in <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.</p>
<p>Perl also has its own built-in variables whose names don't follow
these rules. They have strange names so they don't accidentally
collide with one of your normal variables. Strings that match
parenthesized parts of a regular expression are saved under names
containing only digits after the <code>$</code> (see <a href="../../lib/Pod/perlop.html">the perlop manpage</a> and <a href="../../lib/Pod/perlre.html">the perlre manpage</a>).
In addition, several special variables that provide windows into
the inner working of Perl have names containing punctuation characters
and control characters. These are documented in <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a>.</p>
<p>Scalar values are always named with '$', even when referring to a
scalar that is part of an array or a hash. The '$' symbol works
semantically like the English word "the" in that it indicates a
single value is expected.</p>
<pre>
<span class="variable">$days</span> <span class="comment"># the simple scalar value "days"</span>
<span class="variable">$days</span><span class="operator">[</span><span class="number">28</span><span class="operator">]</span> <span class="comment"># the 29th element of array @days</span>
<span class="variable">$days</span><span class="operator">{</span><span class="string">'Feb'</span><span class="operator">}</span> <span class="comment"># the 'Feb' value from hash %days</span>
<span class="variable">$#days</span> <span class="comment"># the last index of array @days</span>
</pre>
<p>Entire arrays (and slices of arrays and hashes) are denoted by '@',
which works much like the word "these" or "those" does in English,
in that it indicates multiple values are expected.</p>
<pre>
<span class="variable">@days</span> <span class="comment"># ($days[0], $days[1],... $days[n])</span>
<span class="variable">@days</span><span class="operator">[</span><span class="number">3</span><span class="operator">,</span><span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">]</span> <span class="comment"># same as ($days[3],$days[4],$days[5])</span>
<span class="variable">@days</span><span class="operator">{</span><span class="string">'a'</span><span class="operator">,</span><span class="string">'c'</span><span class="operator">}</span> <span class="comment"># same as ($days{'a'},$days{'c'})</span>
</pre>
<p>Entire hashes are denoted by '%':</p>
<pre>
%days # (key1, val1, key2, val2 ...)</pre>
<p>In addition, subroutines are named with an initial '&', though this
is optional when unambiguous, just as the word "do" is often redundant
in English. Symbol table entries can be named with an initial '*',
but you don't really care about that yet (if ever :-).</p>
<p>Every variable type has its own namespace, as do several
non-variable identifiers. This means that you can, without fear
of conflict, use the same name for a scalar variable, an array, or
a hash--or, for that matter, for a filehandle, a directory handle, a
subroutine name, a format name, or a label. This means that $foo
and @foo are two different variables. It also means that <code>$foo[1]</code>
is a part of @foo, not a part of $foo. This may seem a bit weird,
but that's okay, because it is weird.</p>
<p>Because variable references always start with '$', '@', or '%', the
"reserved" words aren't in fact reserved with respect to variable
names. They <em>are</em> reserved with respect to labels and filehandles,
however, which don't have an initial special character. You can't
have a filehandle named "log", for instance. Hint: you could say
<a href="../../lib/Pod/perlfunc.html#item_open"><code>open(LOG,'logfile')</code></a> rather than <a href="../../lib/Pod/perlfunc.html#item_open"><code>open(log,'logfile')</code></a>. Using
uppercase filehandles also improves readability and protects you
from conflict with future reserved words. Case <em>is</em> significant--"FOO",
"Foo", and "foo" are all different names. Names that start with a
letter or underscore may also contain digits and underscores.</p>
<p>It is possible to replace such an alphanumeric name with an expression
that returns a reference to the appropriate type. For a description
of this, see <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.</p>
<p>Names that start with a digit may contain only more digits. Names
that do not start with a letter, underscore, digit or a caret (i.e.
a control character) are limited to one character, e.g., <a href="../../lib/Pod/perlvar.html#item___"><code>$%</code></a> or
<a href="../../lib/Pod/perlvar.html#item___"><code>$$</code></a>. (Most of these one character names have a predefined
significance to Perl. For instance, <a href="../../lib/Pod/perlvar.html#item___"><code>$$</code></a> is the current process
id.)</p>
<p>
</p>
<h2><a name="context">Context</a></h2>
<p>The interpretation of operations and values in Perl sometimes depends
on the requirements of the context around the operation or value.
There are two major contexts: list and scalar. Certain operations
return list values in contexts wanting a list, and scalar values
otherwise. If this is true of an operation it will be mentioned in
the documentation for that operation. In other words, Perl overloads
certain operations based on whether the expected return value is
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?