Questions And Answers

More Tutorials

Perl If-elsif-else, unless

Statement Blocks
A statement block is simply a sequence of statements enclose in curly braces:

Conditional Structures (If/elsif/else)
The basic construction to execute blocks of statements is the if statement. The if statement permits execution of the associated statement block if the test expression evaluates as true. It is important to note that unlike many compiled languages, it is
necessary to enclose the statement block in curly braces, even if only one statement is to be executed.

The general form of an if/then/else type of control statement is as follows:
if (expression_one) {
} elsif (expression_two) {
} else {

For convenience, Perl also offers a construct to test if an expression is false:
unless (expression) {
} else {

Note that the order of the conditional can be inverted as well:
statement if (expression);
statement unless (expression);

The “ternary” operator is another nifty one to keep in your bag of tricks:
$var = (expression) ? true_value : false_value;

It is equivalent to:
if (expression) {
$var = true_value;
} else {
$var = false_value;


In this page (written and validated by ) you learned about Perl If-elsif-else, unless . What's Next? If you are interested in completing Perl tutorial, your next topic will be learning about: Perl do while until for foreach.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.