且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不能创建一个实用的 Perl 到 Python 源代码转换器?

更新时间:2023-09-08 20:08:04

只是为了扩展这里的其他一些列表,这些是一些在 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.

推荐文章