Learning Perl - Ternary Operators


Learning Perl - Ternary Operators


In a previous post, we learned about conditional statements in Perl. The ternary operator is an extension of that, so a shorthand way to write simple 'if-else' statements, enabling you to make decisions in a single line of code.

The ternary operator in Perl uses the '? :' characters and takes three arguments: a condition, a result if the condition is true, and a result if the condition is false.

As an example if you have a condition that checks if a number is even or odd, you could write this using a traditional 'if-else' statement:

my $number = 5;
my $result;
if ($number % 2 == 0) {
    $result = "Even";
} else {
    $result = "Odd";
}
print "The number is $result.\n";
Enter fullscreen mode Exit fullscreen mode

or you could use the ternary operator to achieve the same result in a more concise way:

my $number = 5;
my $result = ($number % 2 == 0) ? "Even" : "Odd";
print "The number is $result.\n";
Enter fullscreen mode Exit fullscreen mode

Both of these code snippets will output:

The number is Odd.
Enter fullscreen mode Exit fullscreen mode

The second example using the ternary operator is shorter and easier to read for simple conditions. The syntax is straightforward: you check the condition '($number % 2 == 0)', and if it's true, '$result' is assigned "Even"; otherwise, it gets "Odd". So the ternary operator is particularly useful for simple conditions where you want to assign a value based on a condition without needing multiple lines of code.

In Perl you can also nest ternary operators for more complex conditions, but be cautious as this can make your code harder to read. Here's an example of a nested ternary operator:

my $number = 5;
my $result = ($number < 0) 
    ? "Negative" 
    : ($number == 0)
        ? "Zero" 
        : "Positive";
print "The number is $result.\n";
Enter fullscreen mode Exit fullscreen mode

This will output:

The number is Positive.
Enter fullscreen mode Exit fullscreen mode

The nested ternary operator checks if the number is negative, zero, or positive and assigns the appropriate string to '$result'.

As demonstrated in this post the ternary operator is a powerful tool in Perl that can help you write cleaner and more concise code for simple conditional assignments. However, always prioritise readability, especially when dealing with more complex conditions. If the logic becomes too convoluted, it might be better to stick with traditional 'if-else' statements for clarity.

I hope this helps you understand how to use the ternary operator in Perl! If you have any questions or need further clarification, feel free to ask. Next time we will look at subroutines and how to write reusable blocks of code in Perl.

Related Blogs

Learning Perl – Introduction
Perl has long been known as the “duct tape of the Internet,” or "the Swiss Army chainsaw of scripting...
Learning Perl - Variables
I will attempt to explain things in this post in a way that is easy to understand, even for those who...
Learning Perl - Arrays
As stated in the previous post, Perl has three types of variables: scalars, arrays and hashes. Today...
Learning Perl - Hashes
In the last post we covered the basics of arrays, today we will look at hashes in more detail. What...
Learning Perl - Conditional Statements
So far we have covered basic variables in Perl, today we are going to look at how to use these...
Learning Perl - Loops and Iteration
In previous posts, we explored variables, arrays, hashes, and conditional statements in Perl. Now...
Learning Perl - Scalars
Before moving onto more complex topics lets come back to how we represent data in Perl. The most...
Learning Perl - References
In the last post we learnt how to create a reference to a scalar, an array, and a hash. In this post,...
Learning Perl - Subroutines
Subroutines are one of the most important building blocks in programming. They allow you to organise...
Learning Perl - Regular Expressions
Regular expressions also known as regex or regexp, are a powerful way to match patterns in text. Most...
Learning Perl - Modules
A module in Perl is a reusable piece of code that can be included in your scripts to provide...
Learning Perl - CPAN
In the last post I showed you how to create a new module and how to use it in your code. In this post...
Learning Perl - Plain Old Documentation
When you write and program in any language it is good practice to document your code. Each language...
Learning Perl - Testing
In this post we will look at how to test Perl code using the Test::More module. Like documentation,...
Learning Perl - Exporting
In programming, we often want to share functionality across different parts of our code. In Perl,...
Learning Perl - Object Orientation
Object-Oriented Programming (OOP) is a widely used programming paradigm that enables the creation of...
Learning Perl - Inheritance
In the last post we discussed Object Oriented Programming in Perl, focusing on the basics of creating...
Learning Perl - File Handles
In programming file processing is a key skill to master. Files are essential for storing data,...
Learning Perl - Prototypes
Today we are going to discuss Perl subroutine prototypes, which are a way to enforce a certain...
Learning Perl - Overloading Operators
In the last post we investigated prototype subroutines in Perl. In this post, we will look at...