Beginner

Hello World in Perl

Your first Perl program - the classic Hello World example with Docker setup

Perl has been printing “Hello, World!” since 1987. As a language designed for practical text manipulation, outputting text is as simple as it gets.

The Code

Create a file named hello.pl:

1
print "Hello, World!\n";

One line. That’s all you need.

Understanding the Code

  • print - The built-in function to output text
  • "Hello, World!\n" - A double-quoted string with an escape sequence
  • \n - The newline character (required for proper output formatting)
  • ; - Statement terminator (required in Perl)

Running with Docker

The easiest way to run Perl without installing it locally:

1
2
3
4
5
# Pull the official Perl image
docker pull perl:5.40-slim

# Run the program
docker run --rm -v $(pwd):/app -w /app perl:5.40-slim perl hello.pl

Running Locally

If you have Perl installed (it comes pre-installed on most Unix/Linux/macOS systems):

1
2
3
4
5
6
# Run with Perl interpreter
perl hello.pl

# Or make it executable and run directly
chmod +x hello.pl
./hello.pl  # (requires shebang line - see below)

Expected Output

Hello, World!

The Shebang Line

To run a Perl script directly as an executable, add a shebang line at the top:

1
2
#!/usr/bin/perl
print "Hello, World!\n";

Or for better portability:

1
2
#!/usr/bin/env perl
print "Hello, World!\n";

Modern Alternative: say

Perl 5.10+ includes the say function, which automatically adds a newline:

1
2
use feature 'say';
say "Hello, World!";

Or enable all modern features:

1
2
use v5.10;
say "Hello, World!";

One-Liner Style

Perl is famous for one-liners. You can run Hello World without even creating a file:

1
perl -e 'print "Hello, World!\n"'

Or with say:

1
perl -E 'say "Hello, World!"'

The -e flag executes code from the command line, while -E also enables modern features like say.

Single vs. Double Quotes

In Perl, quote type matters:

1
2
3
4
5
# Double quotes: interpolation and escape sequences
print "Hello, World!\n";      # Works: prints with newline

# Single quotes: literal string
print 'Hello, World!\n';      # Prints: Hello, World!\n (literally)

Double quotes interpret escape sequences (\n, \t, etc.) and variable interpolation. Single quotes treat everything literally.

Key Concepts

  1. Perl is interpreted - No compilation step; the interpreter reads and executes source directly
  2. Dynamic typing - Variables can hold any type of data
  3. Context-sensitive - Same expression can behave differently in different contexts
  4. TIMTOWTDI - “There Is More Than One Way To Do It” is Perl’s philosophy
  5. Practical extraction - Perl excels at text processing and pattern matching

Interactive Mode

Perl has a debugger that can work as a simple REPL:

1
docker run --rm -it perl:5.40-slim perl -de 0

Then type:

1
2
3
DB<1> print "Hello, World!\n"
Hello, World!
DB<2> q

For a better REPL experience, you can install Reply:

1
2
docker run --rm -it perl:5.40-slim cpan Reply
# Then run: reply

A Slightly Longer Example

Here’s a preview of what’s coming in future tutorials:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;

# Variables use sigils: $ for scalars
my $name = "Perl";
my $year = 1987;

# String interpolation in double quotes
say "Hello from $name!";
say "Created in $year";

# Arrays use @ sigil
my @languages = ("Perl", "Python", "Ruby");
say "First language: $languages[0]";

# Hashes use % sigil
my %info = (creator => "Larry Wall", type => "scripting");
say "Creator: $info{creator}";

Next Steps

Continue to Variables and Data Types to learn about Perl’s sigils, scalars, arrays, and hashes.

Running Today

All examples can be run using Docker:

docker pull perl:5.40-slim
Last updated: