为什么不能创建一个实用的 Perl 到 Python 源代码转换器? [英] Why is it not possible to create a practical Perl to Python source code converter?

查看:46
本文介绍了为什么不能创建一个实用的 Perl 到 Python 源代码转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有一个程序可以自动将 Perl 代码转换为 Python 代码,使生成的 Python 程序与原始程序一样可读和可维护,那就太好了,更不用说以相同的方式工作了.

It would be nice if there existed a program that automatically transforms Perl code to Python code, making the resultant Python program as readable and maintainable as the original one, let alone working the same way.

最明显的解决方案是通过 Python utils 调用 perl:

The most obvious solution would just invoke perl via Python utils:

#!/usr/bin/python
os.exec("tail -n -2 "+__file__+" | perl -")
...the rest of file is the original perl program...

然而,生成的代码几乎不是 Python 代码,它本质上是 Perl 代码.潜在的转换器应该将 Perl 结构和习语转换为易于阅读的 Python 代码,它应该保留变量和子例程名称(即结果不应看起来很模糊)并且不应过多地破坏工作流程.

However, the resultant code is hardly a Python code, it's essentially a Perl code. The potential converter should convert Perl constructs and idioms to easy-to-read Python code, it should retain variable and subroutine names (i.e. the result should not look obfuscated) and should not shatter the wrokflow too much.

这样的转换显然很难.转换的难度取决于 Perl 功能和句法结构的数量,它们没有易于阅读、未混淆的 Python 等价物.我相信大量这样的功能使得这种自动转换实际上是不可能的(虽然理论上可能性存在).

Such a conversion is obviously very hard. The hardness of the conversion depends on the number of Perl features and syntactical constructs, which do not have easy-to-read, unobfuscated Python equivalents. I believe that the large amount of such features renders such automatic conversion impossible practically (while theoretical possibility exists).

那么,能否请您说出无法像原始 Perl 代码那样简洁地用 Python 表达的 Perl 习语和语法特性?

So, could you please name Perl idioms and syntax features that can't be expressed in Python as concise as in the original Perl code?

编辑:有些人将 Python-to-Perl 的发明者联系起来,并在此基础上推断,编写 Perl-to-Python 也应该很容易.但是,我确信将转换为 Python 的需求更大;仍然这个转换器还没有被写入——而相反的已经被写入了!这只会让我对编写一个好的 Python 转换器的可能性更加坚定.

Edit: some people linked Python-to-Perl conventers and deduced, on this basis, that it should be easy to write Perl-to-Python as well. However, I'm sure that converting to Python is in greater demand; still this converter is not yet written--while the reverse has already been! Which only makes my confidence in impossibility of writing a good converter to Python more solid.

推荐答案

只是为了扩展这里的其他一些列表,这些是一些在 Python 中可能非常笨拙的 Perl 构造(如果可能的话).

Just to expand on some of the other lists here, these are a few Perl constructs that are probably very clumsy in python (if possible).

  • 动态范围(通过local关键字)
  • typeglob 操作(多个同名变量)
  • 格式(它们有自己的语法)
  • 可变变量的闭包
  • 语用
  • 左值子程序(mysub() = 5; 类型代码)
  • 源过滤器
  • 上下文(列表与标量,以及调用代码的方式可以使用 wantarray 进行检查)
  • 类型强制/动态类型
  • 任何使用字符串eval
  • 的程序
  • dynamic scope (via the local keyword)
  • typeglob manipulation (multiple variables with the same name)
  • formats (they have a syntax all their own)
  • closures over mutable variables
  • pragmas
  • lvalue subroutines (mysub() = 5; type code)
  • source filters
  • context (list vs scalar, and the way that called code can inspect this with wantarray)
  • type coercion / dynamic typing
  • any program that uses string eval

这个列表还在继续,有人可能会尝试在所有类似的结构之间创建映射,但最终会因为一个简单的原因而失败.

The list goes on an on, and someone could try to create a mapping between all of the analogous constructs, but in the end it will be a failure for one simple reason.

Perl 不能静态解析.Perl 代码中的定义(尤其是 BEGIN 块中的定义)改变了编译器解释剩余代码的方式.因此,对于非平凡的程序,从 Perl => Python 转换会遇到停机问题.

Perl can not be statically parsed. The definitions in Perl code (particularly those in BEGIN blocks) change the way the compiler is going to interpret the remaining code. So for non-trivial programs, conversion from Perl => Python suffers from the halting problem.

在程序运行完成之前无法确切知道所有程序将如何编译,理论上可以创建一个每次运行时编译方式不同的 Perl 程序.这意味着一个 Perl 程序可以映射到无数个 Python 程序,其中正确的只有在 perl 解释器中运行原始程序后才能知道.

There is no way to know exactly how all of the program will be compiled until the program has finished running, and it is theoretically possible to create a Perl program that will compile differently every time it is run. Meaning that one Perl program could map to an infinite number of Python programs, the correct of which is only know after running the original program in the perl interpreter.

这篇关于为什么不能创建一个实用的 Perl 到 Python 源代码转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆