Powershell pitfalls

后端 未结 21 1351
梦谈多话
梦谈多话 2020-12-23 01:44

What Powershell pitfalls you have fall into? :-)

Mine are:

# -----------------------------------
function foo()
{
    @(\"text\")
}

# Expected 1, a         


        
21条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 02:31

    The logical and bitwise operators don't follow standard precedence rules. The operator -and should have a higher priority than -or yet they're evaluated strictly left-to-right.

    For example, compare logical operators between PowerShell and Python (or virtually any other modern language):

    # PowerShell
    PS> $true -or $false -and $false
    False
    
    # Python
    >>> True or False and False
    True
    

    ...and bitwise operators:

    # PowerShell
    PS> 1 -bor 0 -band 0
    0
    
    # Python
    >>> 1 | 0 & 0
    1
    

提交回复
热议问题