Perl Cheat Sheet
Perl syntax, regular expressions, arrays, hashes, and text-processing idioms for scripting and command-line pattern-matching tasks.
1 PageIntermediateApr 10, 2026
Basic Syntax
Variables, control flow, and printing.
perl
#!/usr/bin/perluse strict;use warnings;my $age = 30;my $name = "Ada";my $pi = 3.14159;if ($age >= 18) { print "$name is an adult\n";}for (my $i = 0; $i < 5; $i++) { print "Count: $i\n";}
Regular Expressions
Pattern matching and substitution.
perl
my $text = "Contact: [email protected]";if ($text =~ /(\w+)@(\w+)\.(\w+)/) { print "User: $1, Domain: $2.$3\n";}(my $clean = $text) =~ s/[^a-zA-Z ]//g; # substitutionmy @words = split /\s+/, $text; # split on whitespacemy $joined = join(", ", @words);
Special Variables
Built-in variables used implicitly.
- $_- default variable used implicitly by many operators (e.g. print, m//)
- @_- array of arguments passed into a subroutine
- $0- name of the currently executing script
- @ARGV- command-line arguments passed to the script
- %ENV- hash of environment variables
- $@- error message from the last eval block
Hashes & Arrays
Perl's core aggregate data types.
perl
my @nums = (5, 3, 1, 4, 2);my @sorted = sort { $a <=> $b } @nums; # numeric sortmy %ages = (Alice => 30, Bob => 25);$ages{Carol} = 28;for my $key (keys %ages) { print "$key is $ages{$key}\n";}
References & Data Structures
Building nested structures with references.
perl
my @arr = (1, 2, 3);my $aref = \@arr; # array referencemy %h = (a => 1, b => 2);my $href = \%h; # hash referenceprint $aref->[0]; # 1print $href->{a}; # 1# Anonymous nested structuremy $data = { users => [ { name => 'Al' }, { name => 'Bo' } ],};print $data->{users}[1]{name}; # Bo
Subroutines & Arguments
Defining subs and unpacking @_.
perl
sub greet { my ($name, $greeting) = @_; $greeting //= 'Hello'; // default return "$greeting, $name!";}print greet('World'); # Hello, World!print greet('Al', 'Hi'); # Hi, Al!# Pass arrays/hashes by referencesub total { my $ref = shift; my $s = 0; $s += $_ for @$ref; $s }print total(\@list);
File I/O
Reading and writing files with lexical filehandles.
perl
open(my $fh, '<', 'input.txt') or die "Can't open: $!";while (my $line = <$fh>) { chomp $line; print "$line\n";}close($fh);# Slurp all linesopen(my $in, '<', 'input.txt') or die $!;my @lines = <$in>;close($in);open(my $out, '>', 'output.txt') or die $!;print $out "result\n";close($out);
Common Built-in Functions
Frequently used list and string functions.
- map { } @list- transform each element, returning a new list
- grep { } @list- filter elements matching a condition
- sort { $a <=> $b }- numeric sort; omit block for string sort
- join($sep, @l)- concatenate list into a string with separator
- split /re/, $str- break a string into a list by regex
- sprintf(fmt, ...)- format a string like printf, returning it
- wantarray- true in list context, false in scalar context
Pro Tip
Always start scripts with `use strict; use warnings;` — they catch typos and unsafe symbolic references that would otherwise fail silently.
Was this cheat sheet helpful?
Explore Topics
#Perl#PerlCheatSheet#Programming#Intermediate#BasicSyntax#RegularExpressions#SpecialVariables#HashesArrays#DataStructures#CommandLine#CheatSheet#SkillVeris