Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 43 ยท Getting Started

Installing and Running Perl

Install Perl with Strawberry Perl, your package manager or perlbrew, then run code with perl -e, script files and the shebang line, and read docs with perldoc.

Before you write real programs, you need a working perl command. The good news: you may already have one. Perl ships with most Linux distributions and with macOS, and installing it on Windows takes a few minutes. This lesson shows how to check what you have, how to install a current version, and the different ways to run Perl code.

This site cannot run Perl in your browser, so the Perl commands here are read-only: copy them into your own terminal. The two interactive examples use Bash and Python to demonstrate the same ideas (script files with a shebang line, and syntax checking), and they say so.

Check whether Perl is installed

Open a terminal and ask for the version:

Terminal
perl -v

If Perl is present, you get a few lines that begin like this (your numbers will differ):

TEXT
This is perl 5, version 42, subversion 3 (v5.42.3) built for x86_64-linux

Copyright 1987-2025, Larry Wall

The version number after perl 5, version is the one that matters. Perl 5.36 and newer are a comfortable base for this tutorial, because they let you switch on modern features with a single line. Older versions still run most of the examples, but you will need to add a few lines you will meet later.

Installing Perl

Pick the route for your system

Windows Install Strawberry Perl.
macOS Use the built-in perl, or Homebrew or perlbrew.
Linux Already installed; update with your package manager.
perlbrew Install any version in your home folder.

Never remove the system Perl; the operating system may depend on it.

1 / 2

Windows

The most popular choice is Strawberry Perl, a free distribution that bundles Perl with a C compiler, so you can also install modules from CPAN that need to compile native code. Download the installer from the Strawberry Perl website, run it, then open a new Command Prompt or PowerShell window and check perl -v.

An alternative is ActiveState Perl, a commercial distribution with its own package tools. If you already use WSL (Windows Subsystem for Linux), you can instead follow the Linux steps inside it.

macOS

macOS includes a system perl, but it is often an older release and it belongs to the operating system. It is fine for learning. For a newer version, install one with Homebrew:

Terminal
brew install perl

Linux

Perl is part of nearly every Linux distribution because many system tools use it. To get a newer or complete install, use your package manager:

Terminal
sudo apt install perl        # Debian, Ubuntu
sudo dnf install perl        # Fedora, RHEL

The perl package may be a bare-bones install on some distributions. If a core module seems to be missing, look for packages named like perl-modules or perl-doc.

perlbrew

perlbrew installs and switches between Perl versions inside your home directory, with no administrator rights. It is the best way to try the newest release without touching the system Perl:

Terminal
perlbrew install perl-5.42.3
perlbrew switch perl-5.42.3
perl -v

Compiling Perl takes a few minutes. You will see more about perlbrew in CPAN and Dependency Management.

Three ways to run Perl

1. A one-liner with -e

The -e flag runs the code that follows it, without a file. This is how you test an idea quickly:

Terminal
perl -e 'print "Hello from Perl\n"'
TEXT
Hello from Perl

On Linux and macOS, wrap the code in single quotes. On Windows Command Prompt, use double quotes outside and q() or qq() for strings inside, because single quotes do not group arguments there:

TEXT
perl -e "print qq(Hello from Perl\n)"

The -E flag is the same but turns on the newer features, including say:

Terminal
perl -E 'say "Hello"'

2. A script file

For anything longer than a line, save the code in a file. Perl scripts usually end in .pl:

PERL
use v5.36;

say "Hello from a file";

Save that as hello.pl, then run it by giving the file name to perl:

Terminal
perl hello.pl
TEXT
Hello from a file

3. A file you run directly

On Linux and macOS, you can make the script executable and run it like any program. The first line, the shebang, tells the operating system which interpreter should read the file:

PERL
#!/usr/bin/env perl
use v5.36;

say "I run by myself";
Terminal
chmod +x hello.pl
./hello.pl

#!/usr/bin/env perl asks the system to find perl on your PATH, so it works with perlbrew and other installs. On Windows, file associations decide how .pl files run, so you normally just type perl hello.pl.

The shebang mechanism is not special to Perl. The Bash example below proves it, using the same steps you would use for a Perl script.

Bash Stand-in: how the shebang line works (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.

Both calls print the line. For a Perl file, ./hello.pl and perl hello.pl behave the same way for the same reason.

Useful command-line flags

You will see a few flags again and again. The most useful for beginners:

Flag What it does
-v Prints the version.
-e 'code' Runs the code from the command line.
-E 'code' Like -e, but enables modern features such as say.
-c Compiles the script and checks the syntax without running it.
-w Turns on warnings globally (prefer use warnings in the file).
-l Adds a newline after each print.

The -c flag is a quick safety net. After editing hello.pl, run perl -c hello.pl and Perl answers hello.pl syntax OK, or lists the errors, without executing anything. The Python example below uses the same idea with Python's own compiler.

Python Stand-in: check syntax without running (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.

Reading the documentation with perldoc

Perl comes with excellent documentation, and it is installed on your machine. The perldoc command shows it in the terminal:

Terminal
perldoc perlintro        # a short introduction to Perl
perldoc -f print         # one built-in function
perldoc -q sort          # search the FAQ for a word
perldoc List::Util       # documentation for a module

Press q to leave the viewer. The same documents are on the web at perldoc.perl.org. Each time you meet a new function in this tutorial, try perldoc -f name to see the official description.

What this site can and cannot run

The in-browser editors on this site run languages such as Python, SQL, Bash and JavaScript. Perl is not among them, so:

  • Every ```perl block is read-only, and the expected output is shown beside it.
  • Interactive examples in this tutorial are stand-ins in Python or Bash that mimic regex and text-processing ideas. A label on each one says what is real Perl and what is not.
  • To really run the examples, install Perl as described above and paste the code into a .pl file.

Typing the examples yourself is the best way to learn, so keep a terminal open next to the lessons.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command prints your Perl version?

    perl -v prints the version. -c only checks syntax.
  2. 2. What does the shebang line "#!/usr/bin/env perl" do?

    When you run an executable file directly, the operating system reads the shebang to choose the interpreter. Perl itself treats the line as a comment.
  3. 3. Which flag checks a script's syntax without running it?

    perl -c file.pl compiles the file and reports syntax OK or lists errors, without executing the code.
  4. 4. Why use perlbrew?

    perlbrew installs versions under your home folder, so you can try new releases without touching the system Perl.

Summary

  • Check for Perl with perl -v; Linux and macOS usually have it, Windows users can install Strawberry Perl.
  • perlbrew installs and switches Perl versions in your home directory.
  • Run code with perl -e, perl file.pl, or ./file.pl after chmod +x with a #!/usr/bin/env perl first line.
  • -c checks syntax, -E enables modern features, and perldoc shows the built-in documentation.
  • Perl is not runnable on this site, so Perl blocks are read-only and the TryIts are labelled stand-ins.

Now write something: Your First Perl Program.