320 x 50 Mobile Ad
Cheat Sheets

Perl Cheat Sheet

Transform, format, and optimize your data instantly with this free client-side tool.

Basics

my $var = 'value';

Declare a scalar variable

my @arr = (1, 2, 3);

Declare an array

my %hash = ('a' => 1, 'b' => 2);

Declare a hash

print "Hello\n";

Print to standard output

say "Hello";

Print with automatic newline (needs 'use feature "say"')

use strict;

Enforce strict scoping, variables declaration

use warnings;

Enable optional warnings

Scalars (Strings & Numbers)

my $len = length($str);

Get string length

$str1 . $str2

String concatenation

uc($str); lc($str);

Convert string to uppercase / lowercase

substr($str, 0, 5)

Extract substring

chomp($str);

Remove trailing newline

Arrays

my $size = @arr;

Get array length (scalar context)

$arr[0]

Access array element

push(@arr, $val);

Appends element to end of array

my $val = pop(@arr);

Removes and returns last element

unshift(@arr, $val);

Prepends element to beginning

my $val = shift(@arr);

Removes and returns first element

my @sorted = sort @arr;

Sort array (string comparison)

my @num_sorted = sort { $a <=> $b } @arr;

Sort array numerically

my $str = join(':', @arr);

Join array elements into string

Hashes

$hash{'key'}

Access hash value

my @keys = keys %hash;

Get all keys of a hash

my @values = values %hash;

Get all values of a hash

exists $hash{'key'}

Check if hash key exists

delete $hash{'key'};

Remove key/value pair from hash

Control Structures

if ($x == 5) { ... }

If statement (numeric comparison)

if ($s eq "abc") { ... }

If statement (string comparison)

elsif ($x > 5) { ... }

Else If

unless ($x == 5) { ... }

Execute block if condition is FALSE

for my $i (1..10) { ... }

For loop over range

foreach my $item (@arr) { ... }

Iterate over an array

while (my $line = <$fh>) { ... }

Read filehandle line by line

Regular Expressions

if ($str =~ /pattern/) { ... }

Match regex pattern

if ($str !~ /pattern/) { ... }

Does not match pattern

$str =~ s/foo/bar/g;

Search and replace globally

my @parts = split(/,/, $str);

Split string by regex

Subroutines

sub myfunc { my ($arg1, $arg2) = @_; return $arg1 + $arg2; }

Define a subroutine

myfunc($x, $y);

Call a subroutine

File I/O

open(my $fh, '<', 'file.txt') or die "Could not open: $!";

Open file for reading

open(my $fh, '>', 'file.txt');

Open file for writing

open(my $fh, '>>', 'file.txt');

Open file for appending

my $content = <$fh>;

Read exactly one line

my @lines = <$fh>;

Slurp all lines into an array

print $fh "Some text\n";

Write to filehandle

close $fh;

Close filehandle

Advanced

Advanced Command Snippet

Explore advanced configurations for Perl

System Optimization

Performance tuning best practices for Perl

Security Audit

Run security checks and validation for Perl

320 x 50 Mobile Ad

Frequently Asked Questions

Got questions? We've got answers.

The Perl Cheatsheet is a quick-reference guide that provides developers with the most essential and frequently used Perl commands, syntax, and snippets in one centralized place.
This reference is built for both beginners who are just learning Perl and need a quick syntax lookup, as well as seasoned professionals who need to jog their memory on complex commands.
You can use the real-time search bar at the top of the cheatsheet. Simply type a keyword (like &#39;delete&#39; or &#39;file&#39;) and the list will instantly filter to show only matching Perl commands.
Yes! Every command block features a one-click copy button. Just hover over the command and click the copy icon to instantly send the snippet to your clipboard.
The cheatsheet is divided into logical categories such as Basics, Network, Operations, and specific Perl features. You can click on the category filters at the top to isolate specific groups of commands.
Absolutely. This Perl reference guide is 100% free, requiring no sign-ups or subscriptions, and is always available when you need it.
While these are standard Perl operations, you should always understand what a command does before running it, especially if it involves system operations or destructive actions.
Yes, we have recently expanded this cheatsheet to include advanced snippets, best practices, and edge-case syntax that go beyond basic introductory commands.
ADVERTISEMENT
Boost Your Business Online