.net PowerShell 是什么类型的语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11880447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What type of language is PowerShell?
提问by Neowizard
I have been using quite a lot of PowerShell lately (I'm mostly a system administrator), and I wondered to myself what type of language it is. I would think it is an interpreted language, but I have heard a whole lot of other names describing languages in general: Strong vs. weakly typed, dynamic, static. What does PowerShell fall under?
我最近使用了很多 PowerShell(我主要是系统管理员),我想知道它是什么类型的语言。我认为它是一种解释型语言,但我听说过很多其他描述语言的名称:强类型与弱类型、动态、静态。PowerShell 属于什么范畴?
回答by Neowizard
A few clarifications first:
首先澄清几点:
The two categories: Compiled and Interpreted refer to the way the source code (or script) is translated into actions.
编译和解释这两个类别是指将源代码(或脚本)转换为动作的方式。
In an interpreted language, the commands are interpreteddirectly to actions by the interpreter.
在解释型语言中,命令被解释器直接解释为动作。
In a compiled language the source code is first translated (or compiled) into a machine language (or an intermediate language like Java's byte-code or .NET's assembly) which will be turned into actions when run. In the case of a compiled program, you can look at the compiled code as the source code for an interpreted language and think of the CPU as the interpreter (or JVM in Java's case and the .NET Runtime for .NET).
在编译语言中,源代码首先被翻译(或编译)成机器语言(或中间语言,如 Java 的字节码或 .NET 的程序集),在运行时将转化为动作。在编译程序的情况下,您可以将编译后的代码视为解释语言的源代码,并将 CPU 视为解释器(或在 Java 的情况下为 JVM,而在 .NET 中为 .NET 运行时)。
The concepts of statically and dynamically typed language refer to the variables of that language.
静态和动态类型语言的概念指的是该语言的变量。
A statically typed language (like the C family or Java) will define the type of the variable in its source, and the usage of that variable will be derived (usually by the coder) from the type.
静态类型语言(如 C 系列或 Java)将在其源代码中定义变量的类型,并且该变量的用法将(通常由编码人员)从该类型派生。
A dynamically typed language (like Scheme or VBScript) the type of a variable will be defined by its usage. In some cases the coder simply can't define a type for a variable (like in Scheme or Bash script), and in others this is just optional (like VBScript).
动态类型语言(如 Scheme 或 VBScript),变量的类型由其用法定义。在某些情况下,编码人员根本无法为变量定义类型(如在 Scheme 或 Bash 脚本中),而在其他情况下,这只是可选的(如 VBScript)。
The third concept-pair is strong vs. weak typed language. These concept refer to the rules imposed upon relation between variable types in the language (most commonly related to casts). The question of typing system "strength" is not as Boolean as other questions, so most languages fall somewhere between having a strong and week type system.
第三个概念对是强类型语言与弱类型语言。这些概念指的是强加于语言中变量类型之间关系的规则(最常与强制转换相关)。类型系统“强度”的问题不像其他问题那样布尔值,因此大多数语言介于具有强类型系统和周类型系统之间。
In a loosely (weak) typed language the compiler and the runtime will allow you to treat a variable of one type as if it were of another type and the behavior of such a situation is usually language specific (and in some cases even implementation specific). For instance, you can add together a number with a string and this will be considered valid code.
在松散(弱)类型语言中,编译器和运行时将允许您将一种类型的变量视为另一种类型,并且这种情况的行为通常是特定于语言的(在某些情况下甚至是特定于实现的) . 例如,您可以将一个数字与一个字符串相加,这将被视为有效代码。
In a language with a strong type system the compiler and runtime will demand you perform specific actions in order to perform operations between different types of variables. The most common example for this is casting (like casting an int to a float).
在具有强类型系统的语言中,编译器和运行时将要求您执行特定操作,以便在不同类型的变量之间执行操作。最常见的例子是强制转换(例如将 int 转换为浮点数)。
Bottom line
底线
To define PowerShell, it's an interpreted language, but this is a gray area when it comes to .NET. Defining variables in PowerShell does not include defining their type, and so it's obviously a dynamically typed language, and combining variables can be done seamlessly (as @halr9000 noted), which indicates a loose typing system.
要定义 PowerShell,它是一种解释性语言,但对于 .NET 来说,这是一个灰色地带。在 PowerShell 中定义变量不包括定义它们的类型,因此它显然是一种动态类型语言,并且可以无缝地组合变量(如@halr9000 所述),这表明类型系统松散。
In a sentence, I'd say it's an interpreted dynamically typed language with a weak type system.
一句话,我会说它是一种具有弱类型系统的解释型动态类型语言。
回答by klumsy
PowerShell is not a compiled Language. It doesn't produce IL directly either. Version 1 and Version 2 were totally interpreted The new version of PS V3 does indeed produce and cache expressions as IL in the background for speed and optomization purposes, but does not expose any compiled DLLs or such that other languages could call like regular CLR types (though other languages CAN host the PowerShell Engine, and execute script in it.)
PowerShell 不是编译语言。它也不直接产生 IL。版本 1 和版本 2 被完全解释 新版本的 PS V3 确实在后台生成和缓存表达式作为 IL 以实现速度和优化目的,但不公开任何已编译的 DLL 或其他语言可以像常规 CLR 类型一样调用的 DLL(尽管其他语言可以托管 PowerShell 引擎,并在其中执行脚本。)
So think of it like an interpreted language that just happens to live in a Dotnet Ecosystem which allows it to instantiate and interact with DOTNET objects thus making it seem like a "DOTNET language". PowerShell has its own Extended Type System (ETS) that makes it more dynamic.. You can create objects on the fly with whatever properties you want, or take an existing dotnet object, and add stuff to it.
所以把它想象成一种恰好存在于 Dotnet 生态系统中的解释性语言,它允许它实例化 DOTNET 对象并与之交互,从而使它看起来像一种“DOTNET 语言”。PowerShell 拥有自己的扩展类型系统 (ETS),使其更具动态性。您可以使用所需的任何属性动态创建对象,或者获取现有的 dotnet 对象,并向其中添加内容。
PowerShell is a dynamic language. With dynamic scoping. It is a pipeline centric language, which passes rich objects through the pipeline (in contrast to binary/text pipelines in unix)
PowerShell 是一种动态语言。具有动态范围。它是一种以管道为中心的语言,它通过管道传递丰富的对象(与 unix 中的二进制/文本管道相反)
PowerShell is a command (verb and noun) centric language in philosophy and implementation, and though it is a RICH Object language, I wouldn't say its object ORIENTATED. you can interact with objects, and create them, but the goal is to produce task based COMMANDS
PowerShell 在哲学和实现上是一种以命令(动词和名词)为中心的语言,虽然它是一种丰富的对象语言,但我不会说它的对象是面向对象的。您可以与对象交互并创建它们,但目标是生成基于任务的命令
PowerShell lives in different environments. It is a line by line REPL Command line interpreter, but it is also a full scripting engine that can be embedded in other applications.
PowerShell 存在于不同的环境中。它是一个逐行 REPL 命令行解释器,但它也是一个完整的脚本引擎,可以嵌入到其他应用程序中。
PowerShell has dynamic rather than lexical scoping of variables.
PowerShell 具有变量的动态范围而不是词法范围。
PowerShell has many "functional" features, with Scriptblocks being robust Lambdas, and it also (since V2) has full closures. Despite Lambdas being often considered a difficult concept. they flow nicely in powershell are are used by many people who have a hard time programming. In fact in PowerShell every script, or function , or advanced function is really a lamda. PowerSHell's lamdas are different from other lamdas because of the dyanmic scoping and also because they execute in the pipeline. Here is a simple example using built-in cmdlets
PowerShell 具有许多“功能性”特性,其中 Scriptblocks 是强大的 Lambdas,并且(自 V2 起)具有完整的闭包。尽管 Lambdas 通常被认为是一个困难的概念。它们在 powershell 中运行良好,许多编程困难的人都在使用它们。实际上,在 PowerShell 中,每个脚本、函数或高级函数实际上都是 lamda。PowerSHell 的 lamda 与其他 lamda 不同,因为它们具有动态范围,也因为它们在管道中执行。这是一个使用内置 cmdlet 的简单示例
get-process | where { $_.MainWindowTitle -like '*stack*' } | select processname
Here you pass a lamda to the cmdlet where , and for each item coming through the pipeline it is evaluated, and its results go back into the pipeline which are then processed by the select command.
在这里,您将一个 lamda 传递给 cmdlet where ,并且对于通过管道的每个项目进行评估,其结果返回管道,然后由 select 命令处理。
PowerShell (since V2) is a distributed language, with a complete remoting stack, that allows you to connect from one computer to many at the same time, execute commands with throlling, and process results on many streams (results, error, warnings etc) as they are happening on each computer.
PowerShell(自 V2 起)是一种分布式语言,具有完整的远程处理堆栈,允许您同时从一台计算机连接到多台计算机,通过 throlling 执行命令,并处理多个流上的结果(结果、错误、警告等)因为它们发生在每台计算机上。
So What kind of language is PowerShell?
那么PowerShell是一种什么样的语言呢?
Its a command centric language primarily targeting system administration and automation, but also a rich Object pipeline based language that lives in the dotnet ecosystem. Its a dynamic language that is dynamically scoped, with functional language features and the combination of features, I believe make it quite a new innovative language
它是一种主要针对系统管理和自动化的以命令为中心的语言,也是一种生活在 dotnet 生态系统中的基于对象管道的丰富语言。它是一种动态范围的动态语言,具有函数式语言特性和特性组合,我相信使它成为一种新的创新语言
Sadly however PowerShell has lots of Gotchas and issues, and while the learning curve is not steep from beginner to elementary, it is very steep going into intermediate.
可悲的是,PowerShell 有很多陷阱和问题,虽然从初学者到初级的学习曲线并不陡峭,但进入中级却非常陡峭。
回答by Lea Krause
PowerShell uses what's called a dynamically typed scripting language. It can implement complex operations and it supports variables, functions, loops, branching, structured error/exception handling and it integrates with .NET. A dynamically typed language is when the type checking is done at run-time and not at compile-time and its variables have no type but can refer to a values of any type. Examples of dynamically typed languages include PHP, JavaScript, MATLAB, Ruby, Python, and more.
PowerShell 使用所谓的动态类型脚本语言。它可以实现复杂的操作,支持变量、函数、循环、分支、结构化错误/异常处理,并与 .NET 集成。动态类型语言是在运行时而不是在编译时进行类型检查,并且其变量没有类型但可以引用任何类型的值。动态类型语言的示例包括 PHP、JavaScript、MATLAB、Ruby、Python 等。
回答by techdir
Perhaps, even though the term scripting language is used to define PowerShell, there is an inferred/preferred language (source: http://msdn.microsoft.com/en-us/library/dd901838%28v=vs.85%29.aspx): C#. Reason: the PowerShell example of a cmdlet provided by MSDN is preceded by a tab C# notation. This fits: C# is an interpreted language which .NET runtime compiles (called common intermediate language (CIL)) the first time it is run. Futhermore, C# is an object-oriented programming (OOP) language and PowerShell interacts with the objects within Active Directory. See also the http link blogs.technet.com/b/stefan_gossner/archive/2010/05/07/using-csharp-c-code-in-powershell-scripts.aspx. This, however, does not mean you are limited to only C# within PowerShell: see http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28v=vs.85%29.aspx
也许,即使术语脚本语言用于定义 PowerShell,也有一种推断/首选语言(来源:http: //msdn.microsoft.com/en-us/library/dd901838%28v=vs.85%29。 aspx):C#。原因:MSDN 提供的 cmdlet 的 PowerShell 示例前面带有制表符 C# 表示法。这适合:C# 是一种解释性语言,.NET 运行时在第一次运行时对其进行编译(称为公共中间语言 (CIL))。此外,C# 是一种面向对象的编程 (OOP) 语言,PowerShell 与 Active Directory 中的对象进行交互。另请参阅 http 链接blogs.technet.com/b/stefan_gossner/archive/2010/05/07/using-csharp-c-code-in-powershell-scripts.aspx。但是,这并不意味着您只能在 PowerShell 中使用 C#:请参阅http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28v=vs.85%29.aspx

