2

假设您可以获得动态解释器;静态编译语言可以替代脚本语言吗?我一直不太明白为什么有人会使用脚本语言?我说的是在 PC 上,而不是需要简单解释器的受限系统。我看到了一些 python 安装脚本,并看到了类似的 python 和 C# 解决问题的方法。那么为什么要使用脚本语言呢?

注意:有些事情让我对 C# 感到困扰,我不是在问为什么不使用 C#。我在问为什么要使用脚本语言?我发现静态编译语言更容易调试,而且通常更容易编码。

4

9 回答 9

5

如今,编译和解释之间几乎没有区别。看看解释语言是如何执行的——第一步是将脚本转换成某种内部可执行形式,比如可以通过更简单的指令集执行的字节码。这本质上是编译成虚拟机格式。这正是现代编译语言所做的。当编译语言部署在服务器端 Web 应用程序中时,它们甚至可以即时从源代码重新编译。因此,在编译/执行技术方面几乎没有区别。

唯一的区别在于指令集的细节,特别是类型系统。脚本语言通常(但不总是)是动态类型的。但是许多大型应用程序也是用动态类型语言编写的。同样,这里没有明显的区别。

就我个人而言,我认为静态类型远非“额外不必要的努力”(正如它经常被描述的那样)实际上是一个巨大的生产力助推器,由于智能感知/自动完成,在第一次尝试时更容易正确地编写简短的片段。为了强调这一点,看看微软如何通过简单地添加静态类型信息(在特殊格式的注释中)来改进 jQuery 库,以便我们可以在 IDE 中使用智能感知。

同时,静态语言(包括 C# 和 Java)正在引入更多的动态类型特性。

所以我认为这些类别最终会合并,而区分是没有意义的。

于 2009-08-09T07:25:01.830 回答
3

This kind of question often starts flame wars as people are passionate about their respective camps.

In the computer olden days, Unix command line tools and console shells provided a rich scripting environment where all sorts of processing could be done. You didn't need to be an expert programmer in any specific language and could string (pun intended) various programs (other people wrote) together using the pipe structure to massage your data which was mostly text not binary related. It is quick and easy to make changes to your batch command file. You don't have a source file that has to be edited, compiled linked with external static or shared libries/DLLS in the case of Windows.

One thing scripting does not have normally have is speed. You don't write device drives and live internet trading AI systems in scripting. But if you run a script once a day on some data received via e-mail or ftp you don't normally care how long it takes as it can run it background anyway.

Rewind back to the present and the waters become muddy. Some scripting enviroments offer a kind of speed up facility where they will read you script and almost compile and link in modules the same a normal C++ or VB program might use for speed puposes. But this very iffy and can't be relied on.

So how do you choose which route to go. Start doing tasks using scripting. If it runs too slow or you are having to do stuff every 5 minutes then parts of your script might benifit from a section written in a traditional language or the whole thing could be written in a language.

Like anything dabble and learn

于 2009-08-09T06:09:45.307 回答
3

维基百科说脚本语言是一种控制其他软件的语言。您可以使用 C# 来做到这一点,但像 Powershell 这样的真正脚本语言是专门为此设计的。

我倾向于以比 C# 更“交互式”的术语来考虑脚本语言。使用脚本语言,您可以编写一两行代码,执行它并立即查看结果。这在 C# 中并不容易,您必须将代码放在控制台应用程序中,或者从单元测试中触发它,或者将它输入到没有智能感知的即时窗口中。

这种快速的编写、执行周期允许使用脚本语言对完整的“脚本”进行快速原型设计,因为它可以为您提供每行代码的即时反馈。

于 2009-08-09T05:10:38.000 回答
2

我将我的 shell (bash) 称为脚本语言,但我没有看到已编译的替代版本。

我喜欢使用 scala,它是一种静态类型语言,带有类似解释器的 REPL 接口,由于类型干扰,它看起来很像脚本语言;看看这里:http ://www.simplyscala.com/ 。

但它并不意味着像 shell 那样成为其他程序之间的粘合剂,所以对于只需几行代码就可以轻松通过手和眼睛验证的小型作业,我更喜欢使用 shell。从一个目录跳转到另一个目录在 shell 中很舒服,提示符显示我在哪里。

于 2011-02-04T07:38:36.713 回答
2

Each is used for different purposes. Programs written in scripting languages are often not self-contained; they often function as "glue code" or (as Robert Harvey mentions) to automate a task. You often find scripting language interpreters embedded within an application (cf Python in Blender; Guile, Perl and Python in GIMP; JS in umpteen different browsers; Lua in countless games). Compiled languages, on the other hand, are used to produce self-contained applications. Scripts are mostly cross-platform; compiled applications usually aren't.

Note that a scripting language doesn't necessarily use an interactive interpreter (e.g. Perl), and an interpreted language isn't necessarily use for scripts (e.g. games made using PyGame). Note also that there's nothing about the languages themselves that make them interpreted or compiled. You could have a C# interpreter or a Ruby compiler. There have been a number of Lisp systems that offered both interpreters and compilers.

于 2009-08-09T06:27:41.057 回答
1

在我们开始之前,我认为我从来没有遇到过不尝试就“获得”脚本语言的静态语言用户,包括我自己。这是一种不同的体验。

所以不行。基本上,您可以向静态语言添加功能,使它们表面上看起来像脚本语言(如简单类型推断),但它不一样:

  1. 许多脚本语言用户讨厌静态语言。他们感到受限制。脚本语言通常非常擅长不妨碍用户,这是在静态语言中为了速度/正确性而牺牲的。

  2. 鸭子打字不会出现在静态语言中。

  3. 脚本语言用户不喜欢类型注释。为脚本语言提供类型推断系统实际上是不可能的,一些语言中出现的简单类型推断现在只适用于静态类型。

  4. 像猴子补丁这样的技术(在我看来这是一个非常糟糕的主意)在 Ruby 中很普遍,并且允许非常强大的技术,这些技术也不会很快在静态语言中可用。

这并不是说尚未设计的语言不能以相对静态的方式处理脚本语言功能,但相对于根深蒂固的 Python/PHP/Perl/Ruby,它很难变得流行/Javascript 设置。因素是最接近的东西,AFAICT。

将会发生的是脚本语言实现将通过使用 JIT 变得更快。

于 2009-08-09T09:43:14.890 回答
1

螺丝刀可以代替锤子吗?不,因为您只是不将它们用于相同的目的。如果两者都存在,如果这么多人使用其中之一,那一定是有原因的……

相同的答案:

  • 类继承与原型;
  • 命令式 vs oo;
  • 静态与动态类型;
  • 强类型与弱类型;
  • 手动内存管理 vs GC;
  • C# 与 Java;
  • 蓝色与红色;
  • 男人对女人;
  • 蝙蝠侠大战超人(但我确实认为超人会赢……等等,有氪石……哦,伙计,我不知道……)

ETC...

于 2009-10-31T13:18:08.297 回答
0

因为它是一种高级语言,所以编写起来更短,而且它不需要编译周期,这也使事情变得更短。

于 2009-08-09T07:24:08.383 回答
0

我在问为什么要使用脚本语言?我发现静态编译语言更容易调试,而且通常更容易编码。

因为我发现没有显式编译运行周期的松散类型动态语言更容易调试并且通常更容易编码。

于 2009-08-09T10:03:58.890 回答