Today I ran across Minimal Perl: For UNIX and Linux People. The book was published a few years ago but I hadn’t heard of it because I haven’t kept up with the Perl world. The following chapters from the table of contents jumped out at me because I’ve been doing a fair amount of
3. Perl as a (better)
4. Perl as a (better)
5. Perl as a (better)
6. Perl as a (better)
These chapters can be read a couple ways. The most obvious reading would be “Learn a few features of Perl and use it as a replacement for a handful of separate tools.”
But if you find these tools familiar and are not looking to replace them, you could read the book as saying “Here’s an introduction to Perl that teaches you the language by comparing it to things you already know well.”
The book suggests learning one tool instead of several, and in the bargain getting more powerful features, such as more expressive pattern matching. It also suggests not necessarily committing to learn the entire enormous Perl language, and not necessarily committing to use Perl for every programming task.
Regarding Perl’s pattern matching, I could relate to the following quip from the book.
What’s the only thing worse than not having a particular metacharacter … in a pattern-matching utility? Thinking you do, when you don’t! Unfortunately, that’s a common problem when using Unix utilities for pattern matching.
That was my experience just yesterday. I wrote a regular expression containing
\d for a digit and couldn’t understand why it wasn’t matching.
Most of the examples rely on giving Perl command line options such as
-e so that it acts more like command line utility. The book gives numerous examples carrying out common tasks in
grep etc. and with Perl one-liners. The latter tend to be a little more verbose. If a task falls in the sweet spot of a common tool, that tool’s syntax will be more succinct. But when a task falls outside that sweet spot, such as matching a pattern that cannot be easily expressed with traditional regular expressions, the Perl solution will be shorter.
This is an update, written March 3, 2021.
If you’re going to use Perl as a replacement for command line tools, you’ll need to know about one-liners and quoting.
Here is a post that covers Perl as a better grep.
If your main use for
sed is to run commands like
s/foo/bar/g, you can do this in Perl with
perl -ple 's/foo/bar/g'
I talk more about using Perl to replace
If you want to use Perl as a replacement for
awk, the main thing you need to know about is the
-a option. This populates an array
@F which corresponds to
$3, etc. in
awk. Note however that Perl arrays are indexed from 0, so
$F corresponds to
$1 etc. A few more correspondences between the languages are given in the table below.
| awk | perl | |-----+-------| | $0 | $_ | | $2 | $F | | RS | $/ | | ORS | $\ | | OFS | $, |
Perl can have
END blocks just like
You can set the field separator in Perl with
-F, such as
-F: to make the field separator a colon. In newer versions of Perl 5 you don’t have to specify
-a if you specify
-F; it figures that if you’re setting the field separator, you must want an array of fields to play with.