Expressivity
Explore Perl's expressivity rooted in its linguistic design philosophy. Understand Perl's flexibility, context handling, and the mantra 'there's more than one way to do it.' Learn to write code ranging from simple to expert levels while prioritizing readability, maintainability, and effective programming practices.
Coding in Perl
Before Larry Wall created Perl, he studied linguistics. Unlike other programming languages designed around a mathematical notion, Perl’s design emulates how people communicate with each other. This gives us the freedom to write programs depending on our current needs. We can write simple, straightforward code or combine many small pieces into larger programs. We can select from multiple design paradigms, and we can eschew or embrace advanced features.
Learning Perl
Learning Perl is like learning any spoken language. You’ll learn a few words, then string together sentences, and then enjoy simple conversations. Mastery comes from the practice of both reading and writing code. You don’t have to understand every detail of Perl to be productive, but the principles in this chapter are essential to your growth as a programmer.
Other languages may claim that there should be only one best way to solve a problem. However, Perl allows us to decide what’s most readable, most useful, most appealing, or most fun.
There’s more than one way to do it
Perl hackers call this TIMTOWTDI, pronounced “Tim Toady,” or “There’s more than one way to do it!”
This expressivity allows master craftworkers to create amazing programs, but also allows the unwary to make messes. We’ll develop our own sense of good taste with experience. We can express ourselves, but we should be mindful of readability and maintainability, especially for those who come after us.
Perl novices often find certain syntactic constructs opaque. These idioms offer great (if subtle) power to experienced programmers, but it’s okay to avoid them until we’re comfortable with them.
Do what I mean
Perl tries to avoid surprising experienced (Perl) programmers as another design goal. For example, adding two variables ($first_num + $second_num) is a numeric operation. We’ve expressed our intent to treat the values of those variables as numbers by using a numeric operator. Perl happily does so. No matter what the contents of $first_num and $second_num are, Perl will coerce them to numeric values.
Perl adepts often call this principle DWIM or do what I mean. We could just as well call this the principle of least astonishment. Given a cursory understanding of Perl (especially
Baby Perl
Perl’s expressivity allows novices to write useful programs without having to understand the entire language. This is by design! Experienced developers often call the results baby Perl as a term of endearment. Everyone begins as a novice. Through practice and learning from more experienced programmers, you’ll understand and adopt more powerful idioms and techniques. It’s okay for you to write simple code that you understand. Keep practicing and you’ll become a native speaker.
From a novice to an experienced Perl hacker
A novice Perl hacker might triple a list of numbers with this:
Perl adept
A Perl adept might write this:
Experienced Perl hacker
On the other hand, an experienced Perl hacker could write this:
Every one of these three programs generates the same result. Each uses Perl in a different way.
Let the language do more for you
As you get more comfortable with Perl, you can let the language do more for you. With experience, you can focus on what you want to do rather than how to do it. Perl doesn’t care if you write baby or expert code. Design and refine programs for clarity, expressivity, reuse, and maintainability, in part or whole. Take advantage of this flexibility and pragmatism: it’s far better to accomplish your task effectively now than to write a conceptually pure and beautiful program next year.