The if Condition

The if condition lets you check to see if a statement is true using a comparison operator. If the statement is true, the next block of code between the curly braces {} will be executed, else perl moves on to the eht next command.

sample if statement:

CODE:
  1. $mbytes=5;
  2.  
  3. if ($mbytes <10)
  4.  {
  5.    print "Not much space for storage";
  6.  }
  7.  
  8. print "Plenty of free space remaining.";

This checks to see if the variable $bytes is less than 10. If true, it executes the statement inside the braces, and then goes on. If not, it will go straight to the print statement after the braces. If $mbytes being 5, it prints this result:

Not much space for storage
Plenty of free space remaining.

If the $mbytes variable were equal to 20, it would skip the code inside the braces and just print the last line:

Plenty of free space remaining.

Using else and elsif

You can also make the script do something if the "if" condition is not true. If you have just one other option, simply use and else statement after the if statement, like this:

CODE:
  1. if ($mbytes <10)
  2.  {
  3.    print "Not much space for storage";
  4.  }
  5. else
  6.  {
  7.   print "Free space getting limited.";
  8.  }
  9.  
  10. print "Enough free space for now.";

Now, if $mbytes is less than 10, the program gives you:

Not much space for storage
Free space getting limited.

However, if $mbytes is anything greater than 10, it will bypass the statement in the first set of braces and execute the code within the else statement braces. It would print this:

Not much space for storage
Enough free space for now

If you want to check for a couple of conditions, you can use elsif to do another check and leave the else code as a last resort. You can have as many elsif statements as you want, but keep them between the if and the else statements. Here is a sample:

CODE:
  1. if (($mbytes <10) && ($mbytes> 0))
  2.  {
  3.    print "There is enough free space";
  4.  }
  5. elsif ($mbytes <= 0)
  6.  {
  7.   print "You need more free space!";
  8.  }
  9. else
  10.  {
  11.   print "Enough free space for now.";
  12.  }
  13.  
  14. print "Free space is getting limited.";

You'll notice the && operator here, it checks to see that two conditions are true before it will return true. So, for the first bit of code to execute, $mbytes must be between 1 and 9. Also, note the extra parentheses, they separate the condition from the comparisons within it.

Now, if $mbytes is between 1 and 9, you get:

There is enough free space
Free space is getting limited.

However, if $mbytes is zero or less, you get:

You need more free space!
Free space is getting limited.

Finally, if $mbytes is 10 or more you get:

Enough free space for now
Freespace is getting limited.

You could check for several ranges here and get smart with the people using the program. Just keep using the elsif statement to check for more ranges.

Using unless

The unless condition checks for a certain condition and executes it every time unless the condition is true. Sort of like the opposite of the if statement:

CODE:
  1. unless ($mbytes == 10)
  2.  {
  3.   print "Exactly 10mbytes free space remaining.";
  4.  }

This shows the message every time unless $mbytes is actually equal to 10. It's a handy shortcut for a longer if/else statement.