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/perl โ€” env 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.