Getting Started

Perl Fundamentals — Getting Started
Perl Fundamentals
Course 1 · Chapter 1 · Getting Started

🐪 Getting Started

Welcome to Perl Fundamentals. Perl grew directly out of the same territory as Bash, sed, awk, and regular expressions — Larry Wall built it specifically because shell scripting and text-processing tools were hitting their limits. This chapter covers installing Perl, running a script, and the two pragmas that mark the difference between idiomatic Perl and the "write-only code" reputation the language is unfairly stuck with.

📥 Installing Perl

Most Linux and macOS systems already have Perl installed — check with:

perl -v

On Windows, Strawberry Perl is the standard distribution — a complete, self-contained installer including cpanm (Chapter-9-of-Course-2's package installer) out of the box.

▶️ Running a Perl Script

perl script.pl

On Linux/macOS, a script can also be made directly executable, using a shebang line as its first line:

#!/usr/bin/env perl use strict; use warnings; print "Hello, World!\n";
chmod +x script.pl ./script.pl

#!/usr/bin/env perl is preferred over a hardcoded path like #!/usr/bin/perlenv finds whichever perl is first on the system's PATH, working correctly across different installations rather than assuming one fixed location.

🛡️ use strict; use warnings; — Non-Negotiable Culture

Perl is famously permissive by default — variables can be used without being declared, typos in variable names silently create a new variable rather than raising an error, and genuinely dangerous constructs are allowed without complaint. This permissiveness is exactly where Perl's "write-only code" reputation comes from. The fix, and the actual convention in every real Perl codebase, is two lines at the top of every script:

use strict; # requires variables to be declared before use use warnings; # surfaces likely-bug situations (undefined values, etc.)
⚠ Without strict, a Typo Becomes a New Variable — Silently
# no "use strict" — this typo compiles and runs with NO error: $user_name = "Ada"; print $usr_name; # typo — prints nothing, since $usr_name is a brand new, empty variable

With use strict; in place, that same typo raises Global symbol "$usr_name" requires explicit package name at compile time — before the script ever runs — turning a silent, confusing bug into an immediate, precise error pointing at the exact line. There is essentially no legitimate reason to omit use strict; use warnings; from a real script.

🖨️ print and say

print "Hello, World!\n"; # no automatic newline — you add \n yourself use feature 'say'; say "Hello, World!"; # say adds the newline automatically

print is the original, universal output function — but unlike console.log or Python's print(), it does not add a trailing newline; you write \n explicitly. say (available via use feature 'say';, or automatically with use v5.10; or later) behaves like print but appends the newline for you — the more ergonomic default for everyday scripts.

💬 Comments

# a single-line comment — everything from # to the end of the line =pod This is a multi-line documentation block, using Perl's own built-in documentation format, POD (Plain Old Documentation). It's ignored by the interpreter but can be extracted by tools into real formatted documentation. =cut

POD (=pod ... =cut) is a distinctly Perl feature — a lightweight markup format for documentation, embeddable directly inside the same file as the code it documents, and extractable into HTML/man pages by standard tools. Ordinary # comments cover everything else.

Perl vs. Bash: Why Perl Exists

Perl was built specifically because Bash/sed/awk hit real limits once a script needs actual data structures, subroutines with proper scoping, or complex logic — Bash's "everything is a string" model gets unwieldy fast beyond simple pipelines. Perl keeps the same regex-heavy, text-processing instincts (Chapter 6 covers this directly) while adding real arrays, hashes, references, and subroutines on top — the reason it became the standard "glue language" for text-heavy scripting throughout the 1990s and 2000s, and still shows up constantly in legacy sysadmin and text-processing code today.

Shebang line

#!/usr/bin/env perl — makes a script directly executable.

use strict / use warnings

Mandatory in real code — catches typos and likely bugs at compile time.

print vs say

print needs an explicit \n; say adds one automatically.

Comments & POD

# for single lines; =pod/=cut for embedded documentation.

💻 Coding Challenges

Challenge 1: Your First Script

Write a script beginning with the correct shebang line and use strict; use warnings;, that prints "Hello, Perl!" using say.

→ Solution

Challenge 2: Catch the Typo

Given a short script with use strict; enabled that assigns to $message but then tries to print $mesage (missing an "s"), write down the exact compile-time error Perl would raise, and explain in one sentence why use strict is what causes an error here instead of silent wrong output.

→ Solution

Challenge 3: print vs say

Write two versions of a script that prints three separate lines — one using only print (correctly formatted with newlines), one using say — producing identical output.

→ Solution

🎯 What's Next

Next chapter: Scalars & Basic Data Types — the $ sigil, strings/numbers, string interpolation, and my/our/local scoping.