Program Structure
Understand how Perl programs are structured: statements and semicolons, comments, basic script layout, and using print to produce output.
Introduction
Before diving into variables and data types, it helps to understand the basic anatomy of a Perl program: how statements are separated, how comments work, and how output is typically organized. These building blocks will appear in literally every script you write from here on.
- How Perl statements are separated with semicolons.
- How whitespace and formatting work in Perl.
- How to write single-line and multi-line comments.
- The typical top-to-bottom layout of a Perl script.
- How print works, including printing multiple values.
Statements and Semicolons
A Perl program is a sequence of statements, and every simple statement must end with a semicolon (;). The semicolon is what tells the interpreter "this instruction is complete, move to the next one" — Perl does not use line breaks to separate statements the way some other languages do.
use strict;use warnings;
my $first = "one statement";my $second = "another statement"; my $third = "yet another"; # multiple statements, one line
print "$first\n$second\n$third\n";Click Run to see what this code prints.
Notice that $second and $third are declared on the same physical line, separated only by semicolons. This is legal Perl, though for readability most style guides recommend one statement per line, as shown for $first.
Whitespace and Formatting
Perl is generally whitespace-insensitive between tokens: extra spaces, tabs, and blank lines are ignored by the interpreter and exist purely for human readability. Unlike Python, indentation carries no functional meaning in Perl — code blocks are instead delimited by curly braces { }, which you will see starting with the conditional statements lesson right after this course.
Even though whitespace is not functionally required, consistent indentation (commonly four spaces per level) makes Perl code dramatically easier to read, especially as scripts grow larger.
Comments
Comments are notes in your source code that Perl ignores completely when running the script. They exist purely to help humans (including your future self) understand the code.
A single-line comment starts with a # character and continues to the end of that line.
use strict;use warnings;
# This calculates a simple discount pricemy $price = 50; # original price in dollarsmy $discount = 0.20; # 20% off
my $final_price = $price - ($price * $discount);print "Final price: \$$final_price\n";Click Run to see what this code prints.
For multi-line comments, Perl has no dedicated block-comment syntax like /* */, but the common convention is Plain Old Documentation (POD) markers, which are also used for real documentation.
=begin commentThis entire block, from =begin to =end,is ignored by the Perl interpreter.Useful for temporarily disabling a large chunk of code.=end comment=cutIn practice, many Perl developers simply prefix each line of a multi-line explanation with # rather than using POD markers, since it is more familiar to programmers coming from other languages.
Typical Script Layout
While Perl does not enforce a strict structure, professional scripts tend to follow a consistent, predictable order.
- Shebang line (Linux/macOS): #!/usr/bin/perl or #!/usr/bin/env perl
- Pragmas: use strict; and use warnings;
- Module imports: any use Some::Module; lines needed by the script
- Variable declarations and constants
- Subroutine (function) definitions, if any
- The main body of logic that actually runs
#!/usr/bin/perluse strict;use warnings;
# --- Constants / configuration ---my $greeting = "Hello";
# --- Subroutine definitions ---sub build_message { my ($name) = @_; return "$greeting, $name!";}
# --- Main program logic ---my $message = build_message("Perl Learner");print "$message\n";Click Run to see what this code prints.
We will cover subroutines like build_message in depth in a later lesson — for now, just notice how the file reads top to bottom in a predictable order: setup, definitions, then execution.
The print Function in Detail
print is the workhorse of Perl output. It can take a single value, multiple comma-separated values, or a list, and prints them one after another with no automatic separator or trailing newline unless you add one yourself.
use strict;use warnings;
my $name = "Perl";my $year = 1987;
print "Language: ", $name, ", Created: ", $year, "\n";print "Language: $name, Created: $year\n";Click Run to see what this code prints.
Both lines above print the same result. The first passes several separate comma-separated arguments to print; the second relies on string interpolation, where variable names embedded directly inside a double-quoted string are automatically replaced with their values. We will explore interpolation fully in the next lesson on scalars.
Common Mistakes
- Forgetting a semicolon, especially at the end of the last statement in a block.
- Assuming print automatically adds a newline — it does not; you must include \n yourself (or use say, covered later).
- Relying on indentation for logic the way Python does — Perl ignores indentation entirely; only { } braces define blocks.
- Leaving commented-out debugging code scattered throughout a script instead of removing it once it is no longer needed.
Best Practices
- Write one statement per line for readability, even though multiple statements can share a line.
- Use consistent indentation (commonly four spaces) even though Perl does not require it.
- Comment on why code does something non-obvious, not just what it does — the code itself already shows what.
- Keep a predictable file layout: pragmas, imports, constants, subroutines, then main logic.
Frequently Asked Questions
No. Perl code blocks are always delimited with curly braces { }. Indentation is purely for human readability and has zero effect on how the code executes.
Yes, as long as each is separated by a semicolon, but it is generally discouraged for readability except in very short scripts or one-liners.
Not a dedicated one like /* */ in C-like languages, but the POD =begin/=end/=cut markers achieve the same effect and are the closest standard equivalent.
Key Takeaways
- Every Perl statement must end with a semicolon.
- Whitespace and indentation are for readability only; Perl uses { } to define code blocks.
- Single-line comments start with #; POD markers can create multi-line comments.
- A typical script layout flows from pragmas and imports down to main program logic.
- print outputs one or more comma-separated values with no automatic newline.
Summary
You now understand the basic shape of a Perl program. The next lesson dives into scalars — the fundamental variable type in Perl, marked with the $ sigil — where you will learn how Perl stores and interpolates individual values.