History

  • How:
    • Perl was created by Larry Wall in 1987 as a general-purpose programming language for text processing, inspired by existing languages like C, sed, awk, and shell scripting. Initially, Perl was designed to make report processing easier by combining the power of C with the ease of scripting.
    • Over time, Perl evolved into a more powerful language with support for object-oriented programming, regular expressions, and a vast array of libraries. Its flexibility and expressiveness led to it being widely used for web development, system administration, and data manipulation.
    • Perl was one of the first languages to gain popularity for CGI scripting and web development in the 1990s, before the rise of PHP, Python, and Ruby.
    • Perl’s development has been marked by its “There’s more than one way to do it” (TMTOWTDI) philosophy, which promotes flexibility and expressiveness in how developers solve problems.
  • Who:
    • Larry Wall, a linguist and programmer, is the creator of Perl. He was motivated by the need for a language that combined text manipulation with system programming capabilities.
    • The development of Perl is supported by the Perl Foundation, a non-profit organization that oversees the ongoing maintenance and growth of the language, as well as its open-source community.
    • The Perl community has always been strong, with numerous contributors who extend the language’s features and libraries. However, Perl has seen a decline in popularity in favor of newer languages like Python, Ruby, and JavaScript in the past decade.
  • Why:
    • Perl was created to fill a gap in text processing and to provide an easy-to-learn, powerful scripting language for a wide variety of programming tasks, especially those involving regular expressions, file handling, and system administration.
    • It was designed to provide flexibility and expressiveness, allowing developers to write concise code to solve complex problems. Its versatility led to widespread use in web development, networking, database management, and automation.
    • The language also gained traction due to its support for regular expressions as a first-class feature, making it popular for text parsing, data extraction, and web scraping tasks.

Introduction

Advantages

  • Unrivaled Text Manipulation — Built-in, compile-level integration of regular expressions makes it exceptionally powerful for parsing logs, editing text, and data extraction.
  • TMTOWTDI Philosophy — “There’s more than one way to do it” allows programmers to write code using functional, procedural, object-oriented, or brief script-oriented styles.
  • Massive Ecosystem (CPAN) — The Comprehensive Perl Archive Network has over 200,000 modules solving almost every possible programming challenge.
  • Cross-Platform Scripting — Installed by default on virtually all Unix, Linux, and macOS systems, making it highly portable.
  • Backward Compatibility — Excellent long-term stability; Perl scripts written 20 years ago often run without modification on modern interpreters.

Disadvantages

  • Code Readability (“Write-Only Language”) — The extreme flexibility and heavy use of default variables (like $_) can lead to highly obfuscated, unmaintainable code if not structured carefully.
  • Outdated Objects & Features — Standard object orientation relies on manual package “blessing”, making it feel clunky compared to modern class keywords.
  • Declining Popularity — Replaced in web backend development and scripting by Python, Go, and Node.js, making libraries and support for new tech rare.
  • Single-threaded Core — Threading is not natively lightweight, relying instead on process forks or heavy interpreter thread layers.

Remember Points

  • Context Matters — Code executes differently in Scalar vs. List context.
  • Use Pragmas — Always start scripts with use strict; and use warnings; to catch typos and syntax pitfalls.
  • Variables have Sigils — variable type is prefixed by a symbol indicating its structure ($, @, %).

Basics

Hello World & Entry Point

#!/usr/bin/env perl
use strict;
use warnings;
 
print "Hello, World!\n";
  • The shebang #!/usr/bin/env perl specifies the path to the Perl interpreter on Unix environments.
  • use strict; enforces variable declarations (forces use of my).
  • use warnings; displays compile-time and runtime warnings.
  • print writes text to standard output.

Comments

# This is a single-line comment.
 
=pod
This is a multi-line comment block
using Perl's Plain Old Documentation (POD) format.
=cut

Variables and Sigils

# 1. Scalars ($) - Holds single values: strings, numbers, or references
my $name = "VR Rathod";
my $age = 25;
my $pi = 3.1415;
 
# 2. Arrays (@) - Ordered lists of scalars
my @colors = ("red", "green", "blue");
print $colors[0]; # Access element using scalar sigil '$' -> "red"
 
# 3. Hashes (%) - Unordered key-value pairs (associative arrays)
my %scores = (
  "Alice" => 95,
  "Bob"   => 87,
);
print $scores{"Alice"}; # Access value using '$' sigil -> 95

Operators

# Arithmetic
# +, -, *, /, ** (exponentiation), % (modulo)
 
# Numeric Comparisons
# ==, !=, <, >, <=, >=, <=> (returns -1, 0, or 1)
 
# String Comparisons (Crucial: Perl differentiates numeric and string comparisons)
# eq, ne, lt, gt, le, ge, cmp (returns -1, 0, or 1)
my $x = "10";
my $y = "2";
if ($x > $y) { print "Numeric: 10 > 2"; }
if ($x lt $y) { print "String: 10 is alphabetically less than 2"; }
 
# Logical
# &&, ||, !
# and, or, not (Lower precedence versions, useful for control flow)
open(my $fh, "<", "file.txt") or die "Cannot open: $!";
 
# String Operators
# . (concatenation), x (repetition)
my $str = "Hello" . " " . "World"; # Concatenation
my $repeat = "ab" x 3; # "ababab"

Scalar vs. List Context

  • In Perl, the same expression can yield different values depending on the context the parser expects.
my @animals = ("lion", "tiger", "bear");
 
# List context: assigning to an array copy
my @copy = @animals; # @copy becomes ("lion", "tiger", "bear")
 
# Scalar context: assigning to a scalar
my $count = @animals; # $count becomes 3 (size of the array!)

Control Flow

Conditionals: if, unless, ternary

# 1. If-Else
if ($score >= 90) {
  print "Grade A\n";
} elsif ($score >= 80) {
  print "Grade B\n";
} else {
  print "Grade F\n";
}
 
# 2. Unless (runs if condition is FALSE)
unless ($is_authenticated) {
  print "Access Denied\n";
}
 
# 3. Statement Modifiers (concise syntax for single commands)
print "Access Granted\n" if $is_authenticated;
die "File Error" unless -e "config.txt";

Loops: for, while, until

# 1. C-Style For Loop
for (my $i = 0; $i < 5; $i++) {
  print "$i ";
}
 
# 2. Foreach Loop (iterating lists)
my @list = (10, 20, 30);
foreach my $val (@list) {
  print "$val\n";
}
 
# 3. While Loop
my $count = 0;
while ($count < 3) {
  print $count++;
}
 
# 4. Until Loop (runs until condition becomes TRUE)
until ($count == 0) {
  print $count--;
}

Loop Control Keywords

# last - exits loop (equivalent to 'break' in C/C++/Java)
# next - skips to next iteration (equivalent to 'continue' in C/C++/Java)
# redo - restarts current iteration without evaluating condition
 
for my $i (1..10) {
  next if $i % 2 == 0; # Skip evens
  last if $i > 7;      # Break early
  print "$i ";
}
# Output: 1 3 5 7

Functions & Subroutines

Subroutine Definition & Arguments

# Arguments are passed to subroutines in the special array @_
sub add {
  my ($a, $b) = @_; # Destructure arguments
  return $a + $b;
}
 
# Alternative argument reading using 'shift' (shifts head of @_)
sub greet {
  my $name = shift;
  print "Hello, $name!\n";
}
 
print add(5, 10); # 15
greet("VR");

Scoping: my, local, our

# my    - Private lexical scope variable (block-scoped). Recommended.
# local - Temporary value for global dynamic variable; restored when block exits.
# our   - Declares a package global variable.
 
our $global_val = 100;
 
sub test_scopes {
  my $lexical = 10; # isolated to test_scopes
  local $global_val = 200; # overrides $global_val dynamically
  call_other();
}
 
sub call_other {
  print $global_val; # Prints 200 due to dynamic scope lookup from local!
}

Data Structures & References

References Basics

  • Pointers in Perl are called References. They allow nested data arrays and hashes.
my @array = (1, 2, 3);
my %hash = (a => 1, b => 2);
 
# Create references using backslash (\)
my $array_ref = \@array;
my $hash_ref = \%hash;
 
# Dereferencing using arrow operator (->)
print $array_ref->[0]; # Access element 0 -> 1
print $hash_ref->{a};   # Access key "a" -> 1

Anonymous Arrays and Hashes

# Anonymous Array Reference []
my $list_ref = [ "apple", "banana", "cherry" ];
 
# Anonymous Hash Reference {}
my $user_ref = {
  name  => "VR",
  email => "vr@example.com",
};

Nested Data Structures (HoH)

# Hash of Hashes
my $company = {
  sales => {
    manager => "Alice",
    employees => 5,
  },
  marketing => {
    manager => "Bob",
    employees => 3,
  }
};
 
# Accessing nested structures
print $company->{sales}->{manager}; # "Alice"

Object-Oriented Programming

Packages and blessing

  • In classic Perl, OOP is implemented using packages and the bless function, which binds a reference to a package namespace.
# Person.pm (Module file)
package Person;
use strict;
use warnings;
 
# Constructor method
sub new {
  my $class = shift;
  my $self = {
    name => shift,
    age  => shift,
  };
  # bless converts the anonymous hash into an object of package $class
  return bless $self, $class;
}
 
sub display {
  my $self = shift;
  print "Name: " . $self->{name} . ", Age: " . $self->{age} . "\n";
}
 
1; # Modules must return true
  • Usage:
use Person;
my $user = Person->new("VR Rathod", 25);
$user->display(); # Name: VR Rathod, Age: 25

Regular Expressions

Match and Substitution Operators

my $text = "The quick brown fox jumps over the lazy dog";
 
# 1. Match Operator (m// or //)
if ($text =~ /quick/) {
  print "Found quick!\n";
}
 
# 2. Substitution Operator (s///)
$text =~ s/fox/cat/; # Replaces "fox" with "cat"
 
# 3. Global modification (/g) and Case-Insensitive (/i)
my $digits = "123-456-789";
$digits =~ s/\d/[x]/g; # Replaces all digits -> "[x][x][x]-[x][x][x]-[x][x][x]"

Regex Capturing Variables

my $date = "2026-05-28";
 
if ($date =~ /(\d{4})-(\d{2})-(\d{2})/) {
  my $year  = $1;
  my $month = $2;
  my $day   = $3;
  print "Year: $year, Month: $month, Day: $day\n";
}

File I/O

Reading and Writing Files

use strict;
use warnings;
 
# 1. Writing to a file (use '>' for overwrite, '>>' for append)
open(my $write_fh, ">", "output.txt") or die "Could not open: $!";
print $write_fh "Hello, Perl File I/O!\n";
close($write_fh);
 
# 2. Reading line-by-line
open(my $read_fh, "<", "output.txt") or die "Could not open: $!";
while (my $line = <$read_fh>) {
  chomp($line); # Removes trailing newline character
  print "Line: $line\n";
}
close($read_fh);

File Test Operators

my $filename = "output.txt";
 
print "Exists\n"     if -e $filename;
print "Is file\n"    if -f $filename;
print "Is directory" if -d $filename;
print "Readable\n"   if -r $filename;
print "Size: " . -s $filename . " bytes\n";

Exception Handling

Eval blocks

# eval blocks capture runtime exceptions.
# If code fails, $@ contains the error message.
eval {
  my $result = 10 / 0; # Throws division by zero error
};
if ($@) {
  print "Caught error: $@";
}

Modules & Libraries

Standard Module Construction

# MyMath.pm
package MyMath;
use strict;
use warnings;
 
# Required module to inherit export functionality
use Exporter qw(import);
our @EXPORT_OK = qw(add multiply); # Functions that can be explicitly imported
 
sub add { return $_[0] + $_[1]; }
sub multiply { return $_[0] * $_[1]; }
 
1;
  • Usage:
use MyMath qw(add);
print add(3, 4); # 7

Bit Manipulation

Bitwise Operators

my $a = 12; # 00001100
my $b = 25; # 00011001
 
print $a & $b;  # AND -> 8 (00001000)
print $a | $b;  # OR  -> 29 (00011101)
print $a ^ $b;  # XOR -> 21 (00010101)
print ~$a;      # NOT
print $a << 2;  # Left shift -> 48
print $a >> 2;  # Right shift -> 3

Special Variables

Built-in Constants

Variable      Description
$_            Default input and pattern-matching space (implicit parameter)
@_            Argument list array within subroutines
$@            Error message from the last eval expression
$!            System error string from the last OS/C-library call (errno)
$$            Process ID (PID) of the current running perl interpreter
$0            Name of the script file executing
@ARGV         Array containing command line arguments
%ENV          Hash containing current system environment variables

Command Line Flags

One-Liners and Inline execution

  • Perl command line flags allow powerful text editing without writing script files:
# -e : Execute inline string command
perl -e 'print "Hello CLI!\n"'
 
# -p : Runs command inside a loop printing $_ for every line of input
# -i : Modifies files in-place
# Replace all occurrences of "fox" with "cat" in file.txt directly:
perl -pi -e 's/fox/cat/g' file.txt
 
# -n : Loops over lines of input, but does not print automatically
# Print only lines containing "ERROR" from syslog:
perl -ne 'print if /ERROR/' syslog.log

Useful Libraries & Frameworks

  • Catalyst - Powerful, enterprise-grade MVC web framework for Perl.
  • Dancer2 - Lightweight, simple Sinatra-inspired web microframework.
  • Mojolicious - Real-time web framework with modern async/non-blocking IO out-of-the-box.
  • DBI - Database Interface module allowing database connectivity (MySQL, PostgreSQL, SQLite).
  • Moose - Complete modern object system for Perl 5, implementing declarative OO.

More Learn

Explore valuable resources for Perl: