Idioms

Learn about some of the common idioms of Perl.

Introduction to idioms

Simple means different things to different people. Effective programmers understand how Perl’s features interact and combine. Their code takes advantage of language patterns and idioms. The result of this Perlish thinking is concise, powerful, fluent, and useful code—and it’s simple when we understand it.

Every language has common patterns of expression, or idioms. The earth revolves, but we speak of the sun rising or setting. We brag about clever hacks but cringe at nasty hacks and code smells.

Perl has idioms; they’re both language features and design techniques. They’re mannerisms and mechanisms that give our code a Perlish accent. We don’t have to use them to get our job done, but they play to Perl’s strengths.

The object as $self

Perl’s object system treats the invocant of a method as a mundane parameter. Regardless of whether we invoke a class or an instance method, the first element of @_ is always the invocant. By convention, most
Perl code uses $class as the name of the class method invocant and $self as the name of the object invocant. This convention is strong enough that useful extensions such as Moops assume we will use $self as the name of object invocants:

Get hands-on with 1200+ tech skills courses.