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"
#!/usr/local/bin/perl -w
#
# 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:
perl hello
1.1 Explanations
# | 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.2 Exercises
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
successfully.
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
#!/usr/local/bin/perl -w
#
# A program for greeting people
#
print "What is your name? ";
$name = <STDIN> ;
chomp ($name);
print "Hello, $name! How are you?\n";
2.1 Explanations
$name | a variable for a "name"
|
= | an assignment operator
|
<STDIN> | reads from standard input (i.e. keyboard)
|
chomp | deletes the newline character ("enter" key)
|
2.2 Exercises
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: ";
$a++;
print $a;
print "\n";
print "Decrease $a by one: ";
$a--;
print $a;
print "\n";
3.1 Exercises
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
the result.
3.2 Example
#!/usr/local/bin/perl -w
#
# This program converts from US $ to Canadian $
#
print "Money value in US \$ ";
$us_money = <STDIN> ;
chomp $us_money;
$can_money = $us_money /0.6;
print "US\$ $us_money = Canadian \$ $can_money\n";
3.3 Exercises
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 )
4 Strings
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 "hello\n";
print "hello\\n";
print "\$5.00\n";
print "She said: \"hello\"\n";
print "\tThis is indented\n";
$a = "apples"; $b = "pears";
print "$a and $b\n";
4.1 Exercises
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
orders@hits-online.net. 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 |
4.3 Example
#!/usr/local/bin/perl -w
#
# program that demonstrates operating with strings
#
print "Please, type your favorite color: ";
$color= <STDIN>;
chomp $color;
$color = $color . "***";
chop $color; chop $color;
$color = $color . "!";
$color = $color x 5;
print "$color\n";
(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.)
4.4 Exercises
10) Write a program that asks users for their favorite
color. Create the following output (assuming "red" is the chosen
color).
red red red red 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.