Perl Language

From bibbleWiki
Revision as of 21:09, 10 February 2026 by Iwiseman (talk | contribs)
Jump to navigation Jump to search

Introduction

Perl is a pragmatic, text-friendly language.

  • Scripting language
  • Strong text processing
  • Dynamic typing
  • Multi-paradigm (procedural, OO, functional style)
#!/usr/bin/env perl
use strict;
use warnings;

print "Hello World\n";

VS Code and Perl

Package Installation

sudo apt update

# Core build + Perl tooling
sudo apt install -y build-essential git perl perl-dbg cpanminus

# Common native libs used by CPAN modules
sudo apt install -y libssl-dev libreadline-dev zlib1g-dev libffi-dev libyaml-dev

# Postgres client/dev + DBI drivers
sudo apt install -y libpq-dev libdbi-perl libdbd-pg-perl

# Web framework (optional)
sudo apt install -y libmojolicious-perl

# If you build XS modules that need libperl/libaio
sudo apt install -y libperl-dev libaio-dev

Perl local::lib Environment Variables

Think of `local::lib` like Python `venv`: it gives you a user-scoped install location so your modules and scripts do not touch the system Perl. Unlike `venv`, it does not create a separate Perl executable; it adjusts environment variables so Perl loads modules from your local tree first. These are typical environment variables produced by `local::lib` to install CPAN modules into a user directory instead of system-wide.

PATH="/home/iwiseman/perl5/bin${PATH:+:${PATH}}"; export PATH;
PERL5LIB="/home/iwiseman/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/iwiseman/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/iwiseman/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/iwiseman/perl5"; export PERL_MM_OPT;
  • PATH: prepends the local Perl `bin` so user-installed scripts are found first.
  • PERL5LIB: adds the local Perl library path so `use` and `require` can find modules.
  • PERL_LOCAL_LIB_ROOT: tracks the base directory used by `local::lib` (can be stacked).
  • PERL_MB_OPT: tells `Module::Build` to install into the local base.
  • PERL_MM_OPT: tells `ExtUtils::MakeMaker` to install into the local base.

Running

If you have Perl installed, you can run a script directly.

perl script.pl

Debugging

VS Code can debug with the Perl debugger via the Perl extension. Example launch config:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "perl",
      "request": "launch",
      "name": "Perl Debug App",
      "program": "${workspaceFolder}/script/app.pl",
      "args": ["daemon", "-l", "http://0.0.0.0:5001", "-m", "production"],
      "env": {
        "MOJO_REACTOR": "POLL"
      },
      "cwd": "${workspaceFolder}",
      "stopOnEntry": false,
      "reloadModules": true
    }
  ]
}

Types

Perl has scalars, arrays, and hashes. Scalars can hold strings, numbers, and references.

  • Scalars: numbers, strings, references
  • Arrays: ordered lists
  • Hashes: key/value maps

Numbers

Perl uses numeric context for arithmetic.

my $one = 1;          # integer
my $pi = 3.14159;     # float
my $big = 1_000_000;  # underscore for readability

Strings

Double quotes interpolate variables and escapes; single quotes do not.

my $name = "Perl";
my $greet = "Hello, $name\n";
my $raw = 'Hello, $name\n';

print $greet;
print $raw;

Arrays

Arrays are ordered lists.

my @numbers = (1, 2, 3, 4, 5);
print $numbers[2];  # 3

Hashes

Hashes map keys to values.

my %ages = (
  Alice => 30,
  Bob   => 25,
);

print $ages{Alice};

Collections

Filtering and Mapping

Perl uses grep for filtering and map for transforms.

my @ints = (1, 2, 3, 4, 5);

my @small = grep { $_ < 4 } @ints;
my @squares = map { $_ * $_ } @ints;

Combining

my @ints = (1, 2, 3, 4, 5);

my @small_squares = map { $_ * $_ }
                    grep { $_ < 4 } @ints;

Flow Control

If

if ($answer eq $correct_answer) {
  print "You were correct\n";
} else {
  print "Try again\n";
}

Given/When

Perl supports a switch-like feature via given/when (use with feature). Note: it is experimental in some versions.

use feature 'switch';

my $number = 3;

given ($number) {
  when (0)    { print "Invalid number\n"; }
  when ([1,2]) { print "Number too low\n"; }
  when (3)    { print "Number correct\n"; }
  when (4)    { print "Number too high, but acceptable\n"; }
  default     { print "Number too high\n"; }
}

For and Foreach

for (my $i = 0; $i < 5; $i++) {
  print $i;
}

foreach my $n (@numbers) {
  print $n;
}

Community Notes

Popular Today

  • Perl 5 – the mainline Perl most people mean when they say “Perl”; actively maintained, widely deployed in sysadmin, web, data-processing and scripting.
  • Mojolicious – modern, non-blocking web framework (routers, templates, testing helpers, WebSockets, etc.); good default choice for new web apps.
  • DBI / DBD::* – standard way to talk to databases (e.g. `DBD::Pg` for Postgres); forms the base for higher-level ORMs.
  • Carton / cpanminus – common tools to manage project-specific dependencies from CPAN (`cpanm`) and lockfiles (`Carton`) without touching system Perl.

Less Popular / Legacy

  • Perl 6 / Raku – Perl 6 was renamed to Raku and is now its own language and community; interesting, but not a path for “upgrading” Perl 5 code.
  • CGI.pm – classic module for raw CGI-style web programming; still works, but most new code uses PSGI/Plack or frameworks like Mojolicious instead.
  • mod_perl-only stacks – historically popular for embedding Perl in Apache; many new projects prefer FastCGI, PSGI/Plack, or Mojolicious daemons behind nginx/Apache.