Perl

#22
TIOBE#13
PYPL#29
GitHub#33
IEEESpectrum#25
programming languagescripting languagetext processingregular expressionssystem administrationbioinformatics

Programming Language

Perl

Overview

Perl is a programming language developed as a "Practical Extraction and Report Language" with powerful text processing capabilities.

Details

Perl is an interpreted programming language developed by Larry Wall in 1987, known for its philosophy "There's more than one way to do it." It features native regular expression support, rich string manipulation functions, and built-in associative arrays (hashes), making it particularly well-suited for text processing. Perl is widely used in system administration, log analysis, web development, and bioinformatics, with CPAN providing a massive library ecosystem. Modern Perl also includes comprehensive object-oriented and functional programming features.

Code Examples

Hello World

#!/usr/bin/perl
use strict;
use warnings;

# Basic output
print "Hello, World!\n";

# Output using variables
my $message = "Hello, Perl!";
print "$message\n";

# Formatted output
my $name = "John";
my $age = 25;
printf "My name is %s and I am %d years old.\n", $name, $age;

# say function (Perl 5.10+)
use feature 'say';
say "Newline is automatically added";

Variables and Data Types

#!/usr/bin/perl
use strict;
use warnings;

# Scalar variables ($ sigil)
my $number = 42;
my $float = 3.14;
my $string = "string";
my $boolean = 1;  # Perl doesn't have a dedicated boolean type

# Arrays (@ sigil)
my @fruits = ("apple", "banana", "orange");
my @numbers = (1, 2, 3, 4, 5);
my @mixed = ("string", 42, 3.14);

# Hashes (% sigil)
my %person = (
    "name" => "John Doe",
    "age" => 30,
    "city" => "Tokyo"
);

# References
my $array_ref = \@fruits;
my $hash_ref = \%person;

print "Number: $number\n";
print "Array size: " . scalar(@fruits) . "\n";
print "Hash value: $person{name}\n";
print "Via reference: " . $array_ref->[0] . "\n";

Conditional Statements

#!/usr/bin/perl
use strict;
use warnings;

# Basic if statement
my $score = 85;
my $grade;

if ($score >= 90) {
    $grade = "A";
} elsif ($score >= 80) {
    $grade = "B";
} elsif ($score >= 70) {
    $grade = "C";
} else {
    $grade = "D";
}

print "Score: $score, Grade: $grade\n";

# unless statement (opposite of if)
my $age = 20;
unless ($age < 18) {
    print "Adult\n";
}

# Postfix if/unless
print "You can drive\n" if $age >= 18;
print "Not a minor\n" unless $age < 18;

# given-when statement (Perl 5.10+)
use feature 'switch';
my $day = "Wednesday";

given ($day) {
    when ("Monday") { print "Start of the week\n"; }
    when ("Wednesday") { print "Midweek\n"; }
    when ("Friday") { print "Almost weekend\n"; }
    default { print "Other day\n"; }
}

Arrays and Hashes

#!/usr/bin/perl
use strict;
use warnings;

# Array operations
my @fruits = ("apple", "banana", "orange");

# Adding elements
push @fruits, "grape";
unshift @fruits, "strawberry";  # Add to beginning

# Removing elements
my $last = pop @fruits;      # Remove from end
my $first = shift @fruits;   # Remove from beginning

print "Array: " . join(", ", @fruits) . "\n";
print "Removed elements: $first, $last\n";

# Array slicing
my @selected = @fruits[0, 2];  # Elements at indices 0 and 2
print "Selected elements: " . join(", ", @selected) . "\n";

# Hash operations
my %person = (
    "name" => "John Doe",
    "age" => 30,
    "city" => "Tokyo"
);

# Adding new key-value pair
$person{"job"} = "Engineer";

# Check key existence
if (exists $person{"age"}) {
    print "Age: $person{age}\n";
}

# Get all keys and values
my @keys = keys %person;
my @values = values %person;

print "Keys: " . join(", ", @keys) . "\n";

# Hash iteration
for my $key (keys %person) {
    print "$key: $person{$key}\n";
}

Loops and Iteration

#!/usr/bin/perl
use strict;
use warnings;

# for loop (C-style)
for my $i (1..5) {
    print "$i squared is " . ($i * $i) . "\n";
}

# foreach loop
my @numbers = (1, 2, 3, 4, 5);
foreach my $num (@numbers) {
    print "Number: $num, Double: " . ($num * 2) . "\n";
}

# while loop
my $count = 0;
while ($count < 5) {
    print "Count: $count\n";
    $count++;
}

# do-while loop
my $counter = 0;
do {
    print "Execution count: " . ($counter + 1) . "\n";
    $counter++;
} while ($counter < 3);

# Postfix foreach
print "$_\n" foreach @numbers;

# Range operator
for (1..10) {
    print "Number: $_\n";  # $_ is the default variable
}

Subroutines (Functions)

#!/usr/bin/perl
use strict;
use warnings;

# Basic subroutine
sub greet {
    my ($name) = @_;
    return "Hello, $name!";
}

# Subroutine with default arguments
sub calculate_area {
    my ($width, $height) = @_;
    $height //= 10;  # Default value if undefined
    return $width * $height;
}

# Subroutine returning multiple values
sub get_name_age {
    return ("John Doe", 25);
}

# Variable arguments
sub sum {
    my $total = 0;
    $total += $_ foreach @_;
    return $total;
}

# Subroutine calls
my $message = greet("Alice");
print "$message\n";

my $area = calculate_area(5);  # height uses default value
print "Area: $area\n";

my ($name, $age) = get_name_age();
print "Name: $name, Age: $age\n";

my $result = sum(1, 2, 3, 4, 5);
print "Sum: $result\n";

Regular Expressions and Pattern Matching

#!/usr/bin/perl
use strict;
use warnings;

my $text = "My email address is test@example.com";

# Pattern matching
if ($text =~ /email/) {
    print "Found the word 'email'\n";
}

# Substitution
my $new_text = $text;
$new_text =~ s/test/user/;  # Replace test with user
print "After substitution: $new_text\n";

# Capture groups
if ($text =~ /(\w+)@(\w+\.\w+)/) {
    my $username = $1;
    my $domain = $2;
    print "Username: $username, Domain: $domain\n";
}

# Processing multiline text
my $multiline = <<'EOF';
Line 1: This is the first line
Line 2: This is the second line
Line 3: This is the last line
EOF

# Process each line
foreach my $line (split /\n/, $multiline) {
    if ($line =~ /^Line (\d+): (.+)$/) {
        print "Line number: $1, Content: $2\n";
    }
}

# Global matching
my $data = "Price: 100 yen, Tax included: 110 yen, Shipping: 200 yen";
my @prices = $data =~ /(\d+) yen/g;
print "Found prices: " . join(", ", @prices) . "\n";

File Operations

#!/usr/bin/perl
use strict;
use warnings;
use autodie;  # Automate error handling for file operations

# Writing to a file
open my $fh_out, '>', 'sample.txt';
print $fh_out "This is a test file.\n";
print $fh_out "Learning file operations in Perl.\n";
close $fh_out;

# Reading from a file
open my $fh_in, '<', 'sample.txt';
while (my $line = <$fh_in>) {
    chomp $line;  # Remove newline character
    print "Read: $line\n";
}
close $fh_in;

# Reading entire file at once
open my $fh_all, '<', 'sample.txt';
my @lines = <$fh_all>;
close $fh_all;

print "Total lines: " . scalar(@lines) . "\n";

# CSV file processing
my $csv_data = <<'EOF';
Name,Age,Job
John Doe,30,Engineer
Jane Smith,25,Designer
Bob Johnson,35,Manager
EOF

foreach my $line (split /\n/, $csv_data) {
    next if $line =~ /^Name/;  # Skip header line
    my ($name, $age, $job) = split /,/, $line;
    print "Name: $name, Age: $age, Job: $job\n";
}

Object-Oriented Programming

#!/usr/bin/perl
use strict;
use warnings;

# Package (class) definition
package Car;

# Constructor
sub new {
    my ($class, $make, $model, $year) = @_;
    my $self = {
        make => $make,
        model => $model,
        year => $year,
        mileage => 0
    };
    bless $self, $class;
    return $self;
}

# Accessor methods
sub make { my $self = shift; return $self->{make}; }
sub model { my $self = shift; return $self->{model}; }
sub year { my $self = shift; return $self->{year}; }
sub mileage { my $self = shift; return $self->{mileage}; }

# Instance methods
sub drive {
    my ($self, $distance) = @_;
    $self->{mileage} += $distance;
    print "Drove ${distance}km. Total mileage: $self->{mileage}km\n";
}

sub get_info {
    my $self = shift;
    return "$self->{year} $self->{make} $self->{model}";
}

# Return to main package
package main;

# Object creation and usage
my $car = Car->new("Toyota", "Prius", 2022);
print $car->get_info() . "\n";
$car->drive(50);
$car->drive(30);
print "Current mileage: " . $car->mileage() . "km\n";

Special Features

CPAN (Comprehensive Perl Archive Network)

#!/usr/bin/perl
use strict;
use warnings;

# Using CPAN modules
use LWP::Simple;  # Web content retrieval
use JSON;         # JSON processing
use DateTime;     # Date/time handling

# Fetch data from web API
my $url = "https://api.github.com/users/perl";
my $content = get($url);

if ($content) {
    my $data = decode_json($content);
    print "Username: $data->{login}\n";
    print "Name: $data->{name}\n";
    print "Followers: $data->{followers}\n";
}

# Date/time processing
my $now = DateTime->now();
print "Current time: " . $now->strftime('%Y-%m-%d %H:%M:%S') . "\n";

Special Variables and Context

#!/usr/bin/perl
use strict;
use warnings;

# Using special variables
my @data = ("apple", "banana", "orange");

print "Default variable \$_:\n";
for (@data) {
    print "  $_\n";  # $_ is the current element
}

# Array context
my $count = @data;          # Scalar context (element count)
my @copy = @data;           # Array context (copy)

print "Element count: $count\n";
print "Copy: " . join(", ", @copy) . "\n";

# Input line number
while (my $line = <DATA>) {
    chomp $line;
    print "Line $.: $line\n";  # $. is current line number
}

__DATA__
Data line 1
Data line 2
Data line 3

One-liners and Command Line Processing

# Add line numbers to each line of a file
perl -pe 'print "$.: "' file.txt

# Extract specific columns from CSV file
perl -F, -ane 'print $F[1], "\n"' data.csv

# String substitution
echo "Hello World" | perl -pe 's/World/Perl/'

# Batch string replacement in files
perl -pi -e 's/old/new/g' *.txt

Versions

Version Status Key Features Release Year
Perl 5.38 Latest Experimental features stabilization 2023
Perl 5.36 Current builtin functions, signatures improvements 2022
Perl 5.34 Current try-catch experimental support 2021
Perl 5.32 Current Chained operators 2020
Perl 5.30 Current Limited experimental features 2019
Perl 5.28 Maintenance Unicode 11.0 support 2018

Reference Pages

Official Documentation

Learning Resources

Development Tools