📄 ch03_01.htm
字号:
<html><head><title>Unary and Binary Operators (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Unary and Binary Operators"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch02_11.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part2.htm">Part 2: The Gory Details</a></td><td align="right" valign="top" width="172"><a href="ch03_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 3. Unary and Binary Operators</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch03_01.htm">Terms and List Operators (Leftward)</a><br><a href="ch03_02.htm">The Arrow Operator</a><br><a href="ch03_03.htm">Autoincrement and Autodecrement</a><br><a href="ch03_04.htm">Exponentiation</a><br><a href="ch03_05.htm">Ideographic Unary Operators</a><br><a href="ch03_06.htm">Binding Operators</a><br><a href="ch03_07.htm">Multiplicative Operators</a><br><a href="ch03_08.htm">Additive Operators</a><br><a href="ch03_09.htm">Shift Operators</a><br><a href="ch03_10.htm">Named Unary and File Test Operators</a><br><a href="ch03_11.htm">Relational Operators</a><br><a href="ch03_12.htm">Equality Operators</a><br><a href="ch03_13.htm">Bitwise Operators</a><br><a href="ch03_14.htm">C-Style Logical (Short-Circuit) Operators</a><br><a href="ch03_15.htm">Range Operator</a><br><a href="ch03_16.htm">Conditional Operator</a><br><a href="ch03_17.htm">Assignment Operators</a><br><a href="ch03_18.htm">Comma Operators</a><br><a href="ch03_19.htm">List Operators (Rightward)</a><br><a href="ch03_20.htm">Logical and, or, not, and xor</a><br><a href="ch03_21.htm">C Operators Missing from Perl</a><br></p></div><a name="INDEX-767"></a><a name="INDEX-768"></a><a name="INDEX-769"></a><a name="INDEX-770"></a><p><a name="INDEX-771"></a><a name="INDEX-772"></a><a name="INDEX-773"></a>In the last chapter, we talked about the various kinds ofterms you might use in an expression, but to be honest, isolated termsare a bit boring. Many terms are party animals. They like to haverelationships with each other. The typical young term feels strongurges to identify with and influence other terms in various ways, butthere are many different kinds of social interaction and manydifferent levels of commitment. In Perl, these relationships areexpressed using operators.</p><p>Sociology has to be good for something.</p><p><a name="INDEX-774"></a><a name="INDEX-775"></a>From a mathematical perspective, operators are just ordinary functionswith special syntax. From a linguistic perspective, operators are justirregular verbs. But as any linguist will tell you, the irregularverbs in a language tend to be the ones you use most often. And that'simportant from an information theory perspective because the irregularverbs tend to be shorter and more efficient in both production andrecognition.</p><p>In practical terms, operators are handy.</p><p><a name="INDEX-776"></a>Operators come in various flavors, depending on their<em class="emphasis">arity</em> (how many operands they take), their<em class="emphasis">precedence</em> (how hard they try to take thoseoperands away from surrounding operators), and their<em class="emphasis">associativity</em> (whether they prefer to do thingsright to left or left to right when associated with operators of thesame precedence).</p><p><a name="INDEX-777"></a><a name="INDEX-778"></a><a name="INDEX-779"></a><a name="INDEX-780"></a><a name="INDEX-781"></a><a name="INDEX-782"></a><a name="INDEX-783"></a><a name="INDEX-784"></a>Perl operators come in three arities: <em class="emphasis">unary</em>,<em class="emphasis">binary</em>, and <em class="emphasis">trinary</em> (or<em class="emphasis">ternary</em>, if your native tongue is Shibboleth).Unary operators are always prefix operators (except for thepostincrement and postdecrement operators).<a href="#FOOTNOTE-1">[1]</a> The others are all infixoperators--unless you count the list operators, which can prefix anynumber of arguments. But most people just think of list operators asnormal functions that you can forget to put parentheses around. Hereare some examples:<blockquote><pre class="programlisting">! $x # a unary operator$x * $y # a binary operator$x ? $y : $z # a trinary operatorprint $x, $y, $z # a list operator</pre></blockquote>An operator's precedence controls how tightly it binds. Operators withhigher precedence grab the arguments around them before operators withlower precedence. The archetypal example is straight out ofelementary math, where multiplication takes precedence over addition:<blockquote><pre class="programlisting">2 + 3 * 4 # yields 14, not 20</pre></blockquote>The order in which two operators of the same precedence are executed dependson their associativity. These rules also follow math conventions to someextent:<blockquote><pre class="programlisting">2 * 3 * 4 # means (2 * 3) * 4, left associative2 ** 3 ** 4 # means 2 ** (3 ** 4), right associative2 != 3 != 4 # illegal, nonassociative</pre></blockquote><a href="ch03_01.htm#table-perl3-tab-precedence">Table 3-1</a> lists the associativity and arityof the Perl operators from highestprecedence to lowest.<a name="INDEX-785"></a></p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1]Though youcan think of various quotes and brackets as circumfix operators thatdelimit terms.</p></blockquote><a name="table-perl3-tab-precedence"></a><h4 class="objtitle">Table 3.1. Operator Precedence</h4><table border="1"><tr><th>Associativity</th><th>Arity</th><th>Precedence Class</th></tr><tr><td>None</td><td>0</td><td>Terms, and list operators (leftward)</td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">-></tt></td></tr><tr><td>None</td><td>1</td><td><tt class="literal">++ --</tt></td></tr><tr><td>Right</td><td>2</td><td><tt class="literal">**</tt></td></tr><tr><td>Right</td><td>1</td><td><tt class="literal">! ~ ></tt> and unary <tt class="literal">+</tt> and <tt class="literal">-</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">=~ !~</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">* / % x</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">+ - .</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal"><< >></tt></td></tr><tr><td>Right</td><td>0,1</td><td>Named unary operators</td></tr><tr><td>None</td><td>2</td><td><tt class="literal">< > <= >= lt gt le ge</tt></td></tr><tr><td>None</td><td>2</td><td><tt class="literal">== != <=> eq ne cmp</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">&</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">| ^</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">&&</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">||</tt></td></tr><tr><td>None</td><td>2</td><td><tt class="literal">..</tt> <tt class="literal">...</tt></td></tr><tr><td>Right</td><td>3</td><td><tt class="literal">?:</tt></td></tr><tr><td>Right</td><td>2</td><td><tt class="literal">= += -= *=</tt> and so on</td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">, =></tt></td></tr><tr><td>Right</td><td>0+</td><td>List operators (rightward)</td></tr><tr><td>Right</td><td>1</td><td><tt class="literal">not</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">and</tt></td></tr><tr><td>Left</td><td>2</td><td><tt class="literal">or xor</tt></td></tr></table><a name="INDEX-786"></a><a name="INDEX-787"></a><p>It may seem to you that there are too many precedence levels toremember. Well, you're right, there are. Fortunately, you've got twothings going for you here. First, the precedence levels as they're
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -