Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 43 ยท Getting Started

Your First Perl Program

Write and run your first Perl program: print and say, comments, statements and semicolons, quotes and escapes, plus the errors beginners meet first.

Every language starts with a program that prints a message, and Perl's is only two lines long. In this lesson you will write that program, learn what each part means, and see the rules that every Perl statement follows: semicolons, comments, quotes and escape sequences.

Perl does not run on this site, so the Perl code below is read-only, with its real output printed under each block. The Bash and Python TryIts are stand-ins for the run flow and the ideas. Paste the Perl into a file on your own machine (see Installing and Running Perl) to run it for real.

Hello, Perl

Save this as hello.pl:

PERL
use v5.36;

say "Hello, Perl!";

Run it with perl hello.pl:

TEXT
Hello, Perl!

There are two statements here.

  • use v5.36; tells Perl that your program is written for Perl 5.36 or newer. That one line switches on several modern features at once: the say function, use strict, use warnings and subroutine signatures. The lesson use strict and use warnings explains why that matters.
  • say "Hello, Perl!"; prints the text and then a newline.

Everything between double quotes is a string. Perl reads the program from top to bottom and runs each statement in order.

Statements and semicolons

A Perl statement is one instruction, and it ends with a semicolon. Whitespace does not matter, so a statement can span lines or share a line with another:

PERL
use v5.36;

say "one"; say "two";
say
    "three";
TEXT
one
two
three

The semicolon is the most common thing beginners forget. If you leave one out, Perl runs two lines together and complains about the second one:

PERL
use v5.36;
say "one"
say "two";
TEXT
syntax error at e.pl line 3, near "say"
Execution of e.pl aborted due to compilation errors.

The error points at line 3, the line after the real mistake. When Perl reports a syntax error, look at the previous line too. The last statement in a block may omit its semicolon, but always writing one is a good habit.

Perl has two ways to output text, and they differ by one thing:

  • print writes exactly what you give it.
  • say writes what you give it, plus a newline.

say needs use v5.10 or newer (or use feature 'say'), and use v5.36; includes it. With print, you add the newline yourself with the escape sequence \n:

PERL
use v5.36;

print "No newline here";
print "\n";
print "Name: ", "Ada", "\n";
say "Sum: ", 2 + 3;
TEXT
No newline here
Name: Ada
Sum: 5

Both print and say accept a list of values separated by commas and print them one after another with nothing between them. That is why print "Name: ", "Ada", "\n" produces one tidy line.

Python Stand-in: print vs say (Python)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The stand-in prints the same four lines as the Perl version above. One difference: Python's print puts a space between its arguments by default, while Perl's print and say put nothing there unless you ask for it.

Comments

A # starts a comment: everything from the # to the end of the line is ignored by Perl. Comments explain why the code does something:

PERL
use v5.36;

# Greet the user
say "Hello!";   # a comment can follow code

Perl has no block comment symbol. To disable several lines, put a # in front of each (most editors do this for you). For longer documentation, Perl uses POD (plain old documentation), blocks that begin with a line like =pod and end with =cut. You will see POD in modules, and perldoc can display it.

Quotes and escape sequences

Perl treats double-quoted and single-quoted strings differently, and this is one of the first rules to learn:

  • Inside double quotes, Perl interpolates variables and understands escape sequences such as \n (newline) and \t (tab).
  • Inside single quotes, almost everything is literal. Only \\ and \' are special.
PERL
use v5.36;

say "double:\ttab";
say 'single: $x\n';
say "He said \"hi\"";
say q(It's easy);
TEXT
double:	tab
single: $x\n
He said "hi"
It's easy

The last line uses q(), an alternative quote form that works like single quotes but lets you pick other delimiters, so you do not need to escape the apostrophe. Scalar Variables covers quoting and interpolation in detail.

Printing lists and loops

You can print a list of items, and say can loop over them if you use a statement modifier:

PERL
use v5.36;

say for 1 .. 3;
my @letters = ('a', 'b', 'c');
say "@letters";
say @letters;
TEXT
1
2
3
a b c
abc

Inside double quotes, an array is joined with spaces. Passed directly to say, its items are printed with nothing in between. Do not worry about the details yet; arrays get their own lesson in Arrays.

A common surprise: precedence

Operators bind in a fixed order, and sometimes the result is not what you expect. The dot . joins strings, and + adds numbers, and + binds as tightly as .:

PERL
use v5.36;

say "Sum: ", 2 + 3;      # prints: Sum: 5
say "Sum: " . 2 + 3;     # prints: 3

The second line is read as ("Sum: " . 2) + 3. The string "Sum: 2" is turned into a number, which gives 0, and 0 + 3 is 3. With warnings on, which use v5.36 does for you, Perl also prints Argument "Sum: 2" isn't numeric in addition (+). When in doubt, use commas or parentheses.

The run flow, step by step

Whatever the program, running a script always follows the same steps: create the file, call the interpreter, read the output and the exit status. The Bash stand-in below shows that flow with Bash in place of Perl.

Bash Stand-in: write a file, run it, read the exit status (Bash)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In Perl the same checks apply: perl hello.pl runs the file, and $? in the shell tells you whether it succeeded. A script that stops with a syntax error, like the one earlier, gives a non-zero status and prints no normal output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between print and say?

    say is print with an automatic newline at the end. It needs use v5.10 or newer, or use feature 'say'.
  2. 2. What does Perl print for the statement: say 'Total: $amount\n';

    Single quotes do not interpolate variables or understand escape sequences, so the text is printed literally.
  3. 3. Why does Perl often report a syntax error on the line after a forgotten semicolon?

    Without the semicolon, Perl keeps reading and only fails when the next token does not fit, so the reported line is usually the one after the mistake.
  4. 4. Which character starts a comment in Perl?

    # begins a comment that runs to the end of the line. Perl has no /* */ block comments.
  5. 5. What does "use v5.36;" do?

    It requires at least that Perl version and turns on features such as say, strict, warnings and signatures.

Summary

  • A Perl program is a list of statements, each ending with a semicolon; use v5.36; at the top enables modern features.
  • say prints and adds a newline; print prints exactly what you give it.
  • # starts a comment; documentation blocks use POD.
  • Double quotes interpolate variables and understand \n and \t; single quotes are literal.
  • A missing semicolon is reported on the next line, and "Sum: " . 2 + 3 shows why precedence needs care.

Next: use strict and use warnings.