Perl Syntax Overview and Reference Sheet

perl operatorsPerl is a family of dynamic programming languages first designed by Larry Wall in 1987. It’s an easygoing, high-level language that doesn’t enforce specific programming paradigms and allows the user to sidestep some of its actual rules. This is reflected strongly in the language’s syntax, which reflects an idea Wall stressed in an article he wrote for Linux Journal in 1997, “One of the ideas I keep stressing in the design of Perl is that things that are different should look different.”

In this guide, we’ll go over some basic Perl syntax structures that stand out, and provide readers with a host of resources, such as this Perl programming tutorial for beginners.

Perl Syntax: Basic

Perl syntax is borrowed largely from other programming languages, such as C, awk, and Lisp, but it also deviates strongly from some of their aspects as well. Before you start with Perl, you might want to learn some coding basics in this introductory course.


One thing that stands out about its syntax is that it’s free-form. This means formatting and indentation don’t matter as it does in a language like Python, which requires what is sometimes referred to as an off-side rule. In Python, whitespace indentation is used to delimit blocks of code. Perl, on the other hand, requires no whitespace, and the user is free to format as they wish.

For instance, if you wanted to print Hello, World, you could write something like this:


print "Hello, World\n";

Or you could write something like this:


print                      "Hello, World\n";

Either way, you will produce the following result:

Hello, World

If you put the spaces inside the actual string, between the quotation marks, that will carry over into the code. When it comes to the actual syntax, though, whitespace matters not.

Check out this programming for non-programmers course to learn more fundamental coding concepts like indentation.


Like most programming languages, Perl includes a comment feature, which allows the coder to input useful information within the code that is for their eyes only, and which will be skipped over by the interpreter when it comes time to build. Comments in Perl are denoted with a hashtag symbol.

# A comment in Perl looks like this.

You can also use an equal sign to insert a pod, or “plain old documentation,” which can be embedded in multiple lines. The entire section will be ignored by the compiler, but must be cut off with a =cut in order to indicate the end of the embedded document.

=begin comment
This is how a pod looks.
This will be ignored by the compiler
and can take up as many lines as you want.

If we code something like this:


# blah blah blah

print "I'm coding in Perl\n";

=begin comment
blah blah

The only thing it will produce is this:

I'm coding in Perl

Check out this Perl programming course for beginners to learn more.

Quotation Marks

Double and single quotes are allowed in Perl, but they operate differently. Double quotes interpolate special characters inside them, while single quotes do not. This works for variables as well, which is very important to remember.

What this means is that double quotes will take any special character or variable inside a string and replace it with its value, indicated by your code. Single quotes will only depict the symbol, literally, as it appears inside it.

$money = 5;
print "I have $money dollars\n";
print 'I have $money dollars\n';

What do you think the above code will print? You guessed it. The string in the double quotes will print the sentence with the $money variable replaced by that variable’s actual value of 5, while the string in the single quotes will just print the string as it reads in the code.

I have 5 dollars
I have $money dollars\n$

You’ll notice, too, that the string in the single quotes can’t even interpolate the newline character \n, and prints it as it reads. Learn more about Perl strings in this guide.

Perl Syntax: Functions

Below is a list of Perl syntax rules and examples for function-related code. For most of this, your best bet to learning it is by actually using it. Check out this course on applying your coding skills with Perl 5 to get started.

Function call (with no parameter)

  • f
  • f()
  • &$f
  • $f->()

Function call (with parameters)

  • f(a,b,c,…)
  • &$f(a,b,c,…)
  • $f->(a,b,c,…)

Anonymous Function

  • sub { my ($a, $b, $c) = @_; … }
  • { my ($a, $b, $c) = @_; … }

Function definition

  • sub f { … }

Function definition (with variable argument number)

  • sub f { … @_ }

Perl Syntax: Control Flow

Below you’ll find Perl syntax information for control flow-related code such as loops, if then, and other conditional statements.

If Then Statements

  • if (c) {…}
  • … if c
  • c and …
  • c and …

If Then Else Statements

  • if (c) {b1} elsif (c2) {b2} else {b3}
  • c ? b1 : b2

Loop (Do-Until)

  • do {…} until c

Loop (While-Do)

  • while (c) …

Perl Syntax: Boolean Operators

A boolean is a data type that can have only one of two values: true or false. That isn’t a question, either! A boolean can only be true or false, and in Perl it’s no different. Only the syntax differs.

Logical Or/And

  • ||
  • &&
  • or
  • and

Logical Not

  • !
  • not

Perl Data Structures and References

Anonymous Array

  • $aref = [1,”­foo­”­,un­def­,13];

Reference to array

  • $aref = \@a;

Reference to hash

  • $href = \%h;

Copy array

  • $aref2 = [@{$ar­ef1}];

Copy hash

  • $href2 = {%{$hr­ef1}};

2-dim array

  • @a = ([1, 2],[3, 4]);

Hash of arrays

  • %HoA=(­fs=­>[“f­”­,”b”], sp=>[“h­”­,”m”]);

Access to hash of arrays

  • $name = $HoA{s­p}[1];

You can find a full list of Perl syntax pointers on this Perl reference sheet website, which explains the syntax of variable types such as scalars and strings, arrays and lists, hashes, system interactions and debugging, and a whole lot more. For a more i-depth Perl tutorial, check out this Perl Regex course, or take a course on breaking into the business of programming to see if the job is right for you.

If Perl doesn’t seem to be right for you, consider learning a language like Python instead with this course.