Getting Started with Perl

Install Perl, run your first program, and learn the habits — use strict, use warnings, and perldoc — that make Perl code reliable from day one.

  • perl
  • beginner
  • guide
  • setup

This guide gets you from nothing to a working Perl setup and your first real program, and points you at the habits that keep Perl code maintainable.

Do you already have Perl?

On macOS and nearly every Linux distribution, Perl is already installed. Check:

perl -v

You’ll see a version banner (Perl 5.x). On Windows, install Strawberry Perl, which bundles Perl and a compiler for CPAN modules.

Your first program

Create a file called hello.pl:

use strict;
use warnings;

print "Hello, world!\n";

Run it:

perl hello.pl

Always use strict and warnings

The two lines at the top of every Perl program you write should be:

use strict;
use warnings;
  • use strict forces you to declare variables and catches a whole class of typos and scoping mistakes.
  • use warnings flags questionable things at runtime — using an undefined value, a number that looks wrong, etc.

They are the single cheapest way to write reliable Perl. Turn them on from your first script and never look back.

Variables in 60 seconds

Perl has three core variable types, each with its own sigil:

my $name   = "Ada";                # scalar: a single value
my @colors = ("red", "green");     # array: an ordered list
my %ages   = (Ada => 36, Bo => 41); # hash: key/value pairs

Reading the built-in docs

Perl ships with excellent documentation. perldoc is your reference:

perldoc perlintro   # a short intro to the language
perldoc perlfunc    # every built-in function
perldoc -f split    # docs for one function
perldoc perlretut   # the regular-expression tutorial

Installing modules from CPAN

CPAN is Perl’s package archive — tens of thousands of ready-made modules. The modern client is cpanm:

cpanm Path::Tiny

Then use it in your code:

use Path::Tiny;
my $content = path("notes.txt")->slurp;

Where to go next

Work through the Perl Tutorial for Beginners to learn the language properly, then Writing Your First Perl Script to build something you’ll actually run.