18. Converting Other Languages to Perl
Contents:
18.1 Converting awk Programs to Perl
One of the many cool things about Perl is that it is (at least) a semantic superset of
awk
. In practical terms, this means if you can do something in
awk
, you can also do it somehow in Perl. However, Perl isn't
syntactically
compatible with
awk
. For example,
awk
's NR (input record number) variable is represented as If you have an existing awk program, and wish it to run with Perl, you can perform a mechanical translation using the a2p utility provided with the Perl distribution. This utility converts the awk syntax into the Perl syntax, and for the vast majority of awk programs, provides a directly runnable Perl script. To use the a2p utility, put your awk program into a separate file and invoke a2p with the name of the file as its argument, or redirect the standard input of a2p to the file. The resulting standard output will be a valid Perl program. For example: $ cat myawkprog BEGIN { sum = 0 } /llama/ { sum += $2 } END { print "The llama count is " sum } $ a2p <myawkprog >myperlprog $ perl myperlprog somefile The llama count is 15 $ You can also feed the standard output of a2p directly into Perl, because the Perl interpreter accepts a program on standard input if so instructed: $ a2p <myawkprog | perl - somefile The llama count is 15 $ An awk script converted to Perl will generally perform the identical function, often with an increase in speed, and certainly without any of awk 's built-in limits on line lengths or parameter counts or whatever. A few converted Perl programs may actually run slower; the equivalent action in Perl for a given awk operation may not necessarily be the most efficient Perl code if one was programming from scratch. You may choose to hand-optimize the converted Perl code, or add new functionality to the Perl version of the program. This is fairly easy, because the Perl code is rather readable (considering that the translation is automatic, this is quite an accomplishment).
A few translations are not mechanical. For example, the less-than
comparison for both numbers and strings in
awk
is expressed with the |
|