Perl, lesson 1
A Perl program is a plain text file that Perl reads from top to bottom, running each
statement in turn. There is no main function to write. Here is a complete program:
The first two lines are not required, but you should start every program with them.
use strict; makes Perl refuse some sloppy code, most importantly variables you never
declared. A typo in a variable name becomes an error instead of a silent bug.use warnings; makes Perl tell you (on stderr) when something looks suspicious, such as
using a value that was never set.Every statement ends with a semicolon. Forgetting one is the most common beginner error.
print writes exactly what you give it and nothing more. It does not add a new line, so
you put \n at the end of the string yourself:
Parentheses around the arguments are optional in Perl. and do the same thing.
2 exercises to try what you just read.
print("hi\n")print "hi\n"Modern Perl also has say, which works like print but adds the new line for you. It is a
feature you switch on. The simplest way is to ask for a recent version of the language at
the top of the file; use v5.36; (or later) turns on say and also strict and warnings:
The two kinds of quotes behave differently:
\n (new line) and \t (tab), and they insert
variables, which you will meet in the next lesson.'a\nb' prints a backslash and an n.A # starts a comment that runs to the end of the line. Perl ignores it.
Perl first compiles the whole file in one quick pass. A syntax error, such as a missing semicolon or quote, stops it before anything runs, and the message gives the line number ("syntax error at main.pl line 3"). If the file compiles, Perl runs it from the top. When a line reports a problem it is often the line before it that is missing a semicolon.
Write a program that prints exactly these two lines:
The program reads no input.
Print this receipt header. Use use v5.36; and say for the first and last lines. The middle
line must be one print that receives three separate strings: "Date:", "\t" and
"2026-11-09\n", so a tab sits between the label and the date.
use strict;
use warnings;
print "Hello, World!\n";print "Loading";
print "...";
print "done\n"; # Loading...done
print "Total: ", 42, "\n"; # print takes a list; pieces are joined with nothing betweenuse v5.36;
say "Hello, World!";
say "No \\n needed.";print "Tab:\tdone\n"; # Tab: done
print 'No escape: \n'; # No escape: \n (and no line break)
print "\n";# This whole line is a comment.
print "Hi\n"; # a comment after codeHello, World!
I am learning Perl.*** CORNER BAKERY ***
Date: 2026-11-09
Thank you!