Home » Perl Basics
6

Conditions

A brief overview of IF, UNLESS conditions as well as operators

This chapter is short thanks to the simplistic implementation of the conditions. However you will use conditions a lot and most of the time with regular expressions.

If

"If" condition will allow you to execute code IF a condition is true . If not you will be able to execute another piece of code.

  If (condition) {
  Code;
  }
  Else {
  Alternative code
  }

You can if you want imbricate several If conditions within each other by using elsif AND NOT ELSE IF. If (condition1) { Code } Elsif (condition2) { Code2 } Else { Code 3 }

Unless

Another conditional is unless. You can see it as the opposite of if. It shares the same syntax with the opposing effect.

  Unless (condition) {
  code}

print and if can also be used on one line .

  print “text” if (condition);
  print ‘text” unless(condition);
This syntax is more language like and may be preferred when no else will be used.

Operators

Sting and integer will require different operators.

For Integer

you will use roughly the same as in general mathematic.

#IMPORTANT “=” is used to assign value to a variable “==” is use to test for equality

== is equal
< is smaller than
> is greater than
>= is greater or equal to
<= is smaller or equal to

example :
If (3 <= 5) {
print “3 inferior to 5”;
}


For Strings


Eq = equal
Gt = greater than
Ge = greater or equal to
Lt = less than
Le = less or equal to

example :
If (string1 eq string2) {
print “string1 is equal to string2”;
}