Perl Environment Installation
Check if Perl is Already Installed
Most Linux and Unix systems (including macOS) come with Perl pre-installed. You can check with the following command:
perl -vIf you see output similar to the following, Perl is already installed:
This is perl 5, version 38, subversion 0 (v5.38.0) built for x86_64-linux
...If not installed or you need to upgrade, follow the steps below.
Installing on Linux
Ubuntu/Debian
# Update package list
sudo apt-get update
# Install Perl
sudo apt-get install perl
# Install additional development tools (recommended)
sudo apt-get install build-essential cpanminusCentOS/RHEL/Fedora
# CentOS/RHEL
sudo yum install perl
sudo yum install perl-App-cpanminus
# Fedora
sudo dnf install perl
sudo dnf install perl-App-cpanminusArch Linux
sudo pacman -S perl perl-cpanminusCompile from Source (Latest Version)
If you need to install the latest version of Perl:
# 1. Download source code
wget https://www.cpan.org/src/5.0/perl-5.38.0.tar.gz
# 2. Extract
tar -xzf perl-5.38.0.tar.gz
cd perl-5.38.0
# 3. Configure
./Configure -des -Dprefix=$HOME/localperl
# 4. Compile and install
make
make test
make install
# 5. Update PATH (add to ~/.bashrc or ~/.zshrc)
export PATH="$HOME/localperl/bin:$PATH"Installing on macOS
Using Homebrew
# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Perl
brew install perl
# Install cpanminus
brew install cpanminusUsing System Perl
macOS comes with Perl, but the version may be outdated. You can install an updated version through a package manager.
Installing on Windows
Using Strawberry Perl (Recommended)
Strawberry Perl is the most popular Perl distribution on Windows, including a compiler and complete CPAN support.
- Visit the Strawberry Perl website
- Download the latest installer (e.g.,
strawberry-perl-5.38.0.1-64bit.msi) - Run the installer and follow the prompts
- After installation, open Command Prompt and enter
perl -vto verify
Using ActivePerl
ActivePerl is another Perl distribution for Windows, offering commercial support.
- Visit the ActivePerl website
- Download the free community edition
- Run the installer
- Verify installation:
perl -v
Using Chocolatey
If you use Chocolatey package manager:
# Run PowerShell as administrator
choco install strawberryperlConfiguring Perl Environment
Setting Environment Variables
Linux/macOS
Edit ~/.bashrc or ~/.zshrc, add:
# Perl related paths
export PERL5LIB="$HOME/perl5/lib/perl5:${PERL5LIB}"
export PERL_LOCAL_LIB_ROOT="$HOME/perl5:${PERL_LOCAL_LIB_ROOT}"
export PERL_MB_OPT="--install_base \"$HOME/perl5\""
export PERL_MM_OPT="INSTALL_BASE=$HOME/perl5"
export PATH="$HOME/perl5/bin:${PATH}"Then reload the configuration:
source ~/.bashrc
# or
source ~/.zshrcWindows
Strawberry Perl automatically configures environment variables after installation. For manual configuration:
- Right-click "This PC" → "Properties" → "Advanced System Settings" → "Environment Variables"
- Find "Path" in "System Variables" and add:
C:\Strawberry\perl\binC:\Strawberry\perl\site\binC:\Strawberry\c\bin
Configuring CPAN
CPAN is Perl's module repository and needs configuration to work properly.
# First run will enter interactive configuration
perl -MCPAN -e shell
# Or use cpanminus (recommended, simpler)
cpanmDuring configuration, usually choosing default options is sufficient.
Installing Common Modules
# Install using cpanminus
cpanm Modern::Perl
cpanm Data::Dumper
cpanm JSON::XS
cpanm DBI
cpanm DBD::mysql
cpanm LWP::UserAgentVerifying Installation
Create a test script test.pl:
#!/usr/bin/perl
use strict;
use warnings;
use v5.38;
print "Perl version: $^V\n";
print "Perl installation path: $^X\n";
print "Operating system: $^O\n";
print "Hello, World!\n";Run the test:
# Linux/macOS
chmod +x test.pl
./test.pl
# or
perl test.pl
# Windows
perl test.plRecommended Development Tools
Text Editors
- Vim: Powerful editor with Perl syntax highlighting plugins
- Emacs: Feature-rich editor supporting Perl
- VS Code: Modern editor with excellent Perl plugins
- Sublime Text: Lightweight editor supporting Perl
IDEs
- Padre: IDE designed specifically for Perl
- Eclipse: Supports Perl through EPIC plugin
- Komodo IDE: Commercial IDE supporting Perl
VS Code Perl Plugins
Recommended VS Code plugins:
- Perl - Provides syntax highlighting, code snippets
- Perl Navigator - Provides IntelliSense, code navigation
- Perl Critic - Code quality checking
Installation method:
- Open VS Code
- Press
Ctrl+Shift+Xto open the Extensions panel - Search and install the plugins above
Common Issues
Q: perl command not found
A: Check if the PATH environment variable includes Perl's bin directory.
Q: Module installation failed
A:
- Ensure network connection is working
- Use
cpanm --notest <Module>to skip tests - Check if dependencies are complete
Q: Permission issues
A:
- Linux/macOS: Use
sudoor install to user directory - Windows: Run Command Prompt as administrator
Q: Multiple Perl versions coexisting
A: Use perlbrew to manage multiple Perl versions:
# Install perlbrew
curl -L https://install.perlbrew.pl | bash
# Install specific version
perlbrew install perl-5.38.0
perlbrew switch perl-5.38.0Summary
After completing this chapter, you should have:
- ✅ Successfully installed Perl on your system
- ✅ Configured the development environment
- ✅ Installed necessary tools and modules
- ✅ Able to run Perl scripts
Next, we will learn Perl Basic Syntax.