Basic Perl Syntax Guide – Control Structures and Conditional Statements

 

Control Structures let you iterate through various types of variables.  There are three main control structures I recommend using in programming cgi scripts: foreach, while, and for.

 

Foreach:

 

We learned before how to define arrays.

 

my @array = (“value1”, “value2”, 1, 2, 3, 4);

 

To “iterate” (aka. Loop) through the values of this array, use foreach like so:

 

foreach my $value (@array)

{

  print $value . “\n”;

}

 

prints out:

 

--

value1

value2

1

2

3

4

--

 

While:

 

while is a very useful control statement that let’s you do something while some particular condition is true.  We will get more into conditional statements later in this chapter.

 

This code will print out “I can’t stop, aaaaah!” forever and ever:

 

while(1)

{

  print “I can\’t stop, aaaaah!”;

}

 

Note: A 1 as the value of an argument means ‘true’ in perl.  See below in conditional statements for more information.

 

The next code will print out a scalar variable that happens to be an integer until it reaches 50.  We increment the variable using the ++ operator described in an earlier chapter.

 

my $integer = 0;

while($integer < 50)

{

  print $integer;

  $integer++;

}

 

The less than sign is part of a conditional statement, which we’ll discuss below

 

for loops;

 

for loops are some of the most advanced and ultimately useful control structures in perl.

 

The most common for loop looks like this:

 

for(my $i = 0; $i++; $i < 10)

{

  print $i;

}

 

This will print:

 

--

1

2

3

4

5

6

7

8

9

10

--

 

There are three variable references in the parentheses used in the for statement.  The first defines our “looping variable”, which we here call $i (it can be any named variable you want).  The second tells the variable what to do on each loop.  Here we increment it by one.  The third reference to our $i variable tells the for loop when to stop executing.  Here we have it stop when we reach the number 10.

 

Conditional Statements:

 

Conditional statements in perl are basically a way of saying, if this is true, then do that.  Or if that is true, do something else.  Or if this is not the case, do something, otherwise, do some different thing.  It is by using conditional statements in perl that we are able to give our programs what is called “logic”, or the ability to determine a particular course of action.

 

if/else

 

The most common conditional statement is the if/else construct.  It simply says ‘if this is true, do this, otherwise, do that.

 

For instance, consider this code:

 

my $number = 95;

if($number eq 95)

{

  print ‘Number is ninety-five!’;

}

else

{

  print ‘Number is not ninety-five’;

}

# this prints “Number is ninety-five”;

 

The ‘eq’ you see above simply means ‘equal to’, and does a comparison of two values.  If they are indeed equal, the code inside the brackets after the “if” is executed.

font-family:Verdana'> 

Here is another example using the opposite of eq, ne, or “not equal”.

 

 

my $number = 10;

if($number ne 95)

{

  print ‘Number is NOT ninety-five!’;

}

else

{

  print ‘Number is ninety-five’;

}

# this prints “Number is NOT ninety-five”;

 

if/elsif/else

 

A variation of if/else is the if/elsif/else construct.

 

Condider this code:

 

my $number = 10;

if($number eq 95)

{

  print ‘Number is ninety-five!’;

}

elsif($number eq 10)

{

  print ‘Number is ten!’;

}

else

{

  print ‘Number is NOT ten OR ninety-five’;

}

# this prints “Number is ten!”;

 

Starting to make sense?

 

The last conditional statement I use in cgi script programming is unless.  Sometimes it’s easier to say what something is not than what it is.

 

Consider this code:

 

my $name = ‘David’;

unless($name eq ‘David’) # note, this IS case-sensitive, ‘david’ will not match

{

  print ‘your name is not that cool’;

}

 

Get the idea?

 

Some important notes are that when you’re doing comparisons in conditional statements, you don’t need to quote scalar variables which are integers, but you DO need to quote scalar variables that are strings.

 

So, this will produce an error:

 

my $name = ‘David’;

if($name eq David)

{

  print ‘I love you!’;

}

 

The reason it produces an error is that you didn’t quote the “David” in the if statement.  You can use single or double quotes in this context, the same rules about interpolation that apply to scalars apply here, as these quoted strings are in fact scalar variables.

 

Other useful comparision operators for use with conditional statements are the “and” operator, which is “&&”:

 

my $name = ‘David’;

my $birth_month = ‘May’;

if($name eq ‘David’ && $birth_month eq ‘May’)

{

  print ‘David was born in may!’;

}

else

{

  print ‘go away’;

}

# prints “David was born in may!”

 

and the or operator, which is “||”.

 

For instance:

 

my $name = ‘David’;

my $birth_month = ‘June’;

if($name eq ‘David’ || $birth_month eq ‘May’)

{

  print ‘This is either David, or the person was born in may!’;

}

else

{

  print ‘go away’;

}

# prints “This is either David, or the person was born in may!”

 

Another useful comparison operator is less than, represented as “<”:

 

my $number = 1;

if($number < 5)

{

  print ‘Less than five!’;

}

else

{

  print ‘Not less than five…’;

}

# prints “Less than five!”

 

Another useful comparison operator is greater than, represented as “>”:

 

my $number = 1;

if($number > 5)

{

  print ‘More than five!’;

}

else

{

  print ‘Not more than five…’;

}

# prints “Not more than five…”

 

You can also use less than or equal to, which is: <=

Or greater than or equal to, which is: >=

 


<-- Previous: Basic Perl Syntax Guide - Variables: scalars, arrays, hashes. | Next: Accessing The File System With Perl -->


Please rate this cgi tutorial on cgi-resources.com:

CGI-Resources Rating:


Copy the Shrug Emoji
Ecommerce Shopping Cart Software
ShopCMS Paypal Shopping Cart
Free CGI Scripts
CGI Tutorial
Software Engineering Consultant
Search Engine Optimization Tips
How To Choose Quality Web Hosting
Free Search Engine Ranking Software
HTTP Compression
Install CGI Scripts
Tell A Friend Script
LittleFTP Free FTP Client For Windows


Link To
This Page!


Copy the following code and paste it into your html file.
Ecommerce Shopping Cart Software | ShopCMS Paypal Shopping Cart | Software Engineering Consultant | Free Search Engine Ranking Software | HTTP Compression | Install CGI Scripts | Search Engine Optimization Tips | CGI Tutorial | CGI Scripts | How To Choose Quality Web Hosting | Tell A Friend Script