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 to 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 on a 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 End-Of-File, or EOF character. In Unix, this is the CTRL-D
character (keep the Ctrl key pressed and hit '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
NOTES:
To all of you who know C, or C++, the program is easy to understand. It waits
for you to type a line of input, and prints your input back to the screen,
twice.
Unlike C++, you do not have to declare variable types. All variable names
begin with a '$' character. A vraiable can be number, or a character string.
Your program is waiting to read an input from the keyboard into a
variable called '$inputline'. The keyboard is the STanDard INput device,
specified by <STDIN>.
#!/usr/local/bin/perl $inputline = <STDIN>; print ($inputline, $inputline);
NOTES:
Reads a line of input from terminal, and prints it back out two times.
IMPORTANT: Every perl program should begin with the line:
#!/usr/local/bin/perl
This MUST be the first line of your program, and MUST begin at the first
character -- no spaces !
We shall find the importance of this line when we write cgi's.
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) {
# do nothing: error message has been printed !
} 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++.
Note that when you type something from the keyboard, all characters you types
will be read as a string -- including the 'ENTER' key. Thus, if you input
the number 12 as your divisor, the variable $divisor initially has the value
equal to the string: "12ENTER". You want to remove the ENTER-key from this
variable to treat it as a number.
chop is a function provided by perl which removes the last character
in a string (in our case, a NEWLINE, or ENTER character).
Also notice how the print function can take any number of inputs, which can be strings, variables. The "\n" in the print command prints a NEWLINE character to the screen, just like in C++.
Except for the first line of the program, all characters following the '#' sign in a line are considered by perl to be comments.
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:
This example is just a minor variation of example 3.
Note: If both, divisor and dividend are integers, perl performs INTEGER
division (the C++ div operation); if any of them is real, then REAL NUMBER
divsion is performed. The '%' is the modulus operator.
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 ?
We also meet the common C++ operator
TEST ? IF-TRUE-DO-THIS : IF-FALSE-DO-THIS;
This operator checks whether the TEST is true; if true, it returns the value
of the statement following the '?'; if TEST is false, then it returns the
value of the statement following the ':'. We can set a variable (e.g.
$outputline) equal to this value.
We can test if two NUMBERS are equal using the '==' operator. We test if two strings are equal using the 'eq' operator.
1. For each of the examples above, type in the entire perl code into 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 Example 10 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.