Learning Perl

Learning PerlSearch this book
Previous: 17.6 ExercisesChapter 18Next: 18.2 Converting sed Programs to Perl

18. Converting Other Languages to Perl

Converting awk Programs to Perl
Converting sed Programs to Perl
Converting Shell Programs to Perl

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 $. in Perl.

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 < operator. In Perl, you have lt for strings and < for numbers. awk generally makes a reasonable guess about the number-ness or string-ness of two values being compared, and the a2p utility makes a similar guess. However, it's possible that there isn't enough known about two values to determine whether a number or a string comparison is warranted, so a2p outputs the most likely operator and marks the possibly erroneous line with #?? (a Perl comment) and an explanation. Be sure to scan the output for such comments after conversion to verify the proper guesses. For more details about the operation of a2p, consult its manpage. If a2p is not found in the same directory that you get Perl from, complain loudly to your Perl installer.

Previous: 17.6 ExercisesLearning PerlNext: 18.2 Converting sed Programs to Perl
17.6 ExercisesBook Index18.2 Converting sed Programs to Perl