Broad Network


Perl Conditional and Comma Operators

Perl Operators – Part 6

Perl Course

Foreword: In this part of the series, I talk about the conditional operator and the comma operators in Perl.

By: Chrysanthus Date Published: 12 Oct 2015

Introduction

This is part 6 of my series, Perl Operators. In this part of the series, I talk about the conditional operator and the comma operators in Perl. You should have read the previous parts of the series before reaching here, as this is a continuation.

The Conditional Operator
Consider the following program:

use strict;

    my $a = 5;
    my $b = 10;
    my $bigger;

    if ($a > $b)
        {
            $bigger = $a;
        }
    else
        {
            $bigger = $b;
        }

    print $bigger;

The code of interest begins with the declaration and assignment of $a, then $b. After that you have the declaration of $bigger. The aim of the program is to find the bigger of the two numbers, $a and $b. The variable, $bigger holds the bigger number.

The if-condition is ($a > $b). It checks if $a is bigger than $b. If it is, then the value of $a is assigned to $bigger as the if-block is evaluated. If it is not, then the value of $b is assigned to $bigger as the else-block is evaluated. Note, either the if-block or the else block is evaluated; both blocks cannot be evaluated. So, the variable, $bigger ultimately holds the bigger number. The else-block will also be evaluated if the two values are the same.

The last statement of interest prints the value of $bigger. Note that the if-block has only one statement, and the else-block has also only one statement.

Note: the condition, ($a > $b) is an expression and can be considered as an argument. The statement “$bigger = $a” and the statement “$bigger = $b;” can each be considered as an argument. So, there are three arguments in the if-construct. Also remember that either block above has just one argument.

The above if-construct, in that situation can be replaced with:

    $bigger = $a > $b ? $a : $b;

The operator in this statement is "?:". $a is placed in-between “?” and “:”. Even though “?” and “:” are separated by $a, they form one operator, "?:". Consider just the expression,

    $a > $b ? $a : $b;

This expression is the same as the statement above except for the preceding,. “$bigger =”. In this expression, “$a > $b” in front of ? is an argument; $a in-between “?” and “:” is also an argument; $b after “:” is an argument, as well. This expression is interpreted as follows:

If the argument (e.g. “$a > $b”) before the ? is true, the argument (e.g. $a) before the : is returned, otherwise the argument (e.g. $b) after the “:” is returned. There are three arguments involved: one in front of “?”, another in front of “:” and the last behind “:”.“$a > $b” is the if-condition; $a is the single argument of this contracted if-block; and $b is the single argument of the contracted else-block. So, “:” means “else” in this expression. The expression is a special contracted “if-then-else” expression.

The return value of the expression has to be held in a variable. And so you have the statement:

    $bigger = $a > $b ? $a : $b;

where $bigger holds the returned value. Of course, each of the three variables, $bigger, $a and $b can have a different name.

Read and try the following program that illustrates the use of the expression:

use strict;

    my $a = 5;
    my $b = 10;
    my $bigger;

    $bigger = $a > $b ? $a : $b;
    print $bigger;

In programming, many situations can be simulated by two states. So, conditional operator becomes handy here, where one state is for the if-block and the other state is for the else-block

The conditional operator is right associative.

The Comma Operator
The comma operator is used to separate list items. The comma operator is a binary operator and not a unary operator; it needs a left and right argument. If a function (subroutine) has a list of arguments, the comma is used to separate the arguments. The items of an array during creation, are separated by commas, as in:

    my @arr = ("HBWE", "FGTR", "HTNK", 4587, 4526, 4053, "AB12", "GB58", "TG45", "RE69");

The elements of a hash during creation, are also separated by commas.

The space in front or after the comma operator is optional. You must have used the comma operator before.

In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. In list context, it's just the list argument separator, and inserts both its arguments into the list.

The => Operator
The => operator is a kind of comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores. This includes arguments that might otherwise be interpreted as operators, constants, single number v-strings or function calls. If in doubt about this behavior, the left argument can be quoted explicitly. => is typically used in a hash as in:

    my %h = (foo => 23, bar => 40);

The comma operators (including =>) are left associative.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message