perldata.html

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

HTML
764 行
字号
<p>Numeric literals are specified in any of the following floating point or
integer formats:</p>
<pre>
    12345
    12345.67
    .23E-10             # a very small number
    3.14_15_92          # a very important number
    4_294_967_296       # underscore for legibility
    0xff                # hex
    0xdead_beef         # more hex   
    0377                # octal (only numbers, begins with 0)
    0b011011            # binary</pre>
<p>You are allowed to use underscores (underbars) in numeric literals
between digits for legibility.  You could, for example, group binary
digits by threes (as for a Unix-style mode argument such as 0b110_100_100)
or by fours (to represent nibbles, as in 0b1010_0110) or in other groups.</p>
<p>String literals are usually delimited by either single or double
quotes.  They work much like quotes in the standard Unix shells:
double-quoted string literals are subject to backslash and variable
substitution; single-quoted strings are not (except for <code>\'</code> and
<code>\\</code>).  The usual C-style backslash rules apply for making
characters such as newline, tab, etc., as well as some more exotic
forms.  See <a href="../../lib/Pod/perlop.html#quote_and_quotelike_operators">Quote and Quote-like Operators in the perlop manpage</a> for a list.</p>
<p>Hexadecimal, octal, or binary, representations in string literals
(e.g. '0xff') are not automatically converted to their integer
representation.  The <a href="../../lib/Pod/perlfunc.html#item_hex"><code>hex()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_oct"><code>oct()</code></a> functions make these conversions
for you.  See <a href="../../lib/Pod/perlfunc.html#hex">hex in the perlfunc manpage</a> and <a href="../../lib/Pod/perlfunc.html#oct">oct in the perlfunc manpage</a> for more details.</p>
<p>You can also embed newlines directly in your strings, i.e., they can end
on a different line than they begin.  This is nice, but if you forget
your trailing quote, the error will not be reported until Perl finds
another line containing the quote character, which may be much further
on in the script.  Variable substitution inside strings is limited to
scalar variables, arrays, and array or hash slices.  (In other words,
names beginning with $ or @, followed by an optional bracketed
expression as a subscript.)  The following code segment prints out &quot;The
price is $100.&quot;</p>
<pre>
    <span class="variable">$Price</span> <span class="operator">=</span> <span class="string">'$100'</span><span class="operator">;</span>    <span class="comment"># not interpolated</span>
    <span class="keyword">print</span> <span class="string">"The price is $Price.\n"</span><span class="operator">;</span>     <span class="comment"># interpolated</span>
</pre>
<p>There is no double interpolation in Perl, so the <code>$100</code> is left as is.</p>
<p>As in some shells, you can enclose the variable name in braces to
disambiguate it from following alphanumerics (and underscores).
You must also do
this when interpolating a variable into a string to separate the
variable name from a following double-colon or an apostrophe, since
these would be otherwise treated as a package separator:</p>
<pre>
    <span class="variable">$who</span> <span class="operator">=</span> <span class="string">"Larry"</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="variable">PASSWD</span> <span class="string">"${who}::0:0:Superuser:/:/bin/perl\n"</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="string">"We use ${who}speak when ${who}'s here.\n"</span><span class="operator">;</span>
</pre>
<p>Without the braces, Perl would have looked for a $whospeak, a
<code>$who::0</code>, and a <code>$who's</code> variable.  The last two would be the
$0 and the $s variables in the (presumably) non-existent package
<code>who</code>.</p>
<p>In fact, an identifier within such curlies is forced to be a string,
as is any simple identifier within a hash subscript.  Neither need
quoting.  Our earlier example, <code>$days{'Feb'}</code> can be written as
<code>$days{Feb}</code> and the quotes will be assumed automatically.  But
anything more complicated in the subscript will be interpreted as an
expression.  This means for example that <code>$version{2.0}++</code> is
equivalent to <code>$version{2}++</code>, not to <code>$version{'2.0'}++</code>.</p>
<p>
</p>
<h3><a name="version_strings">Version Strings</a></h3>
<p><strong>Note:</strong> Version Strings (v-strings) have been deprecated.  They will
not be available after Perl 5.8.  The marginal benefits of v-strings
were greatly outweighed by the potential for Surprise and Confusion.</p>
<p>A literal of the form <code>v1.20.300.4000</code> is parsed as a string composed
of characters with the specified ordinals.  This form, known as
v-strings, provides an alternative, more readable way to construct
strings, rather than use the somewhat less readable interpolation form
<code>&quot;\x{1}\x{14}\x{12c}\x{fa0}&quot;</code>.  This is useful for representing
Unicode strings, and for comparing version &quot;numbers&quot; using the string
comparison operators, <code>cmp</code>, <code>gt</code>, <code>lt</code> etc.  If there are two or
more dots in the literal, the leading <code>v</code> may be omitted.</p>
<pre>
    <span class="keyword">print</span> <span class="variable">v9786</span><span class="operator">;</span>              <span class="comment"># prints UTF-8 encoded SMILEY, "\x{263a}"</span>
    <span class="keyword">print</span> <span class="variable">v102</span><span class="number">.111</span><span class="operator">.</span><span class="number">111</span><span class="operator">;</span>       <span class="comment"># prints "foo"</span>
    <span class="keyword">print</span> <span class="number">102.111</span><span class="operator">.</span><span class="number">111</span><span class="operator">;</span>        <span class="comment"># same</span>
</pre>
<p>Such literals are accepted by both <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> and <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> for
doing a version check.  The <a href="../../lib/Pod/perlvar.html#item___v"><code>$^V</code></a> special variable also contains the
running Perl interpreter's version in this form.  See <a href="../../lib/Pod/perlvar.html#__v">$^V in the perlvar manpage</a>.
Note that using the v-strings for IPv4 addresses is not portable unless
you also use the <code>inet_aton()/inet_ntoa()</code> routines of the Socket package.</p>
<p>Note that since Perl 5.8.1 the single-number v-strings (like <code>v65</code>)
are not v-strings before the <code>=&gt;</code> operator (which is usually used
to separate a hash key from a hash value), instead they are interpreted
as literal strings ('v65').  They were v-strings from Perl 5.6.0 to
Perl 5.8.0, but that caused more confusion and breakage than good.
Multi-number v-strings like <code>v65.66</code> and <code>65.66.67</code> continue to
be v-strings always.</p>
<p>
</p>
<h3><a name="special_literals">Special Literals</a></h3>
<p>The special literals __FILE__, __LINE__, and __PACKAGE__
represent the current filename, line number, and package name at that
point in your program.  They may be used only as separate tokens; they
will not be interpolated into strings.  If there is no current package
(due to an empty <code>package;</code> directive), __PACKAGE__ is the undefined
value.</p>
<p>The two control characters ^D and ^Z, and the tokens __END__ and __DATA__
may be used to indicate the logical end of the script before the actual
end of file.  Any following text is ignored.</p>
<p>Text after __DATA__ but may be read via the filehandle <code>PACKNAME::DATA</code>,
where <code>PACKNAME</code> is the package that was current when the __DATA__
token was encountered.  The filehandle is left open pointing to the
contents after __DATA__.  It is the program's responsibility to
<a href="../../lib/Pod/perlfunc.html#item_close"><code>close DATA</code></a> when it is done reading from it.  For compatibility with
older scripts written before __DATA__ was introduced, __END__ behaves
like __DATA__ in the toplevel script (but not in files loaded with
<a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> or <a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a>) and leaves the remaining contents of the
file accessible via <code>main::DATA</code>.</p>
<p>See <a href="../../lib/SelfLoader.html">the SelfLoader manpage</a> for more description of __DATA__, and
an example of its use.  Note that you cannot read from the DATA
filehandle in a BEGIN block: the BEGIN block is executed as soon
as it is seen (during compilation), at which point the corresponding
__DATA__ (or __END__) token has not yet been seen.</p>
<p>
</p>
<h3><a name="barewords">Barewords</a></h3>
<p>A word that has no other interpretation in the grammar will
be treated as if it were a quoted string.  These are known as
&quot;barewords&quot;.  As with filehandles and labels, a bareword that consists
entirely of lowercase letters risks conflict with future reserved
words, and if you use the <code>use warnings</code> pragma or the <strong>-w</strong> switch, 
Perl will warn you about any
such words.  Some people may wish to outlaw barewords entirely.  If you
say</p>
<pre>
    <span class="keyword">use</span> <span class="variable">strict</span> <span class="string">'subs'</span><span class="operator">;</span>
</pre>
<p>then any bareword that would NOT be interpreted as a subroutine call
produces a compile-time error instead.  The restriction lasts to the
end of the enclosing block.  An inner block may countermand this
by saying <code>no strict 'subs'</code>.</p>
<p>
</p>
<h3><a name="array_joining_delimiter">Array Joining Delimiter</a></h3>
<p>Arrays and slices are interpolated into double-quoted strings
by joining the elements with the delimiter specified in the <a href="../../lib/Pod/perlvar.html#item__"><code>$&quot;</code></a>
variable (<a href="../../lib/Pod/perlvar.html#item__list_separator"><code>$LIST_SEPARATOR</code></a> if &quot;use English;&quot; is specified), 
space by default.  The following are equivalent:</p>
<pre>
    <span class="variable">$temp</span> <span class="operator">=</span> <span class="keyword">join</span><span class="operator">(</span><span class="variable">$"</span><span class="operator">,</span> <span class="variable">@ARGV</span><span class="operator">);</span>
    <span class="keyword">system</span> <span class="string">"echo $temp"</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">system</span> <span class="string">"echo @ARGV"</span><span class="operator">;</span>
</pre>
<p>Within search patterns (which also undergo double-quotish substitution)

⌨️ 快捷键说明

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