Is there a tool available which will convert source code in Perl to source code in C? Any platform is fine.
There are Perl to C translators, but none are perfect. Ideally you'd want a translator that is both correct and elegant. Alas, you can't have both, simple Perl code isn't equivalent simple C code so you either have to have a translation that isn't 100% correct or is as complex as Perl itself. This has led some to believe you should not try to translate Perl. It would be more accurate to say that you need to be clear what you want to achieve from the translation, and not expect miracles.
100% Correct is easy: if your Perl script is myperl.pl then the C program void main(){system("perl myperl.pl")}
will do exactly what myperl.pl will do; this is rather pointless though. The perlcc
compiler is a little more sophisticated, but still doesn't seem to give much benefit. I haven't noticed perlcc
being any faster than plain Perl. Also, while Perl code can be notoriously difficult to read, I prefer print "Hello World\n"
to the 700 line long monstrosity that perlcc
translates it into. I haven't seen these programs produce anything that would pass a code-review as well written elegant C code. OTOH, if you want a compiler because you don't want to distribute your source code in a non-obfuscated way, then perlcc
could work wonders.
RPerl can achieve speed ups, but is very limited in what it can translate.
For an example of a trivial "elegant but not correct" translator, see the prototype perl2c++.pl. This works by replacing (a few) standard Perl-isms with C++-isms. C++ was chosen because it is a high-level language like Perl, but still shares the same bare-metal ethos of C.
In the case of a simple LCG Pseudo-Random number generator LCG.pl, the output of perl2c++.pl
is clean and succinct C++ code that runs dozens of times faster than the original Perl and does not depend on any Perl libraries. It could be extended to look for all the standard answers to "How to do X on Perl", and replace it with "How to do X in C++". Then it might successfully translate many simple but real world Perl scripts, and help a human translate non-trivial Perl software into elegant C++ code. This would be most useful if you find yourself writing numerical software in Perl which should have been written in C++ in the first place.
For software for which Perl is well suited, but you just want to go a little faster, the JIT approach used by JavaScript (and ultimately Perl 6) is more promising.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With