The Duct-Tape of IT Infrastructure
Imagine you've just finished coding a brand new shell-in-yaml CI pipeline on your glossy metallic MDM-friendly company-administered laptop1. Spending time and NI2 think-tokens, you output a bunch of intricate commands employing multiple utilities, only to be disappointed by a misspelled flag or missing option manifesting once the pipeline's deployed to the cybernetic freighter your modest organization has. Alas, these containers were full of GNU, while you unknowingly targeted BSD userland.
Fear not! Bearded guys from the 80s already thought out a solution to this disconnect: Perl.
Yes, that Perl, the weird and obsolete3, yet if you push opinions aside and take a closer look, you'll notice it's preinstalled in almost every Linux and BSD system4, no extra steps required.
What can it do for you? Replace grep, for one (with regexps on by default):
printf "alice: access granted\nbob: access denied\n" > error.log
perl -nE 'say if /(.+): access denied/' error.log
Your Awk doesn't have gensub? Perl has it:
perl -pE 's/(.+): access (denied|granted)/$2: $1/g' error.log
Unlike sed's, Perl's -i flag works consistently across operating systems:
perl -i -pE 's/access denied/BAD GUY!/g' error.log
Wanna display documentation comments for your make targets? Try this one:
## help: Show this message
help:
@perl -ne '/^## ([a-zA-Z_-]+): (.*)$$/ && printf "\033[1m%-16s\033[0m\t%s\n", $$1, $$2' $(MAKEFILE_LIST)
.PHONY: help
Did I mention Perl has a JSON parser out-of-the-box?
perl -MJSON::PP -E \
'undef $/; say (decode_json(<>)->{"backend","config","bucket"} =~ /prod/ ? "prod" : "dev")' \
.terraform/terraform.tfstate
Still there? Let's go over the most important command switches:
-
-e progor-E progOne-liner mode: tells Perl to execute
progand not the contents of the first file given in the command line.-Eenables all optional features, likesayfunction (seeman 3pm featurefor the whole list). -
-nor-pBoth wrap your program in a loop iterating over each line of each input file (or
stdin). The difference is that-pprints each line automatically while-ndoesn't. -
-iEnables in-place editing with optional backup. Unlike BSD sed, it's not just an extension, but a pattern, which allows you e.g. to place backup files in a separate directory.
-
-MmoduleLoads
moduleand imports its methods before executing your program. Seeman perlmodlibfor a list of pragmas and core modules andperldoc moduleformoduledocs. -
-0Changes the default line separator to
\NULe.g. to correctly handle paths with newlines fromfind:find . -print0 | perl -n0E 'say "found ", $_'`.Give it an octal or hex number to use the corresponding ASCII or Unicode character to separate input. Special values are
00to enable paragraph mode and0777(or anything greater than0400) to slurp files whole.
I'll stop here and refer you to man perlrun for the rest of the switches and knobs and man perlintro for the syntax overview.
Perl has excellent documentation, organically grown and available at your local terminal emulator, so use it.
As any mature programming language, Perl has its own quirks and sharp edges, particularly around omitting parentheses for function calls, different behavior depending on the context, mutability, and global variables. I'd not use it for anything large, but for an occasional script or one-liner, it's a great replacement for the usual Bash/Sed/Awk bunch.5
-
Smart enough not to pick a corporate battle you'll inevitably lose, esoteric VPN setup and proprietary spyware vs. free software and choice. At least you were graciously given options on your first day, weren't you, even if both are resource-greedy, visually inconsistent, and generally bloated, at least the one you chose doesn't shove ads in your face just yet.
-
Natural Intelligence
-
According to a general sentiment since at least the early 2010s when I first came into this industry.
-
Alpine Linux is one notable exception