Materials marked as "optional" can be skipped.
The first two weeks contain more materials than the later weeks,
because I am assuming that you are already familiar with basic programming
concepts from other programming languages.
Starting from week 4, you will be working with HTML. If you have
never manually edited HTML pages, I would recommend that you familiarize
yourself with HTML tags before week 4. See the resources for week 1
on the module webpage.
1 A first example "Hello World"
# This program says "hello" to the world
print "Hello World!\n";
Telnet to a Unix computer. Copy the blue text from the screen
and paste it into a file. Save the file, for example, under the
name "hello". Then type at the Unix prompt:
|# ||indicates a comment; the line is ignored
|print || a command; prints the string between the quotes
|\n|| newline character
|; || indicates the end of a command
1) Add a second print statement to the script from the example.
(For example print "How are you?".)
What does adding or deleting "\n" change?
2) Make some random changes, for example, delete the "#" before
"This program says ..."; delete the semicolon after "World\n"; etc.
In most cases you will get an error message. The only useful piece
of information in that message is probably the number of the line in
which the error occurred. Fix the errors until the script executes again
3) Type "perl -v" at the command line. It gives you information
about the specific version of Perl that is installed on the computer.
Type "perldoc perl" at the command line. To exit "perldoc perl" type "q".
Perldoc provides on-line help pages.
(Don't try to read the documentation right now - it's too technical. Just
keep in mind that it's there in case you need it later in the semester.)
If perldoc is not installed on a machine, try "man perl".
2 A second example
# A program for greeting people
print "What is your name? ";
$name = <STDIN> ;
print "Hello, $name! How are you?\n";
|$name || a variable for a "name"
| = || an assignment operator
| <STDIN> || reads from standard input (i.e. keyboard)
| chomp || deletes the newline character ("enter" key)
4) What happens if you delete the line with "chomp"?
5) Write a program that
asks two people for their names; stores the names in variables
called $name1 and $name2; says hello to both of them.
3 Operators for Numbers
$a = 3 -4 +10;
$b = 5*6;
$c = 7/8;
print "These are the values: $a $b $c \n";
print "Increment $a by one: ";
print "Decrease $a by one: ";
6) Execute the script. Make sure you understand every line.
7) Write a script that asks a user for a number. The script
adds 3 to that number. Then multiplies the result by 2, subtracts 4,
subtracts twice the original number, adds 3, then prints
# This program converts from US $ to Canadian $
print "Money value in US \$ ";
$us_money = <STDIN> ;
$can_money = $us_money /0.6;
print "US\$ $us_money = Canadian \$ $can_money\n";
8) In analogy to the example, write a script that
asks users for the temperature in F
and prints the temperature in C. (Conversion:
Celsius = (F - 32) * 5/9 )
A string is delimited by double quotes (""). Certain special characters
can be used, such as "\n" and "\t". Variables are interpreted, i.e.
'print "$name"' prints the value of '$name' and not $-sign followed by
'name'. To print the characters $, ", \, @, they must be preceded
by a backslash (\).
print "She said: \"hello\"\n";
print "\tThis is indented\n";
$a = "apples"; $b = "pears";
print "$a and $b\n";
9) Write Perl print statements for
the following text (with the same linebreaks, etc):
Snoopy bought a CD called "Greatest Hits" on the WWW for
$10.00. To buy the CD he had to send an email message to
email@example.com. This is what the design on the CD
cover looked like:
\ | /
4.2 Operators and functions for strings
|chop $a; ||# removes the last character
|chomp $a; ||
# removes the last character if it is a newline character
|$a = $b . $c; ||# concatenate $b and $c
|$a = $b x $c; ||# $b repeated $c times
# program that demonstrates operating with strings
print "Please, type your favorite color: ";
$color = $color . "***";
chop $color; chop $color;
$color = $color . "!";
$color = $color x 5;
(If you are unsure what this program does,
add 'print "$color\n";' after every line so that you can see how
the content of the variable is changed at each step.)
10) Write a program that asks users for their favorite
color. Create the following output (assuming "red" is the chosen
red red red red red red red red red red
red red red red red red red red red red
Hints: For the first and last lines: concatenate the string with
"." and use the x-operator. For the second and third line use
"\t". Don't worry about the length of different color words. It
is not necessary to draw the square exactly.