Perl from the ground up

Your first Perl

What Perl is, how to print a line, why every file here starts with two strictness lines, and the text every program after this one is built from.

Part 1 of 12

Beginner14 min readBack to Courses

A language built for text

Perl started as a tool for reading text and reporting on it: the first version was written to summarise a mailing list. That job left its mark. Reading a line, cutting it into pieces and printing something tidy are short jobs in Perl, which is why it spent thirty years as the language of system logs and reports.

The sandbox here runs a real interpreter, the same one that runs on a server, and it keeps your program in a single file called main.pl. Nothing is compiled: the file is read and run from the first line to the last. There is nowhere for a program to read input from here, so every lesson prints rather than asks.

Two lines at the top of every file

Every example in this course starts with use strict; and use warnings;. They are not decoration. Strict mode refuses to let an undeclared variable quietly become nothing, and warnings tell you when a value is not the kind of thing the code assumed it was.

Older Perl was famous for guessing, and the guesses were silent. Those two lines turn a whole class of silent mistakes into loud ones, and they cost nothing to write, so every serious program begins with them.

Printing a line

print writes its arguments to the output in order, with nothing between them. A line of output is therefore usually a piece of text, then a value, then a "\n" to end the line: print "total ", $total, "\n";. The "\n" is the newline, and print does not add one for you — forgetting it is the first mistake everybody makes.

printf is the careful one. Its first argument is a pattern and the rest fill it in: %d means a whole number and %.2f means one with two decimal places. When a lesson needs output you can predict character for character, it reaches for printf.

What the interpreter does before it runs a line

Perl does not produce a separate executable, but it still reads the whole file before it executes any of it. The first pass checks the syntax and turns the program into an internal form, so a missing semicolon near the bottom stops the program before the first print at the top has written anything.

That is why an error can point at the end of the file when the mistake is a line or two above, and why a program that printed nothing may have a syntax error rather than a runtime one. Once the whole file is understood, execution starts at the first statement and runs down the page.

A print statement is one list

print is a list operator. Everything between it and the semicolon is a single list, and the commas separate one item from the next; print writes the items in order and adds nothing between them. print "total ", $total, "\n"; is three items, and the spaces and newline are yours to place.

printf takes the same list but treats the first item as a pattern and the rest as values to drop into it. That is the difference to keep in mind: print writes exactly what you give it, while printf interprets the pattern you give it.

One line, taken apart

Read my $name = 'Ada'; from left to right: my declares a name for the rest of the block, $name is the name, = stores the value, and the semicolon ends the statement. The dollar sign is not part of the name; it is the sigil that says one value, and a list uses a different one. A missing semicolon is a compile error, and because the whole file is read first, the symptom is an empty output pane.

A comment starts with a hash and runs to the end of the line, so the examples can explain themselves without changing what they print. Blank lines are ignored, which is why a long program is easier to scan with a few of them.

Printing, and a first sum

use strict;
use warnings;

print "Hello, Perl!\n";

# print takes a list of things to write.
print 'Two plus three is ', 2 + 3, "\n";

# A comment starts with a hash and runs to the line end.
my $name = 'Ada';
print "Hello, $name!\n";

# printf fills in a pattern.
printf "%.2f\n", 10 / 3;
printf "%s scored %d\n", $name, 42;

Run it, then make it yours

Press Run and read the output line by line. Then change the name, add a line that prints a sum of your own, and try single quotes around the same text.

Output
Click "Run" to execute your code

When a line prints something you did not expect, print its pieces one at a time instead of the whole sentence. Reading half a line is the fastest way to find which value was wrong.

Three mistakes are worth recognising on sight. A print without "\n" leaves the next line glued to it. A missing semicolon is caught before the program runs, so the output pane stays empty. And single quotes do not fill in variables, so 'Hello, $name' prints the characters $name rather than the name.

Try these next

  • Print a three-line address block, and end every line with "\n" so the lines do not run together.
  • Use printf to print a name in twelve columns and a price in six, then change one width and watch the padding move.
  • Put the same variable in double quotes and in single quotes, and write one sentence explaining the difference.

A receipt with columns

Run it, then add a third item and recompute the total. Change the width in `%-12s` and watch every row move together.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What does print add after the values you give it?