- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The ? : Operator in Perl
Let's check the conditional operator? : in Perl which can be used to replace if...else statements. It has the following general form −
Syntax
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The value of a? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. Below is a simple example making use of this operator −
Example
#!/usr/local/bin/perl $name = "Ali"; $age = 10; $status = ($age > 60 )? "A senior citizen" : "Not a senior citizen"; print "$name is - $status\n";
Output
This will produce the following result −
Ali is - Not a senior citizen
- Related Articles
- The Match Operator in Perl
- The Substitution Operator in Perl
- The Translation Operator in Perl
- Backstick Operator in Perl
- The $[ Special Variable in Perl
- The Infinite Loop in Perl
- The carp Function in Perl
- The cluck Function in Perl
- The croak Function in Perl
- The confess Function in Perl
- The G Assertion in Perl
- The system() Function in Perl
- The fork() Function in Perl
- The kill() Function in Perl
- Comments in Perl

Advertisements