Install Perl and check the version

Is Perl installed

Perl ships with Linux, macOS and most UNIX systems, so there is a good chance you already have it. Ask your shell where the program lives:

which perl

/usr/bin/perl

An empty answer means Perl is missing. The per-system pages below cover the install for each operating system: Ubuntu, Rocky Linux, macOS, FreeBSD, Windows 11, or run Perl in a container: Docker.

perl -v

The version flag prints a short banner followed by a copyright notice. The first line is the one that matters: it carries the language version and the patch level.

perl -v

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

Any release from 5.20 upward runs everything in this course. Do not worry if your number differs from the one above: Perl adds features without breaking old code.

One version, in detail

When a bug report needs the exact build, ask for a single configuration value instead of the whole banner. The version variable holds the release exactly as this build was configured:

perl -V:version

version='5.42.3';

Inside a program the same data is available as $], the numeric version, and $^V, the version object. Both are described in the next lessons.

Several versions side by side

System Perl belongs to the operating system, and upgrading it can break tools the system depends on. For experiments, install a private copy with perlbrew (or plenv, which works the same way). Both keep every version under your home folder and switch with a shell command.

Install perlbrew, then build and switch to the newest stable release:

curl -L https://install.perlbrew.pl | bash

perlbrew install perl-stable

perlbrew switch perl-stable

Future lessons use the Perl you just checked. If you installed a private copy, close the terminal and open a new one so the shell picks up the new path; the notes on PATH explain why that helps.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does which perl tell you?

    Answer

    The full path of the interpreter, and an empty answer means none is installed

    which searches PATH and prints the path it finds; when nothing is printed, the shell has no perl at all.

  2. Which Perl release is the oldest one that runs everything in this course?

    Answer

    5.20

    The course stays within features of 5.20 and later, so an older release has no guarantee.

  3. How does perl -V:version differ from perl -v?

    Answer

    It prints one configuration value, the version of the build, and nothing else

    -V:name asks for a single value of the build configuration, which is what a bug report needs.

  4. What do $] and $^V hold inside a program?

    Answer

    The numeric version and the version as an object that prints with a leading v

    $] is a number such as 5.042003 for comparisons, and $^V prints as v5.42.3.

  5. What is perlbrew for?

    Answer

    Installing and switching private Perl versions under your home folder

    perlbrew builds each version in your home folder and switches with one shell command, so the system Perl stays untouched.

  6. Why should you not upgrade the system Perl of the operating system?

    Answer

    System tools depend on the installed interpreter, so upgrading it can break them

    The distribution ships Perl for its own tools; a private copy with perlbrew or plenv is the safe way to try a newer release.

  7. What does this program print? print $] >= 5.020 ? "new enough\n" : "too old\n";

    Answer

    new enough

    On Perl 5.42 the numeric version is 5.042003, which is greater than 5.020, so the first branch runs.

  8. What does this program print? my $v = $]; print $v < 5.020 ? "upgrade\n" : "your Perl is fine\n";

    Answer

    your Perl is fine

    Any Perl from 5.20 on has a numeric version of at least 5.020, so the comparison sends the program to the second branch.

  9. A reader writes print "5.20" <= $] ? "yes\n" : "no\n"; on Perl 5.42. What does it print, and why?

    Answer

    no, because $] is 5.042003 and that number is smaller than 5.20

    $] is a decimal fraction, not a release number, so 5.042003 is below 5.20; the version is written 5.020 in such a comparison.

  10. What does this program print? use Config; print $Config{version} =~ /^\d+\.\d+\.\d+$/ ? "dotted\n" : "other\n";

    Answer

    dotted

    The version key of %Config holds the dotted release, as perl -V:version prints it, and Config is a core module.

Article author: Arthur Isaev