REVISED: Sunday, March 3, 2013
You will learn how to use the Perl "if, else if, and else" statements to write simple logic in your Perl scripts.
I. PERL QUICK REVIEW
Before we introduce something new, we will have a quick review:
A. CALCULATIONS
Perl uses double-precision floating point values for calculations.
B. STRING
Perl defines string as a sequence of characters. The shortest string is the null string it contains no characters. The longest string is only limited by the available memory of your computer.
C. INTERPOLATION
Interpolation of scalar variables is a mechanism to convert a scalar variable inside a string into string.
D. BLOCK
Perl groups statements into blocks of code. A block is surrounded by a pair of ({ }) curly braces and can be nested within a block.
E. LOGICAL COMPARISON
In Perl, everything is true except the number zero 0, empty string "", zero string "0", and undefined (undef) values.
II. PERL "IF" CONTROL STRUCTURE
Decision statements use the "if" keyword to execute a statement block depending on the evaluation of conditional expressions. Therefore, you can use the Perl "if" control structure to write simple logic in your code.
The "if" control structure can be nested.
The syntax of an "if" control structure is as follows:
if(condition){
statements;
}
If the condition is true the statements inside the block will be executed: for example:
$x = 1;
$y = 1;
if($x == $y){
print "$x is equal to $y";
}
The first and second lines define two variables $x and $y with their values initialized to 1.
In the fourth line we use the "if "statement to print a message if $x is equal to $y.
The message is only printed if the expression ($x == $y) is evaluated as true.
III. PERL "IF-ELSE" CONTROL STRUCTURE
If you need an alternative choice, Perl provides an "if-else" control structure as follows:
if(condition){
if-statements;
}
else{
else-statements;
}
If the condition is false the else-statements will be executed; for example:
$x = 2;
$y = 3;
if($x == $y){
print "x is equal to y";
}
else{
print "x is not equal to y";
}
IV. PERL "IF-ELSE-IF" CONTROL STRUCTURE
A. MULTIPLE CHOICES
Perl also provides an "if-else-if" control structure to make multiple choices based on conditions.
if(condition1){
if-statements;
}
else if(condition2){
else if-statements;
}
else if(condition3){
else if-statements;
}
...
else{
else-statements;
}
Here is the source code example:
$x = 2;
$y = 3;
if($x > $y){
print "x is greater than y";
}
else if ($x < $y){
print "x is less than y";
}
else{
print "x is equal to y";
}
B. READABILITY
Perl helps you create a self documenting style of easy to read code.
The following is another way to format your Perl code:
if (condition1) {
task;
} elsif (condition2) {
a different task;
} else {
a task if all else fails;
}
V. PERL SCRIPT SOURCE CODE
Copy paste the following into your text editor.
#Unicode
use utf8;
use warnings;
#enables -w flag.
use diagnostics;
#Helps you catch typos.
#Forces you to use my() function to declare all variables.
use strict;
#stack trace
use Carp ();
local $SIG{__WARN__} = \&Carp::confess;
my($file) = "C:/Strawberry/ifElse.pl";
#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";
my($x) = 2;
my($y) = 3;
if($x == $y){
print "x is equal to y";
}
else{
print "x is not equal to y";
}
#Close the file.
close FILE;
From your text editor, "File Save As" ifElse.pl using the path to the folder of your Perl download.
From the Perl prompt type the following:
C:\WINDOWS\system32>C:\Strawberry\ifElse.pl
Press Enter and the following will display on your screen:
C:\WINDOWS\system32>C:\Strawberry\ifElse.pl
x is not equal to y
C:\WINDOWS\system32>
C:\WINDOWS\system32>C:\Strawberry\ifElse.pl
Press Enter and the following will display on your screen:
C:\WINDOWS\system32>C:\Strawberry\ifElse.pl
x is not equal to y
C:\WINDOWS\system32>
The my( ) function makes the variables local to the main package. Packages are classes.
A running Perl program has a built-in namespace called "main", which is the default name. For example, a subroutine called Sub1 can be called as Sub1( ) or main::Sub1( ).
With a variable the appropriate sigil is placed in front of the namespace; so a scalar variable called $var1 can also be referred to as $main::var1, or even $::var1. Other namespaces can be created at any time.
With a variable the appropriate sigil is placed in front of the namespace; so a scalar variable called $var1 can also be referred to as $main::var1, or even $::var1. Other namespaces can be created at any time.
Now you have a "learning feed-back loop." Change the values of $x and $y. Think about what you expect Perl will do. Then run the program and see if Perl does what you expect.
If pearl does not do what you expect, look at your code and make the appropriate changes, then rerun the program.
You have learned how to use the Perl "if, else if, and else" statements to write simple logic in your Perl scripts.
Elcric Otto Circle
Elcric Otto Circle
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment