Skip to content

Perl Environment Setup

Perl is highly portable and available on a vast range of operating systems. Before you start writing Perl programs, you need to ensure Perl is installed on your system.

  • Unix-like systems: Linux (various distributions), macOS, FreeBSD, Solaris, AIX, HP-UX, etc.
  • Windows: Modern Windows versions (7, 8, 10, 11, Server editions).
  • And many other platforms.

Most Unix-like systems, including macOS, come with Perl pre-installed. To check if Perl is installed and its version, open a terminal or command prompt and type:

$ perl -v

If Perl is installed, you’ll see output similar to this (the version numbers will vary):

This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux-thread-multi
Copyright 1987-2021, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.

If you don’t see such a message, or if the version is very old, you may need to install or update Perl.

The official Perl website is the primary source for Perl distributions and information: https://www.perl.org/

For documentation, visit: https://perldoc.perl.org/

Installation methods vary by operating system.

Most Linux distributions provide Perl through their package managers. This is often the easiest way to install or update it.

  • Debian/Ubuntu: sudo apt update && sudo apt install perl
  • Fedora/CentOS/RHEL: sudo dnf install perl (or yum on older systems)
  • Using perlbrew or plenv (Recommended for Developers): For managing multiple Perl versions or installing a specific version without affecting the system Perl, tools like perlbrew (https://perlbrew.pl/) or plenv (https://github.com/tokuhirom/plenv) are highly recommended. They allow you to install Perl versions in your home directory.
    • Example with perlbrew: \curl -L https://install.perlbrew.pl | bash perlbrew install perl-stable perlbrew switch perl-stable
  • Building from Source (Advanced): You can download the source code from perl.org and compile it. This gives maximum control but is more complex. The general steps are: tar -xzf perl-5.x.y.tar.gz cd perl-5.x.y ./Configure -des -Dprefix=$HOME/localperl (example prefix) make make test make install

For Windows, the most popular distribution is Strawberry Perl:

  • Strawberry Perl: (http://strawberryperl.com/) - A community-driven Perl distribution for Windows that includes a C compiler (GCC), pre-installed common modules, and tools (like cpanm) needed for installing additional CPAN modules. Download the installer (32-bit or 64-bit) and run it. The wizard is straightforward.
  • ActivePerl: (https://www.activestate.com/products/perl/) - Another well-known commercial distribution. It has a free community edition.

macOS comes with a system Perl, but it’s often not the latest version and is best left untouched for system scripts.

  • Using perlbrew or plenv (Recommended for Developers): Similar to Linux, these tools are excellent for managing Perl environments on macOS.
  • Homebrew: (https://brew.sh/) - You can install Perl using Homebrew: brew install perl.

There are several ways to execute Perl code:

You can execute short Perl commands directly from the command line using the -e switch:

perl -e 'print "Hello, Perl!\n";'

The -M switch can be used to load modules, e.g., perl -Mstrict -Mwarnings -e '...'.

Common command-line options:

OptionDescription
-e ‘code’Executes the given Perl code.
-cChecks syntax of a script without running it.
-wEnables many useful warnings (equivalent to use warnings; for simple scripts).
-MmoduleLoads the specified module (e.g., -Mstrict).
-IdirectoryAdds a directory to Perl’s module search path (@INC).
-d[:debugger]Runs the script under the Perl debugger.

The most common way is to save your Perl code in a file (e.g., my_script.pl) and execute it with the perl interpreter:

perl my_script.pl

On Unix-like systems, you can make the script executable by adding a “shebang” line at the top (e.g., #!/usr/bin/perl or #!/usr/bin/env perl) and setting execute permissions (chmod +x my_script.pl). Then you can run it directly: ./my_script.pl.

A typical Perl script starts with:

#!/usr/bin/env perl
use strict; # Enforces stricter parsing and error checking
use warnings; # Enables helpful diagnostic warnings
# Your Perl code here
print "This is a Perl script.\n";

3. Integrated Development Environments (IDEs)

Section titled “3. Integrated Development Environments (IDEs)”

While many Perl developers use powerful text editors (like Vim, Emacs, Sublime Text, VS Code) with Perl plugins, there are also IDEs:

  • Visual Studio Code (VS Code): With extensions like “Perl Navigator” or “Perl Toolbox”, VS Code offers excellent Perl support.
  • Padre, the Perl IDE: (http://padre.perlide.org/) - An IDE written in Perl for Perl development. (Development has slowed in recent years).
  • Eclipse with EPIC (Perl Editor and IDE for Eclipse): If you use Eclipse.
  • IntelliJ IDEA with Perl Plugin: Provides good support for Perl development.

Ensure your environment is set up correctly before proceeding. Having a working Perl installation and a comfortable editor or IDE will make your learning experience smoother.