in reply to Need advice in for perl use as awk replacement

This is an example where you should rewrite the entire shell script in Perl. Using Perl one-liners in shell scripts is asking for performance problems — perl's startup and shutdown overhead is negligible in an interactive context, but it can add up fast if you are running perl repeatedly for every line of input. Even Awk has a similar issue, where efficient programming requires sending a stream into Awk and reading either a brief summary or a stream of output back.

To better understand Perl's implicit loops, try B::Deparse, used via O:

$ perl -MO=Deparse -n -e 'print $_' LINE: while (defined($_ = <ARGV>)) { print $_; } -e syntax OK

You should have the full line in $_ unless your code changes $_. As other monks have mentioned, you will need to copy the regex capture variables somewhere to preserve them, or take advantage of the fact that regex matches in list context return the subexpressions: my @fields = m/$line_regex/; note that matching against $_ is implicit in Perl if you do not use the =~ operator.

Replies are listed 'Best First'.
Re^2: Need advice in for perl use as awk replacement
by eyepopslikeamosquito (Bishop) on Sep 23, 2020 at 20:12 UTC

    This is an example where you should rewrite the entire shell script in Perl.
    Agreed. After being scarred by having to maintain an ugly 5000-line shell script (that started life as a quick ten line script) I put forward a case that Perl should almost always be preferred to Unix shell: Unix shell versus Perl.

    The key reason is that Perl can comfortably scale to much larger scripts than shell. And shell scripts have a way of growing ... and growing ... and growing ... until they become maintenance nightmares. But by then how do you justify a rewrite? The cost of rewriting, the opportunity cost of not working on something else, and the risk of breaking previously working code in the rewrite. So write it in Perl to begin with.