< Perl Tutorial

Perl conditional statements

← Previous Next →


Perl Conditional Statements

Perl conditional statements helps in the decision making, which require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

A typical decision-making structure in perl is as follows:


Perl does not have specific boolean type, but every scalar value - if checked using if will be either true or false. THere are certain values which are truthy and others which are falsy.

Falsy values - The following values are considered falsy:

  • undef - the undefined value
  • 0 the number 0, even if you write it as 000 or 0.0
  • '' the empty string
  • '0' the string that contains a single 0 digit

Truthy values - All other scalar values, including the following are true:

  • any non-0 number
  • ' ' the string with a space in it
  • '00' two or more 0 characters in a string
  • "0\n" a 0 followed by a newline
  • 'true'
  • even the string 'false' evaluates to true.

 

The following types of conditional statements are available in Perl:

  • if statement - An if statement consists of a boolean expression followed by one or more statements. Syntax is as follows: 
    if(boolean_expression){
       # statement(s) will execute if the given condition is true
    }
  • if...else statement - An if statement can be followed by an optional else statement. Syntax is: 
    if(boolean_expression){
       # statement(s) will execute if the given condition is true
    }else{
       # statement(s) will execute if the given condition is false
    }
  • if...elsif...else statement - An if statement can be followed by an optional elsif statement and then by an optional else statement. Syntax is as follows: 
    if(boolean_expression 1){
       # Executes when the boolean expression 1 is true
    }
    elsif( boolean_expression 2){
       # Executes when the boolean expression 2 is true
    }
    elsif( boolean_expression 3){
       # Executes when the boolean expression 3 is true
    }
    else{
       # Executes when the none of the above condition is true
    }
  • unless statement - An unless statement consists of a boolean expression followed by one or more statements. Syntax is: 
    unless(boolean_expression){
       # statement(s) will execute if the given condition is false
    }
  • unless...else statement - An unless statement can be followed by an optional else statement. Syntax is: 
    unless(boolean_expression){
       # statement(s) will execute if the given condition is false
    }else{
       # statement(s) will execute if the given condition is true
    }
  • unless...elsif...else statement - An unless statement can be followed by an optional elsif statement and then by an optional else statement. Syntax is: 
    unless(boolean_expression 1){
       # Executes when the boolean expression 1 is false
    }
    elsif( boolean_expression 2){
       # Executes when the boolean expression 2 is true
    }
    elsif( boolean_expression 3){
       # Executes when the boolean expression 3 is true
    }
    else{
       # Executes when the none of the above condition is met
    }
  • switch statement - we can make use of the switch statement which allows a simple way of comparing a variable value against various conditions. Syntax is: 
    use Switch;
    
    switch(argument){
       case 1            { print "number 1" }
       case "a"          { print "string a" }
       case [1..10,42]   { print "number in list" }
       case (\@array)    { print "number in list" }
       case /\w+/        { print "pattern" }
       case qr/\w+/      { print "pattern" }
       case (\%hash)     { print "entry in hash" }
       case (\&sub)      { print "arg to subroutine" }
       else              { print "previous case not true" }
    }
← Previous Next →