Learning Perl, ed. 4:
Chapter 1; p. 11 bottom - 15
Chapter 2; p. 18-36
Learning Perl, ed. 5:
Chapter 1; p. 12-15
Chapter 2; p. 19-38 top
Learning Perl, ed. 6:
Chapter 1; p. 13-17
Chapter 2; p. 21-40
Variables (numbers, strings and booleans), assignment (giving values to
variables ), some operators (+-*/.), the print statement (making data
appear on the screen), conditional statements (if statement, which
executes some statements, if certain conditions are true), the while
loop (looping over statements as long as certain conditions are true),
how to give input to the program from the keyboard, the chomp
command, which removes newlines in the end of strings.
Data and program code are separate entities, which go together.
However, data can not be executed as code, and
code is not typically thought of as data.
A program can only be executed, when it has execute permission:
chmod 755 <filename>
This course is very focused on teaching how to make nice, explicitly written
Perl code, which is easy to read by others. Therefore some of Perl's ideoms
are avoided, because they often lead to trashy, hard to understand code.
To be very concrete, use of the default variable, $_, and the diamond
operator, <>, is strongly discouraged, even if it is used in the text books.
Necessary files to complete this exercise
To download the files to your system, just press the Shift key while
you left click on the blue link. Follow the instructions.
Remember to write #!/usr/bin/perl on the first line of
All the following exercises have to be done in Perl
- Write 'Hello World' on the screen.
- Write 'Hello World' 10 times using a loop. One Hello per line.
- Write numbers 1 to 10 to the screen one number per line. Use a loop.
- Make a program ask for a name, and then write a greeting
using that name. However, if it is your name you give as input
the greeting should be extra nice.
- Make a program that ask for two numbers (one at a time) and then
prints them and their sum.
- Ask for two numbers and ask what operation to perform on them
(+, - , *, /) and display the numbers and the result.
- Ask for two integers and print them and all integers between them.
It is not necessary to perform input control - just assume that the user is
well-behaved and inputs integers.
- Now make the same program work even if you switch the input numbers,
so it does not matter if you input the smallest number first or last.
- This needs to be read carefully: Make a program that asks for number, and then continues asking for numbers as long as you
input numbers that are greater or equal to all previous numbers (not the sum of previous numbers).
- Ask for a positive integer and calculate the factorial (n!) of that number.
Display the result. If input is negative, display an error message.
- If you solved the previous one then this should be relatively easy.
Ask for an integer and calculate the sum from 0 up/down to the integer.
An eksample: If you input 5 then calculate 5 + 4 + 3 + 2 + 1 (+ 0) = 15.
If input is -4 then calculate -4 + -3 + -2 + -1 (+ 0) = -10.