perltutorial
Melly
<h1>Introduction</h1>
<p>Please note that this tutorial is undergoing revision and expansion, so the comments that follow it may apply to an earlier version. This version is dated: 5-Dec-2006</p>
<h1>The Basics - Getting Your Sums Right</h1>
<p>If, like me, you don't come from a comp-sci background, then precedence-awareness of [doc://perlop#Operator-Precedence-and-Associativity--operator,-precedence-precedence-associativity|operators] probably only goes as far as knowing that <code>2+3*4</code> means <code>2+(3*4)</code>, and that if you want <code>(2+3)*4</code>, then you'd better damn well say so.</p>
<p>Beyond that, <code>5-1+2</code> might have you scratching your head - which has precedence - the '-' or the '+'? The answer is 'whichever comes first' - they have equal precedence but left associativity, so <code>5-1+2</code> is <code>(5-1)+2</code>, but <code>5+1-2</code> is <code>(5+1)-2</code> (although you'll have fun proving that last one).</p>
<p>... and it's worth mentioning for the comp-sci challenged that left-associativity means "for a sequence of operators of equal precedence, give the left-most operator the precedence". Right-associativity means the reverse. For example, ** (the 'to-the-power-of' operator) has right-associativity, so <code>2**3**4</code> is <code>2**(2**3)</code>, not <code>(2**2)**3</code>.</p>
<p>So far, it's all pretty straight-forward. Whether or not you know what the rules for precedence are for the basic maths operators, you are aware that they exist and need to exist, and, if in doubt, or if you just want to make things clearer for yourself or the code-maintainer, you can always use brackets to make the order of operations explicit.</p>
<h1>First Among Equals</h1>
<p>So far, so good - that is until we get to the numeric-equality test, '==' and the assignment operator, '='.</p>
<p>The first thing to note (or at least remember) about these is that don't really have anything in common with each other. Nor do either have any strict equivalent in maths (unlike, say, '*' and '/', etc.).</p>
<p>It may be tempting to think otherwise, since <code>$x = 2*4</code> (Perl) seems to behave a bit like <code>X = 2 x 4</code> (maths). However, since we can use '=' to assign just about anything to $x, including "hello world", it really doesn't have anything to do with numbers.</p>
<p>In Perl, '==', and its evil-twin, '!=', are perhaps a bit closer to the maths-class meaning of '=', since all are associated with the numeric equality of the calculations on either side - however, in maths if the two sides don't match the operator, then you've probably made a mistake, whereas in Perl if the two sides don't match the operator, then you've just performed a valid test.</p>
<p>Nevertheless, the notion of precedence for these operators is somewhat confusing - if precedence is important, does that mean that we have to write <code>($x+$y) == (12/3)</code> to avoid something like <code>$x+($y == 12/3)</code> happening? And what would that mean anyway?</p>
<p>By and large, you don't need to worry. Both '=' and '==' have such low precedence that they will almost always behave as you expect (and certainly as far as any maths-based functions go), without any need for parenthesis.</p>
<h1>Logical Questions</h1>
<p>However, there are some traps when we start combining '==' and '=' with the various logical operators, such as 'and' and 'or', and their alternatives, '&&' and '¦¦', as these do have lower precedence.</p>
<p>For example, <code>(5 or 2 == 12)</code> doesn't mean "does 5 or 2 equal 12?" (which would be false), instead it translates to <code>5 or (2 == 12)</code>, or "if 5 is true or if 2 equals 12" (which is true - 5 is a 'true' value).</p>
<p>To add to the confusion, '&&' and '¦¦' have a higher precedence than '=', whereas 'and' and 'or' have a lower precedence. This means that <code>$x = 4==5 ¦¦ 5==5</code> has quite a different meaning than <code>$x = 4==5 or 5==5</code> - the first will set $x to 1 ('true') if either 4 or 5 is equal to 5, and will set $x to false if they are not. The second version will set $x to true or false purely on the basis of whether 4 is equal to 5 (and will go on to check whether 5 is equal to 5 if it fails to set $x to a value).</p>
<p>Below is a short table that will hopefully make all of this a little clearer.</p>
<table border="1">
<tr><td>Function</td><td>Meaning</td><td>$x is now..</td></tr>
<tr><td>$x = 5 == 6 or 5 == 5</td><td>($x = (5 == 6)) or ($x = (5 == 5))</td><td>FALSE</td></tr>
<tr><td>$x = (5 == 6 or 5 == 5)</td><td>$x = ((5 == 6) or (5 == 5))</td><td>TRUE</td></tr>
<tr><td>$x = 5 == 6 ¦¦ 5 == 5</td><td>$x = ((5 == 6) ¦¦ (5 == 5))</td><td>TRUE</td></tr>
<tr><td>($x = 5 == 6) ¦¦ 5 == 5</td><td>($x = 5 == 6) ¦¦ 5 == 5</td><td>FALSE</td></tr>
<tr><td>$x = 5 ¦¦ 6 == 6</td><td>$x = (5 ¦¦ (6 == 6))</td><td>5</td></tr>
<tr><td>$x = (5 ¦¦ 6) == 6</td><td>$x = ((5 ¦¦ 6) == 6)</td><td>TRUE</td></tr>
<tr><td>$x = 5 or 6 == 6</td><td>($x = 5) ¦¦ ($x = (6 == 6))</td><td>5</td></tr>
<tr><td>$x = 1 == 2 && 3</td><td>$x = (1 == 2) && $x = 3</td><td>3</td></tr>
<tr><td>$x = 1 == 2 ¦¦ 3</td><td>$x = (1 == 2) ¦¦ $x = 3</td><td>FALSE</td></tr>
</table>
<p>The real lesson here is that when you start mixing '==' or '=' with any logical operators, get into the habit of using parenthesis... and just to rub that in, let's take a look at another logical operator, the slightly obscure, but extremely useful '?:' - and a particular trap you can fall into due to making unwarranted assumptions about the behavior of '='.</p>
<h1>?: - If If/Else fails...</h1>
<p>The '?:' operator is probably the least-known operator, so let's take a quick look at what it does.</p>
<p>The basic syntax is: <code><test>?<value to return if test is true>:<value to return if test is false></code></p>
<p>Now, the "?:" construct is [doc://perlop#Conditional-Operator-operator,-conditional-operator,-ternary-ternary-?: |very useful] - basically, it means that we can replace the following code:</p>
<code>
if($x){
$y=1;
}
else{
$y=0;
}
</code>
<p>with:</p>
<code>
$y = $x ? 1 : 0;
</code>
<p>Which is all well and good - unless you make the [id://586664|mistake] of writing:</p>
<code>
$x ? $y=1 : $y=0;
</code>
<p>If you run the above code, you will find that, whatever value you assign to $x, you are always told that, apparently, $x was false (i.e. $y is set to 0).</p>
<p>So how did that happen, why was it confusing (IMHO), and what can you do about it?</p>
<p>Well, to illustrate what happened, let's write an alternative version that doesn't exhibit the problem, but looks pretty much identical (using a reg-ex substitution instead of '='):</p>
<code>
$x ? $y=1 : $y=~s/.*/0/;
</code>
<p>This time, we get the result we expect. So what happened in the bad version that didn't happen here? Well the first thing to notice in the [doc://perlop#Operator-Precedence-and-Associativity--operator,-precedence-precedence-associativity|operator-precedence table] is that '=~' has a higher precedence than '?:', but '=' has a lower precedence. So what? All that means, presumably, is that we decide on the truth or falsehood of our initial condition before we assign any value to $y (which sounds like a good thing). </p>
<p>Well... no. What precedence conceptually means in this context is "where is the boundary of our false expression?" and the answer is "it's when we hit an operator with a lower precedence than '?:'"</p>
<p>So <code>$x ? $y=1 : $y=0</code> can be expressed as <code>($x ? $y=1 : $y)=0</code> - which, if $x is false, leads to <code>($y)=0</code> (correct), but if $x is true, leads to <code>($y=1)=0</code> (uh-oh - we did set $y to 1, but then immediately reset it to 0).</p>
<p>Now, when we replace a false expression such as <code>$y=0</code> with <code>$y=~s/.*/0/</code>, the higher precedence of '=~' means that Perl evaluates this as:</p>
<code>
$x ? $y=1 : ($y=~s/.*/0/)
</code>
<p>which is probably what we (the comp-sci challenged) expected in the first example.</p>
<p>Bottom line, '?:' can benefit from parenthesis just as much as <code>(2+3)*5</code> - here is the bad code made good:</p>
<code>
$x ? $y=1 : ($y=0);
</code>
<p>As a small side-note, really we ought to be writing <code>$x ? ($y=1) : ($y=0);</code>, but Perl 'knows' that the function between '?' and ':' must be our 'true' function and is kind enough to add the virtual parenthesis for us...</p>
<p>...and, as noted before, we can avoid the need for parenthesis, and save a few key-strokes, by writing:</p>
<code>
$y = $x ? 1 : 0;
</code>
<p>... which is really what we should have done in the first place - there is an Meditation discussing the use of '?:' at [id://587227].</p>
<h1>A Final Word</h1>
<p>This is not meant to be an exhaustive look at precedence and operators - I haven't mentioned the bit-wise operators for example. However, I hope I've covered the issues likely to fox the comp-sci challenged (basically, if you're using bit-wise operators, I assume you know what you're doing).</p>
<p>Also, I'm half-tempted (well, 25% tempted) to replace this tutorial with just the one sentence "USE LOTS OF PARENTHESIS" - it's certainly the bottom line. They will make your code more readable, and you will avoid most of the traps associated with precedence.</p>
<p>That said, don't go over the top:</p>
<code>
$x = ((((((1 * 2) * 3) * (4 ** 2)) * 5) * 6) * 7)
</code>
<p>is not really helping anyone....</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-66612">
Tom Melly, pm@tomandlu.co.uk
</div></div>