Perl lessons
Text processing master
Perl was written in 1987 by Larry Wall to simplify text processing and system administration tasks. In the early days of the internet, the vast majority of web applications were written in Perl; at that time, it was referred to as the "duct tape of the Internet".
Perl's strongest point is regular expressions. Regular expression support is so deeply embedded into the language that the syntax used in other languages today is called "Perl Compatible Regular Expressions". Extracting data from a text file, parsing logs, or performing batch replacements takes just a few lines in Perl.
The philosophy of the language is "there's more than one way to do it". This increases expressive power but can make readability difficult; well-written Perl code is very clear, while hastily written Perl code is famously unreadable. Although Perl is rarely chosen for new projects today, it still lives on in system administration and legacy code maintenance.
- •→
Introduction to Perl
First step into Perl, the powerful text-processing language: print, variables ($), and string interpolation.
- •→
Operators
Arithmetic (+ - * / % **), concatenation with ., and comparison (number: ==, string: eq).
- •→
Strings
length, uc, substr, and concatenation with .
- •→
Arrays (@)
Arrays with @, adding items with push, counting with scalar, and iterating with foreach.
- •→
Hashes (%)
Storing key-value pairs with % and accessing with $h{key}.
- •→
Conditions (if)
if-elsif-else, unless, and number/text comparison operators.
- •→
Loops
Iterating over items/ranges with foreach and conditional repetition with while.
- •→
Functions (sub)
Subroutines with sub; arguments are in the @_ array, return with return.
What you will learn
- Printing output with print and using scalar variables
- Using array and hash structures
- Searching and replacing with regular expressions
- Reading files and processing them line by line
- Writing subroutines
- Taking command-line arguments
Who is this for?
- Those who will automate log file and text processing
- System administrators taking over an existing Perl codebase
- Developers who want to master regular expressions
When to use?
Perl is still very efficient for parsing large text files, processing logs, performing batch file conversions, and one-liner command-line jobs.
It is not preferred for a new web application, mobile app, or data science project; in these areas, the Python and JavaScript ecosystems are much more vibrant.
Frequently asked questions
Is Perl worth learning?
Not as a new career language, but definitely as a tool. It is still very effective in text processing and comes pre-installed on many Linux systems. Additionally, in system administration jobs, you are likely to encounter Perl scripts whose maintenance you'll inherit.
What is the relationship between Perl 5 and Raku?
Raku is the project that was developed under the name Perl 6 for a long time and eventually named as a separate language. It is not a continuation of Perl 5; it is a different language, and their codes are not compatible with each other. Today, when Perl is mentioned, almost always Perl 5 is meant.
What do the dollar, at, and percent signs mean?
Perl indicates the variable type with the symbol at the beginning of its name: dollar means a single value (scalar), the at sign means an array, and the percent sign means a hash. These signs, called sigils, feel foreign at first, but they instantly show what the variable holds when reading the code.
Why is use strict written?
Without this line, Perl silently accepts a variable you haven't defined; a typo goes unnoticed and produces wrong results. The use strict and use warnings lines catch such errors and are recommended to be placed at the beginning of every Perl script.
Summary
Perl is still very powerful in text processing and regular expressions, and is a widely used language in system administration. In this tutorial, we progress from basic variable types to regular expressions and file handling.