Conditional statements, using if, else, elsif in Perl

So far in the tutorial each statement got executed one after the other. There
are cases when we want to execute certain statements only if some condition is met: i.e. if the condition
evaluates to "true".

Both the parentheses around the condition and the curly braces {} are required.
(A special note for people with background in the C programming language:
Even if there is only one statement, that statement needs to be wrapped in a pair of curly braces.)

The condition itself can be any Perl-expression. If it is true,
the statement(s) inside the curly braces will be executed. If it is false,
it will be skipped. (For what constitutes true and false, check the article about boolean values.)

Layout: Indentation

As in Perl white-spaces are not significant we could have laid-out the code in a number of different ways,
without any impact on what the code does, but let me point out the way you see it here and why
it is the recommended layout. The important part is that the code inside the curly braces does not start
at the beginning of the line. It starts a few characters to the right. This is called indentation.
In the tutorial we will see a number of statements that have an internal block (a pair of curly braces),
and in each one of those we'll indent the code to the right. This indentation makes it easier for the
reader to see which parts of the code is inside the block and what is outside.
There are languages, such as Python that force you to add such indentation. Perl is much more laid-back,
and much more flexible in this area, but it is still strongly recommended that you always indent your code.

People love to debate whether one should use a tab character or spaces for indentation,
and the "spaces" camp is also subdivided by the number of spaces. I think it is not the critical part.
The important thing is that the code will be indented in a consistent way.

In these examples I use 4 spaces, but when I write an application I usually use TAB characters.
When I have to edit the code written by other people, I try to follow their style. If there is one.

if - else

There are cases when we want to do something if a condition is met (when it is true),
and something else if it is false.
For this we can use the else extension of the if-statement:

One is that the width of the screen is limited. If you indent too much then either your rows will be too wide and won't fit in
the screen or you'll have to wrap the code. Both make it harder to read the code.

The second one is that most programmers when reading code tend to focus on what's on the left hand side and either neglect the right hand side
of the code or just perceive as less important. So by indenting the code you send the message to the maintenance programmer, that the
nested conditions are less important.
If this is your intention then go ahead write the code this way, on the other hand, if the only reason to write nested conditions is
because that's the easy order to evaluate the conditions, then you'd be better off using elsif and flattening the construct.

This code does exactly as the previous one, but this time, instead of nesting an if-statement inside the else-block,
we used an elsif-statement which means we don't need an extra indentation level.
This also sends the message that the 4 cases have the same importance.

When you code, remember, the computer will understand you either way, but make it easy for the next reader to understand
your intentions. Who knows, that person might be you...

else if in Perl

Please also note that the spelling of the "else if" construct has the most diversity among programming languages. In Perl it is written in one word with a single 'e': elsif.

Empty block

Though usually it is not necessary, in Perl we can also have empty blocks:

The 3 dots there, is called the yada, yada operator. It is what most people would
write when they are jotting down some text and want to leave place for more. Some might write
etc.

Since version 5.10 came out, it is also a valid construct in Perl. It will throw an Unimplemented exception if it is executed.
It can be very handy, but probably a bit premature to learn so early in the Perl Tutorial.