Perl Cheatsheet
Basics
Use this Perl reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Script Skeleton
#!/usr/bin/env perl use v5.36; say "Hello, Perl";
use v5.36; (or newer, like use v5.40;) enables strict, warnings, say, and subroutine signatures in one line and turns off legacy misfeatures. It is the recommended opener for new code. The pre-2022 skeleton is still common in older codebases:
#!/usr/bin/env perl use strict; use warnings; use feature qw(say);
Running Perl
perl script.pl # run a file perl -c script.pl # syntax check perl -w script.pl # warnings from command line perl -E 'say 2 + 2' # one-liner with modern features perl -ne 'print if /error/' app.log # loop over input lines perldoc perlintro # local Perl documentation perldoc -f chomp # docs for a built-in function
Variables and Sigils
my $scalar = "one value"; # scalar: string, number, reference, undef my @array = (1, 2, 3); # ordered list my %hash = (a => 1); # key-value map print $array[0]; # one array element is scalar, so $ print $hash{a}; # one hash value is scalar, so $
Safety Defaults
use v5.36; # implies strict + warnings (and more) use strict; # explicit form: require declarations use warnings; # report suspicious behavior my $x = 10; # lexical variable our $VERSION = '1.0'; # package global, rare in scripts
Output
print "no automatic newline"; print "line\n"; say "line with newline"; # enabled by use v5.36 (or use feature 'say') printf "%s scored %.1f\n", "Ada", 98.5;
Truthiness
# false values: undef, 0, "0", "" (empty string) # true values: almost everything else, including "00" and [] refs if ($value) { print "true\n"; }
Comments and Statements