Learn/Perl/1. Hello, output
Lesson 1 of 8
Sign in

Perl, lesson 1

Hello, output

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 safety net

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 and the new line

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.

Exercise 1

Your first program

Press Ctrl Enter or Check to test your code.

Exercise 2

Receipt header

Press Ctrl Enter or Check to test your code.

print("hi\n")
print "hi\n"

say

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:

Double and single quotes

The two kinds of quotes behave differently:

  • Double quotes process escapes like \n (new line) and \t (tab), and they insert variables, which you will meet in the next lesson.
  • Single quotes keep the text exactly as written. 'a\nb' prints a backslash and an n.

Comments

A # starts a comment that runs to the end of the line. Perl ignores it.

How a Perl program runs

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.

Further reading

  • perlintro: Basic syntax overview
  • perlintro: Safety net
  • perlfunc: say

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 between
use 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 code
Hello, World!
I am learning Perl.
*** CORNER BAKERY ***
Date:	2026-11-09
Thank you!