473,513 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ


Perl Control Statements

By Blair Ireland
Senior Editor, TheScripts.com

Control Statements

Make sure you got your caffeine and cigarettes ready for this lesson though, this is where you start to apply things I've spoken about.

Believe me, once you get started, you will have trouble stopping. So make sure all your necessities are nice and close before you start.....

If/Else Statements

If/Else statements determine what the heck your script is going to do. They go in the following syntax;

if (what you are looking for goes here) { # The stuff you want the script to do # *if* the above statement is true...... }

If you want more then one option though, it would look like this....

if (what you are looking for goes here) { # The stuff you want the script to do # *if* the above statement is true...... }
elsif (what you are looking for goes here) {
# The stuff you want the script to do # *if* the above statement is true......
}

As well, just say you want the script to do a bunch of things *if* a group of statements are true, or do something else if they are all false. Then you would add this code to your if/else statements...

else {
# The stuff you want the script to do.
}

Notice there are no (brackets) beside this statement. That is because you aren't looking for something here, only the above statements are.

The last type of these statements is now as the until statement. Until is basically the opposite of the if statement, as it will only do something if something else is untrue. It looks like this;

unless (what you are looking for goes here) {
# what you want the script to do
# if the above statement is not true.
}

Understand? Good. These statements are what drives your script in the first place, so play around with them. Now you can insert some of those operators I taught you last week (I sure hope you remember) within the brackets to determine what you are looking for. Next stop, Loops.

Loops

Loops can be a huge time saver, and fall under the programming category of "command structure". If you've had any experience programming, the whole concept of loops should be familiar to you. If you have no prior experience though, well, it's time to learn!

All loops in perl have the following *general* look

command (statement) {# another group of statements;}

Within a loop, you can include more loops, if/else statements, and whatever else you feel the need to add.

The For Loop

The For loop has the most complicated syntax of any perl loop. You can find it in a lot of other languages though, so some might find it more comfortable to use. It looks like this though;

for ($i=0;$i<10 ;$i++) { print "$i\n";}

Now, within this for loop, you have to declare the variable that will *only* exist within the loop. In this case, I use $i. I am comfortable with $i, I like the letter $i, and I will stick with the letter $i. You can make it whatever you want it to be like though, it's all a matter of taste.

This variable is increasing by 1 every single time the loop is run. At the start though, I also made sure I declared $i as being equal to 0, which of course, can be changed by you as you see fit. Next, I told the for loop that $i must be less then or equal to 10. So now it will only repeat 10 times. The last part within the brackets though is $i++, which increments $i by one each time. My print statement is really basic in this for loop though. All it does is print the current value of $i and a new line each time the for loop is executed. This was just to give you an idea of how to use the $i variable within the loop.

While and Until Loops

While and Until loops both function in a similar fashion as the if/unless statement. It will look at the condition you gave it to look for (in the usual brackets, like this), and if it is true for while, or false for until, it will execute. Kinda cool? Well, sure, I guess. These loops are quite similar to the for loops, but they aren't as self-contained.

Remember though, these loops can be considered dangerous. They run the largest risk of never stopping, as, unlike the for loop, it doesn't rely on any value tests to stop. Basically, the value must be declared outside of the loop, and changed within the loop. This can all get a little tricky, so just give it a couple of tries. The syntax looks like the following;

$number = 0;
while ($number < 10) {
   print "$number\n";
   $number++;

The syntax for until looks like this;

$number = 0;until ($number > 10) { print "$number\n"; $number++;

}

Foreach Loops

These foreach loops are kinda cool/funky, if you think that this kind of thing is cool anyways. Please, no laughing now.

A foreach loop looks like this;

foreach $value (@array) { print "$value";
}

Some people like to call them a lazy version of the for loop, which I can agree with. Anyways, what it will do is take each value of @array, refer to each value as $value, and do what the statements within the loop tell it to do. Each value is processed one at a time. You can rename the reference to each value of the array to whatever you want it to be, just I used the first word that came to mind. They are also great for associative arrays, AKA Hashes. The syntax for that would be;

foreach $value (@array) { print "$value";
}

Similar to what the foreach loop does for normal list arrays, the loop here would grab each key in the Hash, and print the value as I told it to do.

« Perl Operators Perl File Manipulation »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.