Perl

Perl Ternary Operator

The “if-else” statement is used to do conditional tasks in any programming language. The same task can be performed using a ternary operator. This operator is supported by most programming languages. Perl also supports the ternary operator. It uses the “?” symbol after the conditional expression and “:” to define the statements that will be executed if the condition is true or false. The statements of the left side of the colon (:) are executed if the condition is true. Otherwise, the statements of the right side of the colon (:) are executed. The methods of using the ternary operator in Perl are shown in this tutorial.

Syntax:

The syntax of the ternary operator is given as follows:

Conditionals? Statements (if the condition is true): Statements (if the condition is false)

 

Uses of the Ternary Operator in Perl

The uses of the ternary operator to execute the conditional statements in Perl are shown in this part of the tutorial.

Example 1: Using the Ternary Operator as the Alternative of “If-Else”

Create a Perl file with the following script where the use of the ternary operator as the alternative to the “if-else” statement is shown. A number is taken from the user and the input number is even or odd and is checked using both the “if-else” statement and “ternary” operator.

#!/local/bin/perl
 
use strict;
use warnings;
use 5.34.0;
 
#Take input from the user
print "Enter a number: ";
my $number = <>;
chomp($number);
 
#Check whether the number is odd or even
#using 'if-else'
say "\nOutput from 'if' :";
if($number % 2 == 0)
{
 say "$number is even.";
}
else
{
 say "$number is odd.";
}
#Check whether the number is odd or even
#using 'ternary operator
say "\nOutput from 'ternary operator' :";
my $output = ($number % 2 == 0) ? "$number is even." : "$number is odd.";
say $output;

 

Output:

The following output appears after executing with the input value of 89:

The following output appears after executing with the input value of 44:

Example 2: Using the Ternary Operator with Multiple Conditions

In the previous example, the ternary operator is used for a single condition. But the multiple conditions can be applied with the Boolean OR, AND, or NOT in the conditional expression of the ternary operator. Create a Perl file with the following script in which the use of a ternary operator with multiple conditions is shown. A numeric value is taken as input from the user and is checked whether the number is between 10 to 99 or not.

#!/local/bin/perl
 
use strict;
use warnings;
use 5.34.0;
 
#Take input from the user
print "Enter a number: ";
my $number = <>;
chomp($number);
 
#Check whether the number is within 10 to 99
my $output = ($number >= 10 && $number <100) ? "The number is within the range." : "The number is out of range.";
say $output;

 

Output:

The following output appears after executing the script with the input value of 5:

The following output appears after executing the script with the input value of 101:

The following output appears after executing the script with the input value of 50:

Example 3: Using Multiple Ternary Operators in the Same Statement

Create a Perl file with the following script where the use of nested ternary operators is shown. In this example, if the condition of the first ternary operator returns true, the condition of the second ternary operator is checked.

#!/local/bin/perl

use strict;
use warnings;
use 5.34.0;

#Take the post name
print "Enter the post name: ";
my $post = <>;
chomp($post);

#Take number of years
print "Enter the years of experience: ";
my $years = <>;
chomp($years);


#Check multiple conditions using multiple ternary operator
my $output = ($post eq 'Manager') ?
             ($years >= 5) ? "You will get 10% bonus." : "You will get 3% bonus."
             : "You are not eligible for the bonus.";
#Print the output
say $output;

 

Output:

According to the output, “Manager” and “8” are taken as the input values and these values matched with the conditions of both ternary operators. So, the message of the second ternary operator for the true condition is printed:

According to the output, “Manager” and “2” are taken as the input values and these values matched with the condition of the first ternary operator. So, the message of the second ternary operator for false condition is printed:

According to the output, “Assistant Manager” and “5” are taken as the input values. The first value did not match the condition of the first ternary operator. So, the message of the first ternary operator for false condition is printed:

Conclusion

The uses of ternary operators with single and multiple conditions in Perl are shown in this tutorial using multiple Perl scripts.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.