TExcelTAD
Home › Perl › Perl Syntax

Perl Syntax

A Perl program is a sequence of statements. Each statement ends with a semicolon ;, and whitespace (spaces, tabs, newlines) is mostly ignored, so you can lay code out the way that reads best.

perl · try it yourself
use strict;
use warnings;

print "One statement\n";
print "Two statements on ", "one call\n";
print
    "A statement can span ",
    "several lines\n";
One statement
Two statements on one call
A statement can span several lines

Comments

A # starts a comment that runs to the end of the line. Perl has no special block-comment syntax; for long notes, programmers use several # lines or POD (Plain Old Documentation), which the interpreter skips:

perl · try it yourself
use strict;
use warnings;

# This whole line is a comment
my $price = 20;    # a comment after code
my $qty   = 3;

=pod

Everything between =pod (or any =word at the start of a line)
and =cut is documentation. perl ignores it; perldoc renders it.

=cut

print "Total: ", $price * $qty, "\n";
Total: 60

Sigils: the shape of a variable

Perl variable names begin with a sigil that tells you what kind of thing you are looking at:

Sigil Type Example Holds
$ scalar $count one value: a number, a string or a reference
@ array @names an ordered list of scalars
% hash %age_of key/value pairs
& subroutine &greet code (rarely written with the sigil today)

Names are case-sensitive and may contain letters, digits and underscores, but cannot start with a digit. $total, $Total and $TOTAL are three different variables.

perl · try it yourself
use strict;
use warnings;

my $city   = "Lisbon";                 # scalar
my @colors = ("red", "green", "blue"); # array
my %capital = (France => "Paris", Japan => "Tokyo"); # hash

print "City: $city\n";
print "First color: $colors[0]\n";
print "Capital of Japan: $capital{Japan}\n";
City: Lisbon
First color: red
Capital of Japan: Tokyo

Notice that a single element of @colors is written $colors[0]: the sigil describes what you get back (one scalar), not the container it lives in. This trips up nearly every newcomer, so it is worth remembering early.

Blocks

Curly braces { ... } group statements into a block. Blocks are used by if, loops and subroutines, and they also create a new scope for my variables. Unlike C or Java, the braces are required even when the block holds a single statement.

perl · try it yourself
use strict;
use warnings;

my $temperature = 31;
if ($temperature > 30) {
    print "It's hot today.\n";
}

# Statement modifiers: a postfix form for single statements
print "Remember sunscreen.\n" if $temperature > 25;
It's hot today.
Remember sunscreen.

The second form, STATEMENT if CONDITION;, is a statement modifier. It reads like English and is idiomatic for short, simple conditions.

Parentheses on function calls

Built-in functions do not need parentheses around their arguments: print "hi" and print("hi") are the same. Use parentheses whenever they make precedence clearer.

Tip: A famous gotcha: print (1 + 2) * 3; prints 3, because Perl reads print (1 + 2) as the complete function call and then multiplies its return value by 3. With use warnings you get a hint about it. Write print((1 + 2) * 3) or print 3 * (1 + 2) instead.

Test yourself

Which sigil is used for a hash?

  1. $
  2. @
  3. %
  4. &

How do you access the third element of @items?

  1. @items[3]
  2. $items[2]
  3. @items(2)
  4. $items{2}

Which line is a valid statement modifier?

  1. if ($x) print "yes";
  2. print "yes" if $x;
  3. if $x { print "yes" }
  4. print "yes" when $x;