Perl Basics: A Crash Course for CGI Developers

4

Perl is a scripting language. It’s old. It’s respected. It’s everywhere. If you’ve ever seen a CGI script running a basic web application from the late 90s, you were looking at Perl.

It does two things well. First, it replaces DOS batch files and C shell scripts. Second, it powers web servers. That’s the context that matters now.

The good news? Source code is free. Developers give it away. You can download thousands of scripts. You can study them. You can break them.

The bad news? Much of that code is unreadable. Perl encourages a style so cryptic it borders on obfuscation. If you don’t know what you’re doing, you won’t know what you’re reading.

This guide assumes you already code. If you know C, you’re ahead. If not, start here. We’re skipping the philosophy. We’re getting to the syntax.

Getting the Interpreter Running

You need the Perl interpreter.

If you’re on UNIX, it’s probably there. 99.99% chance. Check your terminal.

If you’re on Windows or Mac, go to the official site. Download the latest release. Install it. Perl is free. It’s open source. It’s been around since 1987. Don’t overthink the installation.

Once installed, find the DOCS directory. It comes with the package. Scan it. Don’t read every word yet. It’s dense. It’s dry. But it’s accurate. You’ll need it later when the basics stop making sense.

Your First Script

Set your path. Include the Perl executable. Open a text editor. Create a file named test1.pl.

Type this:

Save it. Open your command prompt. Type:

You’ll see “Hello World!” on the screen. That’s stdout. That’s standard output.

On UNIX, you can add #!/usr/bin/perl to the first line. Then you can just type ./test1.pl. Cleaner. Faster.

The print command outputs to stdout. The \n is a line feed. Simple.

Try this:

Two lines. One command.

Why did it work? Because of double quotes. In Perl, double quotes trigger quoting interpretation. It parses escape characters.

Use single quotes?

Now you see literal text. \n stays as \n. No interpretation. No magic.

There’s also the backquote character: `.

This runs an operating system command. The output prints to the screen. On Windows, cmd /c is necessary. dir isn’t an executable. It’s a built-in command of the command interpreter.

If you try print 'dir'; on Windows NT, it fails. You need the shell wrapper.

Note the / character. It’s used for regular expressions. You’ll see it everywhere.

print takes commas as separators:

It also takes periods as concatenation operators:

The period joins strings. The comma separates arguments.

C programmers will like printf. It works exactly like C.

Variables and Data Types

Perl variables don’t need declaration. They exist the moment you assign them.

Always use a $ symbol.

Double quotes interpret escapes. Single quotes do not.

Numbers work too.

\t is a tab. Perl handles it.

String concatenation uses the dot:

Shorthand exists. .= works like +=.

Arrays

Arrays start with @.

Arrays are zero-indexed. Like C. Like Java. Like Python.

The $#a notation gives you the highest index. Not the count. The index. So if you have three items, $#a is 2.

Hashes

Hashes start with %. They’re key-value pairs.

‘bar’ is linked to ‘dog’. ‘meow’ to ‘cat’.

There’s a cleaner syntax. The => operator.

It quotes the left side. It acts as a comma. It makes code readable. Use it.

Perl is easy once you stop fighting the syntax. The basics are straightforward. The advanced stuff is where things get weird. Stick to the core. Master the variables. Master the quoting rules.

The rest is just patterns. And bad habits.

If you’ve spent any time in C, Perl’s syntax will feel familiar. It borrows heavily from that lineage, but it loosens the grip on strictness.

Take a for loop. It looks exactly like what you expect:

You can also lean on while statements for simpler iterations. Here, the variable increments inside the block:

Logic gates follow the same rules as C. Use && for “and”, || for “or”, and ! for “not”.

Note: Perl offers a second set of comparison operators for strings. While == checks numerical equality, eq checks string equality.

The numeric operators remain standard:
== (equal)
!= (not equal)
<, <=, >, >=

But if you are dealing with text, you might prefer the word-based alternatives:
eq (equal)
ne (not equal)
lt (less than)
le (less than or equal)
gt (greater than)
ge (greater than or equal)

Iterating Through Arrays

Looping over a collection in Perl is clean thanks to foreach. It assigns each element of an array to a scalar variable one by one.

Here, $b takes on the value of ‘dog’, then ‘cat’, then ‘eel’. The loop ends when the array @a is exhausted.

The Brace Rule

There is one non-negotiable syntax rule in Perl: braces.

You must use { and } to wrap code blocks, even if the block contains only a single line.

Skip the braces, and the compiler will complain.

Writing Subroutines

Functions in Perl are defined using the keyword sub.

When you call a subroutine, all arguments passed to it are stuffed into a special array called @_.

The variable $#_ returns the highest index of the @_ array. Since arrays are zero-indexed, this effectively gives you the count of parameters minus one.

It’s a bit obtuse, but once you get used to it, it’s efficient.

Variable Scope and Return Values

To avoid polluting the global namespace inside a function, declare local variables using the local keyword.

You can also call a function explicitly using the & symbol.

Technically, & is only required when there is ambiguity in the parser. But many developers use it by habit to be explicit.

To send a result back to the caller, use the return keyword.

Input and Output

Reading from the standard input stream (STDIN ) is straightforward. The angle bracket operator reads a line at a time.

This assumes the input is a valid integer. If the user types garbage, the logic breaks.

For single-character input, use getc :

Or use the read function, which specifies the length of input to capture.

The 1 at the end dictates that only one character is read.

Environment and Command-Line Data

Perl exposes system data through global variables.

The %ENV hash holds all environment variables. Remember, the keys must be in upper case.

If you need to access command-line arguments, look at the @ARGV array.

  • $ARGV[0] is the first argument.
  • $ARGV[1] is the second.
  • $#ARGV gives you the index of the last argument (count minus one).

This makes it easy to build scripts that accept dynamic inputs without hardcoding values.

Is Perl Still Relevant?

It’s a fair question. Do programmers still use Perl? Yes.

Is Perl still used in 2024? Yes, though its role has shifted. It’s a high-level, general-purpose, interpreted, dynamic language. It doesn’t dominate new greenfield web projects the way Python or Node.js might.

However, Perl remains a powerhouse for system administration and legacy maintenance. It excels at text processing and rapid scripting