This tutorial uses examples which have been borrowed from the online manual for perl provided by Larry Wall (who is the creator of Perl). There are many online tutorials for Perl, which you can easily find using any of the common search engines such as Yahoo, Excite, etc.
The main purpose of the examples in this tutorial is to show you how easy it is begin using perl. While you can run perl on any platform (including Windows, Windows NT, unix) we shall mostly use it on unix, since we shall eventually "use" sybperl, a library which allows us to connect to Sybase DB system (also ona unix server at CCST) using perl.
Easy: perl is already loaded on all CCST maintained unix accounts. When you log into your account, you can run perl by typing the following (% is the prompt):
% perl
perl will then wait for you to type a perl command, such as: print( "hello, world"); followed by an EOF character (usually a CONTROL-D).
A more common way is to first edit a perl file, which contains your perl program, and then executing it using perl:
This is an example perl file, example1.pl:
#!/usr/local/bin/perl
$inputline = <STDIN>;
print ($inputline, $inputline);
and now you can run this example using:
% perl example1.pl
To all of you who know C, or C++, the programis easy to understand. It waits for you to type a line of input, and prints your input back to the screen, twice.
#!/usr/local/bin/perl
$inputline = <STDIN>;
print ($inputline, $inputline);
NOTES: Reads a line of input from terminal, and prints it back out two times.
Example2.pl
#!/usr/local/bin/perl
$inputline = <STDIN>;
print( $inputline );
$inputline = <STDIN>;
print( $inputline );
NOTES:
Reads a line of input, prints it out. Reads another line, and prints that one
out.
Notice how any variable names start with "$". Variables can be strings, or any
type of numbers.
Example3.pl
#!/usr/local/bin/perl
print ("Enter the dividend (number to divide):\n");
$dividend = <STDIN>;
chop ($dividend);
print ("Enter the divisor (number to divide by):\n");
$divisor = <STDIN>;
chop ($divisor);
if ($divisor == 0) {
print ("Error: can't divide by zero!\n");
} elsif ($dividend == 0) {
$result = $dividend;
} elsif ($divisor == 1) {
$result = $dividend;
} else {
$result = $dividend / $divisor;
}
if ($divisor == 0) {
# skip the print, since we detected an error
} else {
print ("The result is ", $result, "\n");
}
NOTES:
Divides one number (input via terminal) by another (input via terminal).
notice the use of "if .. elseif .. else" is almost identical to C, C++.
chop is a function provided by perl which removes the last character in a
string (in our case, a NEWLINE, or ENTER character).
Example4.pl
#!/usr/local/bin/perl
$count = 1;
$done = 0;
while ($done == 0) {
print ($count, "\n");
if ($count == 10) {
$done = 1;
}
$count = $count + 1;
}
NOTES:
This example introduces a common iterative structure: while (condition){ ...}
Example5.pl
#!/usr/local/bin/perl
$count = 1;
$number = 0.1;
until ($count == 10) {
print ("$number\n");
$number = $number + 0.1;
$count = $count + 1;
}
NOTES: Another iterative structure: until ( condition) {... }
Example6.pl
#!/usr/local/bin/perl
$inputline = <STDIN>;
chop ($inputline);
if ($inputline == 0) {
print ("0\n");
} else {
print ("1\n");
}
NOTES:
Notice how a NULL string is discovered by the condition check in the "if.."
Example7.pl
#!/usr/local/bin/perl
print("Enter the integer to be divided:\n");
$dividend = <STDIN>;
print("Enter the integer to divide by:\n");
$divisor = <STDIN>;
# check for division by zero
if ($divisor == 0) {
print("error: can't divide by zero\n");
} else {
$quotient = $dividend / $divisor;
$remainder = $dividend % $divisor;
print("The result is $quotient\n");
print("The remainder is $remainder\n");
}
NOTES:
A minor variation of example 3.
Example8.pl
#!/usr/local/bin/perl
$value = 0;
while (++$value <= 5) {
print("value is now $value\n");
}
print("all done\n");
NOTES:
Here we see more C++ like syntax in perl, for example the use of the ++
operator to increment the variable $value.
Example9.pl
#!/usr/local/bin/perl
$value = 0;
while ($value++ <= 5) {
print("value is now $value\n");
}
print("all done\n");
NOTES:
Notice behaviour of $x++ as opposed to ++$x, for a variable $x.
Example10.pl
#!/usr/local/bin/perl
print ("Enter the secret password:\n");
$password = "bluejays";
$inputline = <STDIN>;
chop ($inputline);
$outputline = $inputline eq $password ?
"Yes, that is the correct password!\n" :
"No, that is not the correct password.\n";
print ($outputline);
NOTES: Not much use, this function. But wouldn't it be nice if you could find a way such that the password is NOT printed out as you type it ?
1. For each of the examples above, type in the entire perl code nto files,
and execute the perl program. Try to input the expected inputs, and also
try to see what happens when "wrong" inputs are given to the programs.
Note: You should actually type the code, not cut-and-paste,
for the examples.
2. Write a perl program to calculate the area of a traingle. The program asks
the user to input the length of the three sides, and outputs the area. [Hint:
you will need the perl function sqrt( EXPRESSION)].
3. Modify Example10 above, as follows: The program will ask the user to enter a password. It will then ask the user to re-enter the password. If the two entries are the same, it will output "Password matches!", otherwise, it will output "Sorry, try again!" and loop back.