Control Structures in Perl

The “unless” control structure

  • Opposite of the “if” condition
  • Only executes the code block if the condition is FALSE
# UNLESS Control Structure
 $_ = "foey foo foobar!";
 unless (/foobarz/) { print "Inside of UNLESS block \n"; } # this prints 
 unless (/foobar/) { print "Inside of UNLESS block \n"; } # this does NOT print
  • The “until” Control Structure
  • Opposite of the “while” condition
  • Runs the loop while FALSE. Breaks the loop when condition is TRUE.
# UNTIL Control Structure
 $var1 = 5; $var2 = 1;
 until ($var2 > $var1) { # This starts as False, runs until True
 print $var2;
 $var2 += 1;
 } # Final output: 12345
  • Expression Modifiers

 

  • A compact writing notation, specifically for the programmer so that they do not have to write as much code
  • Have an expression followed by a modifier that controls it
# Expression Modifiers
 $var1 = 1;
 print "$var1 is the start value. \n" if $var1 > 0; # this line is printed
 &myfunc("some parameter that is ignored") unless 1 == 2; # myfunc is printed

sub myfunc { print "In myfunc \n"; }

@mylist = (1,2,3);
 &myfunc($_) foreach @mylist; # myfunc printed 3 times

$var1 += 1 until $var1 > 5;
 print $var1 . "\n"; # 6
  • The Naked Block Control { }

 

  • Any section of code may be blocked with empty conditions by using { }
  • Any code within these blocks are scoped within that block only
{
 my $var = 1;
 print $var;
 }
 print $var; # this will be UNDEF as $var is out of scope
  • Auto-increment and Auto-decrement
# Auto Increments/Decrements
 $var1 = 1;
 $var2 = 1;
 print $var1++ . " " . ++$var2 . " \n"; # 1 2 – postincrement , preincrement
 print $var1 . " " . $var2 . "\n"; # 2 2 – final result
 $var3 = --$var1; # predecrement
 $var4 = $var2--; # postdecrement

print $var3 . " " . $var4 . "\n"; # 1 2
  • The loop Controls

3 major types of loop controls: last, next, and redo

  • Last is like Break
  • Next is like Continue
  • Redo is unlike anything. It is Perl’s way to restart the current iteration. Unlike Next, it just re-executes the current iteration.
# Loop Controls
 foreach (1..10) {
 print "Iteration number: $_ \n";
 print "Enter 1-Last, 2-Next, 3-Redo \n";
 chomp ($input = <STDIN>);
 last if $input =~ /1/; # exits loop
 next if $input =~ /2/; # next iteration of loop
 redo if $input =~ /3/; # repeats this iteration of loop
 print "Invalid Choice - going to next iteration...\n";
 }
  • The Ternary Operator ?:

This is an IF – THEN – ELSE operation statement

expression ? if_true_expression : if_false_expression

# Ternary Operator ?:
 $var1 = 3;
 $var2 = 
 ($var1 == 1) ? "small" :
 ($var1 == 2) ? "medium" :
 ($var1 == 3) ? "large" :
 "unknown";
 print $var1 . " " . $var2 . "\n"; # 3 large
  • Logical Operators

&& # and

|| # or

<= # less than equal to

>= # greater than equal to

eq # equal to

!= # not equal to

?: # ternary operator

# Special Partial Evaluation operators
 $var1 = 1;
 $var2 = 2;
 $var3 = $var4 = 0;
 ($var1 < $var2) && ($var3 = $var1);

# In the line above, the && operator evaluates the left of the expression first
 # Since var1 < var2 is True, the && passes and thereby evalutes the right side of expression
 # Therefore, var3 is set as var1.

print "$var1 $var2 $var3 $var4 \n";
 ($var1 > $var2) || print "var1 is not greater than var2 \n";

# In the line above, the || (or) is looking for a True from either side. The left expression
 # is evaluated but results in False. Therefore the right is evaluated, which leads to 
 # printing the string

($var1 > $var2) ? ($var3 = $var1) : ($var3 = $var2);
 print "$var1 $var2 $var3 \n";

# In the above ternary statement, var1 is less than var2 so the FALSE (or else) part of the
 # expression is evaluated. Therefore var3 = var2. Result is: 1 2