Perl Conditionals

While Perl truly is the programming language of the slacker, the bored, and the (okay I’ll say it with great lamentation) creative, it is also a harsh mistress. But let’s face it: as a computer programmer, you are luck to have a mistress at all. So chin up. Perl doesn’t just freely give you love. There are conditions, or, more precisely, conditionals, which is the topic of this fourth part in a series.

Sure, in the beginning, when you are learning syntax and how to print My cat is named Darth Fluffymittens, Perl is nice and easy, ready to go anytime you are. But as soon as you get to the part about Conditionals…bam! Marriage over.

Maybe I have written too many articles on conditionals; if there is a programming language, you can rest assured I told someone how to write conditionals in it. Maybe I am jaded. Perl conditionals, I have to admit though, are very simple once you grasp the concept.

So put on your safety gloves and let’s get to grasping.

Conditional Lovin’

Conditionals are statements written in code that ask a question, and then respond accordingly to the answer. A good way to think of a conditional is to think about yourself when a bill is overdue. Yeah you slacker, I know all about your late bills.

In pseudo code (or fake code), the situation would look like this:


If


I pay bill


Then lights stay on


End

The above is an example of an If-statement. Simply put, do this, and this happens. Now let’s see the same situation in real code:


#!/usr/bin/perl


$your_money = 0;


if ($your_money < 1) #asks if your money is less than 1


{

print “You are broke!”; #if it is, print “You are broke”

}


print “Your lights might be shut off!”;

The above code asks if you have less than a dollar. If the answer is yes (which it is), then the program prints: “You are broke!”. The last line: “Your lights might be shut off!” prints regardless of the conditional, as it is outside of the statement.

If the value of $your_money was greater than 1, the program would have skipped the “You are broke!” line but still printed the “Your lights might be shut off!” sentence.

{mospagebreak title=You Better or Else!}

Part of being a rich, illustrious writer is bossing people around. Many times throughout the day I will berate the employees around me, sending them scurrying for cover. [Well, you can dream. --Ed.] When they don’t do what I like or want, I give them an If Statement with an Else clause.


#!/usr/bin/perl


$do_my_bidding = ‘Yes Master!';


if ($do_my_bidding = ‘Yes Master!’


{

print “You are a good slave. Tell your mother I will see her tonight”;

}

else

{

print “You ignorant buffoon! Tell your mother I will see her on my lunch break!”;


print “Get out of my sight!”;

In the above code, if $do_my_bidding holds the value, “Yes Master!” you will get the following result:

  You are a good slave. Tell your mother I will see her tonight.

  Get out of my sight!

If the value of $do_my_bidding held any other value, you would see this:

  You ignorant buffoon! Tell your mother I will see her on my lunch break!

  Get out of my sight!

No matter which condition is met, the program will always print: Get out of my sight!, as it is not part of either conditional.

Elsif

Sometimes one Else clause is not enough. That’s where Elseif comes in handy. Observe!


#!/usr/bin/perl


$do_my_bidding = ‘Yes Master!';

{

print “You are a good slave. Tell your mother I will see her tonight”;

}

elsif ($do_my_bidding = ‘No’)

print “Insolent swine! Tell your mother to come right now!”;

else

{

print “You ignorant buffoon! Tell your mother I will see her on my lunch break!”;


print “Get out of my sight!”;

Again, with the above code, if the value of $do_my_bidding is “Yes Master!” it will result in:

  You are a good slave. Tell your mother I will see her tonight.

  Get out of my sight!

If the value of $do_my_bidding is “No” the following would print to your screen:

  Insolent swine! Tell your mother to come right now!

  Get out of my sight!

And finally, if the value of $do_my_bidding is anything else, it will print:

  You ignorant buffoon! Tell your mother I will see her on my lunch break!

  Get out of my sight!

You will note two things: 1) The phrase “Get out of my sight!” is always printed as it is outside of the statements. And 2) No matter what you say, I am still going to spend some time with your mother. It’s simply a matter of when.

{mospagebreak title=The Unless Statement}

The Unless Statement is the opposite of the If statement in that it asks if a condition is false, and executes the code unless the condition is true.


#!/usr/bin/perl


$my_iq = 1000;

$your_iq = 90;


unless ($your_iq > $my_iq)

{

print “My brain is enormous and far superior to your own!”;

}

In the above code, the program will print out the sentence: “My brain is enormous and far superior to your own!” every time, unless $you_iq is greater than $my_iq. 

Getting Loopy

Near the end of the day I always get a little loopy; I can’t think straight and words just come out wrong. But I’m getting off topic here. Loops in code are something completely different.

The purpose of a loop is to loop a piece of code so that you don’t have to type it over a billion times. Let’s take a look at the For Loop:


#!/usr/bin/perl


for ($count =1; $count<10; $count++)

{

print “I R THE GREATEST!n”;

}

The above code created a variable name $count and assigned an initial value of 1. It also has a part that tells it to increment the value of $count by +1 ($greatness++) and to do so until the value of $count is 10 or greater ($count<10). So long as the value of $count is less than 10, the following will print to the screen:

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

If we were working with numbers we could have also just assigned a numeric value to $count and have it do this:


#!/usr/bin/perl


for ($count =1; $count<10; $count++)

{

print “$countn”;

}

That would print out the following to your screen:

  1

  2

  3

  4

  5

  6

  7

  8

  9

The For Loop loops for the number of times you tell it to.

{mospagebreak title=The While Loop}

The While Loop loops until a condition you give it is false.


#!/usr/bin/perl


$count= 1


while ($count<10)

{

print “$countn”;

$count++;

}

The above code will, like the code before it, print out the numbers 1 through 9, stopping once the value becomes equal to ten and the statement is false.

Foreach There is Another!

Our final Loop is known as the Foreach Loop and is used mostly with arrays.


#!/usr/bin/perl


@breakfast = (“Sausage”, “Eggs”, “Bacon”, “Grits”, “Toast” “Hash”);


print “My breakfast:nn”;


foreach $breakfast (@breakfast)

{

print “$breakfastn”;

}

This code access the values in your array and prints them one by one:

  Sausage

  Eggs

  Bacon

  Grits

  Toast

  Hash

And that is it for Loops and Conditionals. In my next tutorial, I will go over a few more statements, and we will learn to work with files — creating them, reading them, writing them, you name it.

So go forth, young nerdling, and use your Perl wisdom for great deeds. Till then…

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan