哪种动态语言可以轻松使用其他语言的库?
讲述为什么您认为 Python、Perl、Ruby 等最容易以最少的思考插入其他语言的模块。
为了澄清这一点,举一个例子:我想用 Python 编写业务逻辑,但使用作为 Perl 模块方便存在的功能。
换句话说,哪种语言“适合”最多的模块?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Perl 通过 Inline 模块集对其他语言提供了很好的支持。
Inline::Python 允许您将 Python 模块、类和函数导入到 Perl 中代码并从 Perl 调用它们,就好像它们是本机的一样 - 请参阅 导入函数。
Inline::Ruby 有效几乎以同样的方式。
Perl has very good support for other languages via the Inline set of modules.
Inline::Python allows you to import Python modules, classes and functions into your Perl code and call them from Perl as if they were native - see Importing Functions.
Inline::Ruby works virtually the same way.
Parrot VM 看起来是实现此目的的最佳方法,因为
我认为,使用哪种语言作为“主要驱动程序”以及使用哪种语言作为这个模块或那个模块的“客人”并不重要——只需检查您选择的 Parrot 实现是否完整且成熟即可(因为 Parrot 是一个相当新的项目,所以其中一些语言的实现也是在它之上)。
The Parrot VM looks like the way to go for this purpose, since
It matters less, I think, which of these languages you use as the "main driver" and which one just as "guests" for this module or that -- just check that you pick one whose Parrot implementation is complete and mature (since Parrot's a rather new project, and so are some of these language implementations on top of it).
如果您想插入 Perl 模块,最适合的语言是 Perl。 Perl 能够正确表示用 Perl 编写的代码的语义和功能。这确实不应该令人震惊。
如果您有一个独立的程序,您想在其自己的进程中从另一个程序调用,而不是不断地交互,那么任何这些语言都可以用任何语言编写的程序来做到这一点。那时,您并没有真正在程序中使用其他语言,而只是调用其他问题。
有几个项目可以组合各种对和项目(如 Parrot),这些项目寻求为多种语言提供兼容性平台,而项目(如 .NET)几乎意外地在以前不兼容的语言之间提供兼容性。然而,我认为其中大多数并不像您希望的那样健壮、成熟并且适合组合普通代码。
If you want to plug in a Perl module, the language which is best suited for this is Perl. Perl is able to represent the semantics and capabilities of code written in Perl correctly. This really shouldn't be a shock.
If you have a self-contained program you want to call from another program in its own process, not constantly interacting, any of these languages can do that with programs written in whatever language. At that point, you aren't really using other languages inside a program but just calling other problems.
There are several projects to combine various pairs and projects (like Parrot) that seek to provide a platform for a large range of languages for compatibility and projects (like .NET) that almost accidentally provide compatibility among previously-incompatible languages. However, I do not think most of these are as robust, mature, and suited for combining normal code as you would hope.
所有 3 种语言都有非常好的、清晰的工具,用于调用子进程中的任何可执行文件(包括像 python Somethingelse.py 或 ruby Somethingelse.rb 这样的可执行文件)。
使用你最了解的。
all 3 languages have very good, clear facilities for just calling any executable in a subprocess (including executables like
python somethingelse.py
orruby somethingelse.rb
).use what you know best.
动态语言运行时经过专门设计,允许一种动态语言使用另一种动态语言中定义的对象和函数。目前Python 和Ruby 有DLR 实现,但我还没有听说过有关Perl 的任何内容。
要使用 DLR,您需要 .NET 或 Mono。
The Dynamic Language Runtime was specifically designed to allow one dynamic language to use objects and functions defined in another dynamic language. Currently Python and Ruby have DLR implementations, but I haven't heard anything about Perl.
To use the DLR you need either .NET or Mono.
大多数脚本语言都可以处理这种事情(通过运行用其他语言编写的外部程序),但似乎最好的选择可能是某种 shell 脚本(Windows 用户称之为“批处理脚本”,但 DOS 语法是很糟糕,不推荐。) UNIX 程序员长期以来一直以这种方式自由混合语言。在 Windows 上,您可以安装 Cygwin 以获得功能齐全的 BASH shell。
shell 最初的目的是作为用户界面,用于启动其他程序或以有趣的方式组合它们。然而,许多 shell(特别是 Bourne shell 或其现代后代 BASH)也是成熟的编程语言。每个“模块”都可以创建为单独的独立程序,由 shell 脚本运行。
Most scripting languages can handle this kind of thing (by running external programs written in other languages,) but it seems as if your best bet might be shell scripting of some kind (Windows users call this "batch scripting," but the DOS syntax is horrible and not recommended.) UNIX programmers have been freely mixing languages in this way for a long time. On Windows, you can install Cygwin to get a fully functional BASH shell.
The shell was originally intended as a user interface, used to launch other programs or combine them in interesting ways. However, many shells (notably the Bourne shell or its modern descendant, BASH) are also fully-fledged programming languages. Each of your "modules" can be created as separate, standalone programs which will be run by the shell script.
我将在这里从更多的架构层面来回答。问题是你想做什么...你想用 Python 编写业务逻辑并从 Python 调用 Perl 函数吗?或者您想执行脚本?如果是的话,两人将如何沟通?
我怀疑(但不知道)Parrot VM 可能允许您这样做,但正如 Mike 指出的那样,存在困难。跨语言工作很困难,就像跨程序的 IPC 很困难一样,除非您使用某种形式的松散耦合(与语言不太相关)。为此,您可以考虑使用一种语言设置控制器,并让其他人通过 dbus 进行交谈 队列,或者您喜欢的任何平台的机制。如何做到这一点并不重要(提示关于最佳机制的争论),但正确的设计使得跨语言交谈和构建插件变得非常容易。例如,您可能有一个
process_new_user
队列。在该队列中注册的任何脚本都可以访问数据,以便新开发人员可以轻松地为其程序部分添加功能。将其解释为:您可以轻松地使用不同的脚本语言来实现该位。I'm going to answer on a more architectural level here. The question is what are you trying to do... do you want to write your business logic in Python and call a Perl function from Python? Or are you looking to execute a script? If so, how will the two communicate?
I suspect, but don't know, that Parrot VM might allow you to do that, but as Mike points out, there are difficulties. Cross-language work is hard just like IPC across programs is hard unless you use some form of loose coupling (not so tied to the language). To that end, you might consider setting up the controller in one language and having everyone else talk via a dbus queue, or whatever mechanism you prefer for whichever platform you're on. It doesn't really matter how you do it (cue a debate on the best mechanism) but properly designed it makes talking across languages and building plug-ins very easy. For example, you might have a queue for
process_new_user
, for example. Any script who registers in that queue gets access to the data so a new developer can easily add functionality for their part of the program. Interpret that as: you can easily use a different scripting language to implement that bit.