I see awk as a DSL to be honest. Yes, it can be used as a general purpose language, but that quickly becomes, as you say, awkward :D
Like many DSLs, it is simple, fast and lightweight as long as it is used for it's intended purpose. Once you start using it for something else, these advantages evaporate pretty quickly, because then you have to essentially work around the DSL design to get it to do what you want.
... {
print $0 | "command"
}
"command" is executed once, and the pipe is kept open until closed explicitly by close("command"), at which point the next invocation will execute it again. The command string itself acts as a key for the pipe file descriptor.And of course, no mention of awk is complete without the "uniq" implementation, which beats the coreutils uniq in every way possible (by supporting arbitrary expressions as keys and not requiring sorted input):
!a[$0]++0: https://github.com/bentxt/microperl-standalone
1: Original article from 2000 by the author Simon Cozens: https://www.foo.be/docs/tpj/issues/vol5_3/tpj0503-0003.html
It depends on scale.
If you have some quick parsing to do, then awk will get you started quickly, but as you expand your experimentation on what you want to extract/manipulate, it may not be easy to add onto the awk beginnings of your "one liner".
But if you start with awk-like† syntax but invoking it with Perl, then if you find you have to expand, Perl has more elbow room.
The intention is not to 'go big', which those other languages may be better at, but to more easily 'start small'.
† IIRC, Larry Wall wanted a utility that had awk/(s)ed-like syntax for text manipulation, just 'with more'.
#!/usr/bin/perl
while (<>) {
# various processing here
# $ARGV is set to either "-" for piped input, or the current filename
# $_ is the data of the current line
}
That (<>) construct accepts data from stdin, redirection or file(s) named as arguments and iterates over the data. There's lots of things like that throughout the language.It can be very useful and they are pretty robust. I often found Perl scripts running for years and years without issues at different companies.
My main issue with Perl-scripts is that they often are not "readable" by anybody but the original creator. Which of course left the company. (not a fault of Perl itself tough)
But your millage may vary and any script can be made (un)readable.
Anyone writing Perl scripts like this should not be trusted with any programming language.
Perl scripts are no less readable than bash scripts or Awk scripts. This is because so much of Perl was written to do the same work as bash, awk, sed, and the other related Unix text processing command line programs, but all under one roof.
Don't believe me? Take a look for yourself:
Most programming languages can be obfuscated. That does not mean people write code in those programming languages like that:
Javascript: view-source:https://www.google.com/
The truth is that insulting Perl is considered stylish by some, so many people do despite knowing little to nothing about Perl and having never used it.
However, if you want Perl to be hilariously unreadable, why not write it in Latin:
https://metacpan.org/dist/Lingua-Romana-Perligata/view/lib/L...
Or Klingon:
fn print_d(t: &'static impl Display) {- Want to cut through and move loam, compost, sandy, and compacted soil? You're gonna want a rounded shovel.
- Want to break up rocky, clay soil? A pick mattock will penetrate deep, breaking up soil, shattering smaller rocks, and is used as a lever to uproot. A tiller is a faster method but disturbs the soil more.
- Want to dig a narrow, deep hole? An augur will quickly break up rocks and soil in a shaft and move them upwards.
What do you use the Perl tool for?
- Quickly and efficiently open files, read line by line, analyze text, and perform any kind of operation you can think of, with complex data structures, objects and modular code, using very few lines of code.
- Executing external commands with a shell, returning their output, and making complex yet short programs easily with arguments to the interpreter from a command line.
One day while avoiding working on something important, I spent half a day learning Perl in order to implement something related to a build tool that was being used in the important thing I was avoiding.
I was blown away. It's a really delightful language. Its big downfall is that it makes it feel good to do something "clever."
Perl is a joy to write, and a devil to read. I liked it, and wish I had started my career earlier so I could have enjoyed Perl in its heyday.
I have similar feelings about Ruby.
In fact, Perl remains remarkably robust if you stack clever tricks on top of each other.
1. You can write totally unreadable perl. It is probably the single worst language in this regard most programmers will run into. Be careful to make your code readable.
2. Keep your amount of perl small. 200-300 lines is a good bit of it.
So for quick bang it out scripts that want to parse text etc... perl is great. For writing a major application, not so much.
That stuff is just awkward and painful in Python by comparison.
Aside from that I've mostly been using it for quick statistics [2], but it quickly moves into perl territory...
1: https://github.com/9001/asm/blob/hovudstraum/etc/bin/beeps#L...