Section B.25. Converting Other Languages to Perl


B.25. Converting Other Languages to Perl

If you've got old sed and awk programs that you wish were written in Perl, you're in luck. Not only can Perl do everything that those can do, it has a conversion program, and it's probably installed on your system. Check the documentation for s2p (for converting from sed) or a2p (for converting from awk).[*] Since programs don't write programs as well as people do, the results won't be the best Perl, but it's a start and it's easy to tweak. The translated program may be faster or slower than the original, too. After you've fixed up any gross inefficiencies in the machine-written Perl code, it should be comparable.

[*] If you're using gawk or nawk or some other variant, a2p may not be able to convert it. Both of these conversion programs were written long ago and have had few updates except when needed to keep working with new releases of Perl.

Do you have C algorithms you want to use from Perl? Luck is on your side; it's not too hard to put C code into a compiled module that can be used from Perl. In fact, just about any language that compiles to make object code can be used to make a module. See the perlxs manpage and the Inline module, as well as the SWIG system.

Do you have a shell script you want to convert to Perl? Your luck has run out. There's no automatic way to convert shell to Perl. That's because the shell hardly does anything by itself; it spends all of its time running other programs. Sure, we could make a program that would mostly call system for each line of the shell, but that would be slower than letting the shell do things in the first place. It takes a human level of intelligence to see how the shell's use of cut, rm, sed, awk, and grep can be turned into efficient Perl code. It's better to rewrite the shell script from scratch.



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net