perlsyn.html
来自「perl教程」· HTML 代码 · 共 685 行 · 第 1/5 页
HTML
685 行
<?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>perlsyn - Perl syntax</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>perlsyn - Perl syntax</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="#declarations">Declarations</a></li>
<li><a href="#comments">Comments</a></li>
<li><a href="#simple_statements">Simple Statements</a></li>
<li><a href="#truth_and_falsehood">Truth and Falsehood</a></li>
<li><a href="#statement_modifiers">Statement Modifiers</a></li>
<li><a href="#compound_statements">Compound Statements</a></li>
<li><a href="#loop_control">Loop Control</a></li>
<li><a href="#for_loops">For Loops</a></li>
<li><a href="#foreach_loops">Foreach Loops</a></li>
<li><a href="#basic_blocks_and_switch_statements">Basic BLOCKs and Switch Statements</a></li>
<li><a href="#goto">Goto</a></li>
<li><a href="#pods__embedded_documentation">PODs: Embedded Documentation</a></li>
<li><a href="#plain_old_comments__not__">Plain Old Comments (Not!)</a></li>
</ul>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlsyn - Perl syntax</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>A Perl program consists of a sequence of declarations and statements
which run from the top to the bottom. Loops, subroutines and other
control structures allow you to jump around within the code.</p>
<p>Perl is a <strong>free-form</strong> language, you can format and indent it however
you like. Whitespace mostly serves to separate tokens, unlike
languages like Python where it is an important part of the syntax.</p>
<p>Many of Perl's syntactic elements are <strong>optional</strong>. Rather than
requiring you to put parentheses around every function call and
declare every variable, you can often leave such explicit elements off
and Perl will figure out what you meant. This is known as <strong>Do What I
Mean</strong>, abbreviated <strong>DWIM</strong>. It allows programmers to be <strong>lazy</strong> and to
code in a style with which they are comfortable.</p>
<p>Perl <strong>borrows syntax</strong> and concepts from many languages: awk, sed, C,
Bourne Shell, Smalltalk, Lisp and even English. Other
languages have borrowed syntax from Perl, particularly its regular
expression extensions. So if you have programmed in another language
you will see familiar pieces in Perl. They often work the same, but
see <a href="../../lib/Pod/perltrap.html">the perltrap manpage</a> for information about how they differ.</p>
<p>
</p>
<h2><a name="declarations">Declarations</a></h2>
<p>The only things you need to declare in Perl are report formats and
subroutines (and sometimes not even subroutines). A variable holds
the undefined value (<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>) until it has been assigned a defined
value, which is anything other than <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>. When used as a number,
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is treated as <code>0</code>; when used as a string, it is treated as
the empty string, <code>""</code>; and when used as a reference that isn't being
assigned to, it is treated as an error. If you enable warnings,
you'll be notified of an uninitialized value whenever you treat
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> as a string or a number. Well, usually. Boolean contexts,
such as:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$a</span><span class="operator">;</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$a</span><span class="operator">)</span> <span class="operator">{}</span>
</pre>
<p>are exempt from warnings (because they care about truth rather than
definedness). Operators such as <code>++</code>, <code>--</code>, <code>+=</code>,
<code>-=</code>, and <code>.=</code>, that operate on undefined left values such as:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$a</span><span class="operator">;</span>
<span class="variable">$a</span><span class="operator">++;</span>
</pre>
<p>are also always exempt from such warnings.</p>
<p>A declaration can be put anywhere a statement can, but has no effect on
the execution of the primary sequence of statements--declarations all
take effect at compile time. Typically all the declarations are put at
the beginning or the end of the script. However, if you're using
lexically-scoped private variables created with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a>, you'll
have to make sure
your format or subroutine definition is within the same block scope
as the my if you expect to be able to access those private variables.</p>
<p>Declaring a subroutine allows a subroutine name to be used as if it were a
list operator from that point forward in the program. You can declare a
subroutine without defining it by saying <code>sub name</code>, thus:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> myname</span><span class="operator">;</span>
<span class="variable">$me</span> <span class="operator">=</span> <span class="variable">myname</span> <span class="variable">$0</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't get myname"</span><span class="operator">;</span>
</pre>
<p>Note that <code>myname()</code> functions as a list operator, not as a unary operator;
so be careful to use <code>or</code> instead of <code>||</code> in this case. However, if
you were to declare the subroutine as <code>sub myname ($)</code>, then
<code>myname</code> would function as a unary operator, so either <code>or</code> or
<code>||</code> would work.</p>
<p>Subroutines declarations can also be loaded up with the <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> statement
or both loaded and imported into your namespace with a <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement.
See <a href="../../lib/Pod/perlmod.html">the perlmod manpage</a> for details on this.</p>
<p>A statement sequence may contain declarations of lexically-scoped
variables, but apart from declaring a variable name, the declaration acts
like an ordinary statement, and is elaborated within the sequence of
statements as if it were an ordinary statement. That means it actually
has both compile-time and run-time effects.</p>
<p>
</p>
<h2><a name="comments">Comments</a></h2>
<p>Text from a <code>"#"</code> character until the end of the line is a comment,
and is ignored. Exceptions include <code>"#"</code> inside a string or regular
expression.</p>
<p>
</p>
<h2><a name="simple_statements">Simple Statements</a></h2>
<p>The only kind of simple statement is an expression evaluated for its
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?