Home / Alt manpages / perlcheat(1)

  • perlcheat(1)
  • User command
  • linux

Use perlcheat to Find the Right Perl Syntax Quickly

You will use the installed perlcheat reference to answer small Perl syntax questions without guessing, then test a copied pattern in a temporary one-liner. This guide follows Perl 5.38.2 and the perl-doc package version 5.38.2-3.2ubuntu0.6 on this machine. Allow about 15 minutes. You need a shell, Perl, and permission to read the local manual pages. Nothing here needs elevated privileges or changes a system file.

1. Open the local cheat sheet

perlcheat is a documentation page, not a separate executable on this installation. Open it with either of the following:

$ man perlcheat
$ perldoc perlcheat

The page describes itself as a compact reference for beginning Perl programmers, and groups related syntax into a single sheet. The local manual is the primary reference here. If one of the commands fails, check the package before trying random alternatives:

$ dpkg-query -W -f='${Package} ${Version}\n' perl-doc perl-base
perl-base 5.38.2-3.2ubuntu0.6
perl-doc 5.38.2-3.2ubuntu0.6

Checkpoint: you should be able to see the headings CONTEXTS, SIGILS, REGEX MODIFIERS, and SPECIAL VARIABLES. The sheet is deliberately dense. Use it to locate a concept, then read the relevant Perl documentation before putting unfamiliar syntax into production.

2. Read sigils and references from left to right

The first useful distinction is the sigil, the leading character that tells Perl what kind of value is being accessed. The sheet maps $ to a scalar, @ to an array, and % to a hash. A reference adds another layer: -> dereferences an array or hash reference in the readable form shown by the manual.

my $person = {
    name   => 'Ada',
    skills => ['Perl', 'Unix'],
};

print $person->{name}, "\n";
print $person->{skills}[0], "\n";

Run it without creating a source file:

$ perl -e 'my $x = {name => "Ada", skills => ["Perl", "Unix"]}; print $x->{name}, "\n", $x->{skills}[0], "\n"'
Ada
Perl

Do not confuse $array[0] with @array[0, 2]. The former retrieves one scalar element. The latter is an array slice and returns a list. The same distinction applies to hashes: $hash{'a'} retrieves one value, while @hash{'a', 'b'} retrieves multiple values.

3. Check context before choosing a return value

perlcheat separates void, scalar, and list contexts because the same expression can behave differently in each. A function that returns a count in scalar context may return all matching items in list context. When a result looks surprising, first ask which context the surrounding code supplies, rather than changing punctuation at random.

my @items = ('red', 'blue', 'red');
my $count = @items;
my @copy  = @items;

print "$count\n";
print join(',', @copy), "\n";
$ perl -e 'my @items = ("red", "blue", "red"); my $count = @items; my @copy = @items; print "$count\n", join(",", @copy), "\n"'
3
red,blue,red

Checkpoint: when consulting the sheet, identify both the sigil and the context. This catches many errors involving arrays, hashes, function return lists, and references.

4. Pick numeric or string operators deliberately

The sheet places numeric and string comparisons side by side. Use == and != for numeric values. Use eq and ne for strings. The concatenation operator is ., while numeric addition is +. Perl's false values include undef, the empty string, 0, and the string "0"; most other values are true.

$ perl -e 'my ($a, $b) = ("10", "010"); print(($a == $b ? "same number" : "different number"), "\n"); print(($a eq $b ? "same text" : "different text"), "\n")'
same number
different text

That difference is intentional. If input came from a file or command line, decide whether its meaning is textual or numeric before comparing it. The cheat sheet also shows operator precedence, but parentheses are usually clearer when a condition mixes arithmetic, comparisons, and boolean operators.

5. Use the regex table as a map, not a full tutorial

The regex section gives the common modifiers and metacharacters. For example, ^ anchors a match at the start, $ at the end, \d matches a digit, and parentheses capture text. The /i modifier makes matching case-insensitive, while /g finds successive matches.

$ perl -e '"error: 404" =~ /^(\w+):\s+(\d+)$/ and print "$1/$2\n"'
error/404

Read the pattern in pieces: a word, a colon, whitespace, digits, and the end of the string. If you need to handle user input, test cases containing newlines, Unicode, empty strings, and unexpected punctuation. A compact reference cannot explain every interaction between modifiers, captures, backtracking, and Unicode rules.

6. Look up special variables and configuration

The bottom of the sheet is useful when an old script contains punctuation-heavy variables. It lists $_ as the default variable, @ARGV as command-line arguments, %ENV as the environment, $! for system or library-call errors, and $@ for an eval error. The configuration example perl -V:ivsize prints the size of Perl's integer type on the current build.

$ perl -V:ivsize
ivsize='8';

Use the corresponding full manual page when the variable affects error handling, tainted data, signals, locales, Unicode, or security. Treat values from %ENV and @ARGV as untrusted input. The cheat sheet helps you recognise the syntax; it does not make shell execution, file writes, or dynamic evaluation safe.

7. Keep experiments reversible

Prefer perl -e for a short read-only check. If an experiment will write files, run it in a temporary directory and use a new output name. Do not paste a command containing unlink, shell backticks, system, or a redirection into a production directory until you have inspected it. No elevated privilege is needed to learn syntax, and adding sudo only increases the possible damage from a mistake.

If a one-liner is becoming difficult to review, stop and put it in a temporary script instead. Keep the original input, record the Perl version with perl -v, and remove only the temporary files after checking the result. There is no system change to undo in the examples above.

Done means

  • You can open the installed reference with man perlcheat or perldoc perlcheat.
  • You can distinguish scalar, array, hash, slice, and reference syntax.
  • You check context and choose numeric or string operators intentionally.
  • You can use the regex table to locate anchors, classes, captures, and modifiers.
  • You verify unfamiliar examples with a small, unprivileged Perl command before relying on them.