NOTE:
The extent of loop content in this tute is mostly limited to a couple of
examples, the next primer covers loops in much more detail.
Perl relational/comparision operators.
Firstly I want to cover relational (or comparison) operators in more
detail..
Relational operators are exactly what they are called, put simplistically
they are used to test the relationship between two objects, be they numbers,
strings or pretty much anything else.
You saw one in the last primer, namely: "eq".
If you just want to set the value of a variable, you use just a single
equals symbol. like so:
$a_string_variable = 'a_string of characters';
$a_numeric_variable = 1;
If you want to test or compare objects, you need relational operators.
Here is a small table of relational operators.
Numeric: String: Means: -------------------------------- == eq is equal to. != ne is not equal to. > gt is greater than. < lt is less than. >= ge is greater than or equal to. <= le is less than or equal to.
There are others, but these are the ones you will use in the vast majority of
cases.
So if you are comparing numbers or numeric variables, you'd use the numeric
comparison operators and if you were comparing strings,
you'd use the string comparison operators.
Here is a new if statement that uses numeric testing or relational
operators.
# Pick a number between 1 and 10.
my $number = 6; if ($number <= 5) { print 'your number is less then or equal to 5'; } elsif ($number >= 6) { print 'your number is higher then 5'; } else { print 'don't know what that number is'; }
Just briefly, I'll show you another example of the usage of unless, (which you would be familiar with from the previous tutorial.)
unless ($animal eq 'dog') { print 'sorry there are no dogs here'; }
One last thing I will tell you about if/elsif/else statements.. its VERY
VERY important that when you write one, you ensure that you cover every
possible answer that a user could enter... a BIG cause of bugs in programs
is when a program gets a value set that it wasn't expecting and it crashes,
or worse, opens a security flaw... thats why you should nearly ALWAYS have a
else statement at the end of an if test... to allow for anything that
doesn't match a previous test.
Now for a brief explanation of for/foreach statements.
Say you have an array (list) of values.
my @array_of_animals = ( 'Dog', 'cat', 'horse', 'mouse', 'sheep', 'cow');
and you wanted to add see if any of the items in the list is a sheep.
(important if you are from New Zealand :-)
Here is what you would do:
foreach my $animal (@array_of_animals) { if ($animal eq 'sheep') { print 'WoooHooo, we have a sheep !!!'; } else { print 'sorry, there are no sheep here.'; } # end of else statement. } # end of foreach loop.
Can you guess what we just did? Its quiet clever.. we just pulled each item
out @array_of_animals (one at a time) and assigned it to $animal so we could
test if any of them matched the string 'sheep'.
If we were to read that line in English, it would say something like this:
for each animal in the list of animals, does animal equal sheep??, if so,
then print woohoo we have a sheep !!!
if not then print sorry, we there are no sheep here.
Pretty simple huh???
In the next primer, I'll show you a use for the 'for' statement, (which as
far as perl is concerned means exactly the same thing as foreach, it was
just created so that humans could read it better...)
But here is an example for those that can't wait.. can you guess what it
does???
for ($counter = 0; $counter < 10000; $counter +=1;)
{
# Do stuff here!
}
'while' is another useful one (it does much the same thing as the previous one).
my $counter = 0; while ($counter < 10000;) { # Do some stuff here. # This is the same as saying: $counter = $counter + 1; $counter += 1; }
As you can see, you can do the same thing in many different ways in Perl,
it's one of its great strengths. You just choose the one that makes most
sense to you and go with that.
(if you guessed that both of the above (for and while) looped 10000 times,
you'd be right.)
############################################
As a last note, I read years back, that someone had written a DVD deCSS
decoder in Perl (very complicated to do), and the code is shorter then
it is in any other language. AND it was fast enough to actually play a DVD
in real time. . so don't let anyone tell you Perl can't be fast.
############################################
So if you are ready to learn more:
Back to the Tutorial Index