如何将 Perl 转换为 C?

发布于 2024-07-26 22:41:23 字数 47 浏览 6 评论 0原文

有没有可用的工具可以将 Perl 源代码转换为 C 源代码? 什么平台都可以。

Is there a tool available which will convert source code in Perl to source code in C? Any platform is fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

触ぅ动初心 2024-08-02 22:41:23

对此问题的规范答案是 MJD 的“为什么不将 Perl 翻译成 C? ”

The canonical answer to this is MJD's "Why Not Translate Perl to C?".

转瞬即逝 2024-08-02 22:41:23

perlcc 它将 Perl“翻译”为 C。

它并不是真正的Perl 到 C 的编译器; 它的输出只是 Perl 解释器和程序的解析字节码的捆绑。

There is perlcc which "translates" Perl to C.

It's not really a Perl to C compiler; its output is simply a bundle of the Perl interpreter and the parsed bytecode of your program.

ι不睡觉的鱼゛ 2024-08-02 22:41:23

答案几乎是“不”。 Perl 是一种极其动态的语言。 C 是一种用于静态大小数据类型的语言。 Perl 到 C 的任何翻译都可能会重复“执行这个子例程调用来模拟 Perl 的功能”。 构建这样一个翻译器没有什么意义,因为它执行 Perl 的速度不太可能比 Perl 快得多。

The answer is going to be pretty much "No". Perl is an extremely dynamic language. C is a language for statically-sized data types. Any translation of Perl to C will likely be pretty much "execute this subroutine call to simulate what Perl does" repeatedly. And there's little point in building such a translator, as it is unlikely to execute Perl much faster than Perl does.

烟酒忠诚 2024-08-02 22:41:23

我用 Perl 编写了一个相当大的程序,它基于 HTML 和数据库查询创建 PDF,实际上就像浏览器一样。 源代码总体积超过1MB。 该程序评估 HTML、创建 SQL 查询并检索数据、在磁盘上搜索图像或从 HTTP 服务器下载图像、构建文档结构、执行所有布局计算,最后生成 PDF。

我必须找出如何通过多种方式加快操作速度。 基于此,我声明 Perl 相当快,许多在 Perl 中快速成功完成的任务在 C 甚至 C++ 中需要很长时间。

有两种方法会导致 Perl 变慢或消耗内存:大量复杂的数据结构(它们需要大量内存)和大量计算。 是的,Perl 中的计算确实很慢。 像这样的简单术语

$a = $b * $c

在 Perl 中相当耗时,但在任何编译语言中都非常快。 这里的操作数甚至可能是整数,而不是浮点变量 - 它很慢。 我想这就是为什么 Perl 在语言大战中得分很差的原因 [http://shootout.alioth.debian.org/](计算机语言基准游戏)。

我发现我的相当大的程序 - 它使用了许多 Perl 核心和附加的 CPAN 模块 - 尽管经过解释,但可以快速启动。

它的表现非常好......直到涉及到文本大小和布局坐标的计算。 那是非常耗时的。 陈述完这一点后,我编写了小型 Perl 测试程序,仅进行数百万次算术计算,发现它们非常慢。

此外,我使用面向对象的方法来对每个布局元素进行建模。 每个对象都由一个哈希值表示 - 每个对象至少大约 10kByte。 如果要打印大量数据,则该程序消耗数百兆字节的内存并不罕见。

所以,我仍然有充分的理由将计算布局的部分移至 C,使用结构,其中我现在有带有固定键的哈希值,并且有 C 整数运算,而现在 Perl 的工作速度很慢。

但其他一切都已完成并很快进行了测试,并且运行得如此之快,以至于我看不出任何改变的理由。 我还发现 Perl 代码比 C 代码更方便编写和测试。 许多 CPAN 模块提供了您不必自己解决的解决方案。 其中许多都经过了充分的测试和记录。

经过这个相当长的讨论后,我得出结论:如果它是一个服务器或命令行程序,请考虑 Perl。 但如果这个程序必须构建巨大的数据结构或大量算术,请考虑更快的方法。 有时,它可能是一个带有用 C 编写的模块的 Perl 程序。

I wrote a pretty large program in Perl that creates PDF based on HTML and database queries that is virtually acting like a browser. The total source code volume is over 1MB. The programs evaluates HTML, creates SQL queries and retrieves data, searches for images on disk or downloads them from HTTP servers, builds a document structure, does all the layout calculations and, finally, produces a PDF.

I had to find out how to speed up the operation in a number of ways. Based on that I state that Perl is quite fast, and many tasks completed swiftly and successfully in Perl take a long time in C, and even C++.

There are 2 ways to make Perl slow or memory consuming: a large number of complex data structures - they need a lot of memory - and a large number of calculations. Yes, calculation is indeed very slow in Perl. A simple term like

$a = $b * $c

is quite time consuming in Perl, but very quick in any compiled language. The operands here may even be integers, not floating point variables - it is slow. I guess this is the reason why Perl scores pretty bad in the language shootout contest [http://shootout.alioth.debian.org/](The Computer Language Benchmarks Game).

I found my pretty large program -it uses many Perl core and additional CPAN modules as well- to start quickly, despite being intepreted.

It performs very well... until it comes to the calculation of text sizes and layout coordinates. Thats very time consuming. After stating this, I wrote small Perl test programs just doing millions of arithmetic calculations and found them to be very slow.

Besides, I am using and object oriented approach to model each and every layout element. Each object is represented by a hash - thats at least about 10kBytes per object. If there is a large amount of data to print, a memory consumption of several 100MBytes is not unusual for that program.

So, I still have a good reason to move the part calculating the layout to C, using structs where I now have hashes with fixed keys and have C integer arithmetic where now Perl does a slow job.

But everything else was done and tested swiftly and runs so fast that I don't see any reason to change. I also find Perl code much more convenient to write and test than C code. And a lot of CPAN modules provide solutions you don't have to work out yourself. Many of them are well tested and documented.

After this quite lenghty dicussion I conclude: if it is to be a server or command line program, consider Perl. But if this program has to build up huge data structures or lots of arithmetics, consider something faster. Sometimes, it may be a Perl program with a module written in C.

那片花海 2024-08-02 22:41:23

有 Perl 到 C 的翻译器,但没有一个是完美的。 理想情况下,您需要一个既正确又优雅的翻译器。 可惜,您不能同时拥有两者,简单的 Perl 代码并不等同于简单的 C 代码,因此您要么必须有一个不是 100% 正确的翻译,要么与 Perl 本身一样复杂。 这使得一些人相信您不应该尝试翻译 Perl。 更准确的说法是,你需要明确你想从翻译中达到什么目的,而不是期待奇迹。

100% 正确很容易:如果您的 Perl 脚本是 myperl.pl,则 C 程序 void main(){system("perl myperl.pl")} 将完全执行 myperl.pl 的操作; 但这毫无意义。 perlcc 编译器稍微复杂一些,但似乎仍然没有带来太多好处。 我没有注意到 perlcc 比普通 Perl 更快。 另外,虽然 Perl 代码非常难以阅读,但我更喜欢 print "Hello World\n" 而不是 perlcc 将其翻译成 700 行长的庞然大物。 我还没有看到这些程序产生任何可以通过代码审查的东西以及编写精美的 C 代码。 OTOH,如果您想要一个编译器,因为您不想以非混淆的方式分发源代码,那么 perlcc 可以创造奇迹。

RPerl 可以实现加速,但它可以翻译的内容非常有限。

有关简单的“优雅但不正确”翻译器的示例,请参阅原型 perl2c++.pl。 这是通过用 C++ 主义替换(一些)标准 Perl 主义来实现的。 选择 C++ 是因为它是像 Perl 一样的高级语言,但仍然具有与 C 相同的裸机精神。

对于简单的 LCG 伪随机数生成器 LCG.plperl2c++.pl 的输出是运行时干净简洁的 C++ 代码比原来的 Perl 快几十倍,并且不依赖于任何 Perl 库。 它可以扩展为查找“How to do X on Perl”的所有标准答案,并将其替换为“How to do X in C++”。 然后,它可能会成功翻译许多简单但真实的 Perl 脚本,并帮助人们将不平凡的 Perl 软件翻译成优雅的 C++ 代码。 如果您发现自己用 Perl 编写数值软件,而该软件本来应该用 C++ 编写,那么这将非常有用。

对于 Perl 非常适合的软件,但您只想运行得更快一点,JavaScript(最终是 Perl 6)使用的 JIT 方法更有前途。

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.

天暗了我发光 2024-08-02 22:41:23

转换器称为程序员,转换过程称为编程。 说真的,perl 语言本身是如此庞大和强大,以至于任何试图编写转换器的人都会看不起这项终生任务。 此外,性能提升的效果可能不是一个数量级,那何必呢?

The converter is called a programmer, and the conversion process programming. Seriously, the language proper of perl is so vast and powerful, that anyone attempting to write a converter would look down on a life-long task. Additionally, the effect in performance improvement might not be an order of magnitude, so why bother?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文