Complete reference for Perl built-in functions, regex, file I/O, DBI databases, and CGI scripting.
Part of The Direct Path to Linux Ubuntu — free for all.
Save as .pl file and run with perl script.pl.
Perl syntax, string interpolation, and list processing.
#!/usr/bin/perl
use strict; use warnings; use feature 'say';
say "Hello, MyWebUniversity! Perl " . $^V;
my @nums = 1..20;
my @even_sq = map { $_ ** 2 } grep { $_ % 2 == 0 } @nums;
say "Even squares: @even_sq";
say "Sum: " . do { my $s=0; $s+=$_ for @even_sq; $s };
my %lang_year = (Perl=>1987,Python=>1991,Ruby=>1995,Go=>2009,Rust=>2010,R=>1993);
say "$_: $lang_year{$_}" for sort { $lang_year{$a} <=> $lang_year{$b} } keys %lang_year;
# perl hello.plParse log files and extract structured data with regex.
#!/usr/bin/perl
use strict; use warnings; use feature 'say';
my $log = <<'END';
2026-06-01 10:23:45 ERROR 503 /api/users 152ms
2026-06-01 10:24:01 INFO 200 /api/posts 43ms
2026-06-01 10:24:15 ERROR 408 /api/search 5023ms
2026-06-02 09:10:01 INFO 200 /index.html 12ms
END
my (@errors, %endpoints, $total, $count);
while ($log =~ /^(\S+)\s+(\S+)\s+(\w+)\s+(\d+)\s+(\S+)\s+(\d+)ms$/mg) {
my ($date,$time,$level,$code,$ep,$ms) = ($1,$2,$3,$4,$5,$6);
push @errors, "$date $time $code $ep" if $level eq 'ERROR';
$endpoints{$ep}{count}++; $endpoints{$ep}{total} += $ms;
$total += $ms; $count++;
}
say "=== Errors ==="; say " $_" for @errors;
say "=== Endpoints ===";
for my $ep (sort keys %endpoints) {
printf " %-20s n=%d avg=%.1fms\n",
$ep, $endpoints{$ep}{count}, $endpoints{$ep}{total}/$endpoints{$ep}{count};
}
printf "Overall avg: %.1fms\n", $total/$count;
# perl log_demo.plPerl object-oriented programming with bless and inheritance.
#!/usr/bin/perl
use strict; use warnings; use feature 'say';
package Animal;
sub new { my ($class,%a) = @_; bless {name=>$a{name},sound=>$a{sound}//'...'}, $class }
sub name { $_[0]->{name} }
sub sound { $_[0]->{sound} }
sub speak { say $_[0]->name . " says: " . $_[0]->sound }
package Dog;
our @ISA = ('Animal');
sub new { my ($class,%a) = @_; $a{sound}='Woof!'; $class->SUPER::new(%a) }
sub fetch { say $_[0]->name . " fetches the ball!" }
package Cat;
our @ISA = ('Animal');
sub new { my ($class,%a) = @_; $a{sound}='Meow!'; $class->SUPER::new(%a) }
package main;
my @animals = (Dog->new(name=>'Rex'),Cat->new(name=>'Luna'),Dog->new(name=>'Max'));
$_->speak for @animals;
for my $a (@animals) {
$a->fetch if $a->isa('Dog');
say $a->name . " purrs..." if $a->isa('Cat');
}
# perl oop_demo.plThe original web technology — Perl CGI.
#!/usr/bin/perl
use strict; use warnings;
use CGI;
my $q = CGI->new;
my $name = $q->param('name') // 'World';
$name =~ s/[<>&"']//g; # basic sanitization
my $color = $q->param('color') // 'steelblue';
$color =~ s/[^a-zA-Z#0-9]//g;
print $q->header('text/html; charset=UTF-8');
print <<HTML;
<!DOCTYPE html>
<html><head><title>Perl CGI | MyWebUniversity</title></head>
<body style="font-family:Arial;max-width:600px;margin:40px auto;background:#0f172a;color:#e5e7eb">
<h1 style="color:$color">Hello, $name!</h1>
<p>Perl $] running as CGI on MyWebUniversity.com</p>
<form method="get">
Name: <input name="name" value="$name"><br><br>
Color: <input name="color" value="$color"><br><br>
<input type="submit" value="Update">
</form>
</body></html>
HTML
# Deploy: chmod 755 hello.cgi → /usr/lib/cgi-bin/hello.cgi
# Access: https://yoursite.com/cgi-bin/hello.cgi?name=Alice&color=greenCommand-line text processing — replace awk and sed.
#!/bin/bash
# Perl one-liners — the original DevOps tool
# Print with line numbers
perl -ne 'printf "%4d: %s", $., $_' file.txt
# Sum a column of numbers
echo -e "10\n20\n30\n40" | perl -ne '$s+=$_; END{print "Sum: $s\n"}'
# Extract all email addresses
perl -nE 'say for /[\w.+-]+@[\w-]+\.[\w.]+/g' document.txt
# Replace in-place (like sed -i)
perl -pi -e 's/foo/bar/g' myfile.txt
# Print lines matching pattern
perl -ne 'print if /ERROR/' server.log
# Print lines NOT matching pattern
perl -ne 'print unless /DEBUG/' server.log
# Count words (like wc -w)
perl -ne '$c += scalar split; END{print "$c words\n"}' file.txt
# Print unique lines preserving order
perl -ne 'print unless $seen{$_}++' file.txt
# CSV: print column 2
perl -F, -lane 'print $F[1]' data.csv
# JSON: extract a field (requires JSON module)
echo '{"name":"Alice","score":95}' | perl -MJSON -ne '$d=decode_json($_);print $d->{name},"\n"'