![]() ![]() Some comparison operators, as their associativity, chain with some operators of the same precedence (but never with operators of different precedence). A few operators such as &= have special evaluation rules that can result in an operand not being evaluated at all in general, the top-level operator in an expression has control of operand evaluation. In fact Perl has a general rule that the operands of an operator are evaluated in left-to-right order. But the order of operations is not fully determined by this: in 2 * 2 + 4 * 5 both multiplications must be performed before the addition, but the grouping does not say anything about the order in which the two multiplications are performed. For example, in 2 + 4 * 5, the grouping implied by precedence means that the multiplication of 4 and 5 must be performed before the addition of 2 and 20, simply because the result of that multiplication is required as one of the operands of the addition. So the expression yields 6 - 2 = 4, rather than 9 - 1 = 8.įor simple operators that evaluate all their operands and then combine the values in some way, precedence and associativity (and parentheses) imply some ordering requirements on those combining operations. It is as if the expression were written (9 - 3) - 2, not 9 - (3 - 2). For example, in 9 - 3 - 2, subtraction is left associative, so 9 - 3 is grouped together as the left-hand operand of the second subtraction, rather than 3 - 2 being grouped together as the right-hand operand of the first subtraction. ![]() Operator associativity defines what happens if a sequence of the same operators is used one after another: usually that they will be grouped at the left or the right. So the expression yields 2 + 20 = 22, rather than 6 * 5 = 30. It is as if the expression were written 2 + (4 * 5), not (2 + 4) * 5. For example, in 2 + 4 * 5, the multiplication has higher precedence, so 4 * 5 is grouped together as the right-hand operand of the addition, rather than 2 + 4 being grouped together as the left-hand operand of the multiplication. Operator precedence means some operators group more tightly than others. Operator precedence and associativity work in Perl more or less like they do in mathematics. There are a few exceptions though: x can be either string repetition or list repetition, depending on the type of the left operand, and &, |, ^ and ~ can be either string or numeric bit operations. For example $x = $y compares two numbers for equality, and $x eq $y compares two strings. It also means that Perl has two versions of some operators, one for numeric and one for string comparison. This is in contrast to many other dynamic languages, where the operation is determined by the type of the first argument. For example $x + $y is always a numeric addition, and if $x or $y do not contain numbers, an attempt is made to convert them to numbers first. In Perl, the operator determines what operation is performed, independent of the type of the operands. ![]() Perlop - Perl operators and precedence #DESCRIPTION Gory details of parsing quoted constructs. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |