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:
| |
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:
| |
Running Locally
If you have Perl installed (it comes pre-installed on most Unix/Linux/macOS systems):
| |
Expected Output
Hello, World!
The Shebang Line
To run a Perl script directly as an executable, add a shebang line at the top:
| |
Or for better portability:
| |
Modern Alternative: say
Perl 5.10+ includes the say function, which automatically adds a newline:
| |
Or enable all modern features:
| |
One-Liner Style
Perl is famous for one-liners. You can run Hello World without even creating a file:
| |
Or with say:
| |
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:
| |
Double quotes interpret escape sequences (\n, \t, etc.) and variable interpolation. Single quotes treat everything literally.
Key Concepts
- Perl is interpreted - No compilation step; the interpreter reads and executes source directly
- Dynamic typing - Variables can hold any type of data
- Context-sensitive - Same expression can behave differently in different contexts
- TIMTOWTDI - “There Is More Than One Way To Do It” is Perl’s philosophy
- Practical extraction - Perl excels at text processing and pattern matching
Interactive Mode
Perl has a debugger that can work as a simple REPL:
| |
Then type:
| |
For a better REPL experience, you can install Reply:
| |
A Slightly Longer Example
Here’s a preview of what’s coming in future tutorials:
| |
Next Steps
Continue to Variables and Data Types to learn about Perl’s sigils, scalars, arrays, and hashes.