ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы.



18.2 Converting sed Programs to Perl

Well, this may begin to sound like a repeat, but guess what? Perl is a semantic superset of sed as well as awk.

And with the distribution comes a sed-to-Perl translator called s2p. As with a2p, s2p takes a sed script on standard input and writes a Perl program on standard output. Unlike a2p, the converted program rarely misbehaves, so you can pretty much count on it working, barring any bugs in s2p or Perl.

Converted sed programs may work faster or slower than the original, but are generally much faster (thanks to the highly optimized regular expression routines of Perl).

The converted sed script can operate either with or without a -n option, having the same meaning as the corresponding switch for sed. To do this, the converted script must feed itself into the C preprocessor, and this slows down the startup a little bit. If you know that you will always invoke the converted sed script with or without a -n option (such as when you are converting a sed script used in a larger shell program with known arguments), you can inform s2p (via the -n and -p switches), and it will optimize the script for that switch setting.

As an example of how versatile and powerful Perl is, the s2p translator is written in Perl. If you want to see how Larry codes in Perl (even though it's very ancient code relatively unchanged since Perl Version 2), take a look at the translator. Be sure you are sitting down.


Previous: 18.1 Converting awk Programs to PerlLearning PerlNext: 18.3 Converting Shell Programs to Perl
18.1 Converting awk Programs to PerlBook Index18.3 Converting Shell Programs to Perl