The Perl language Primer--2--perl operator

Source: Internet
Author: User
Tags arithmetic operators perl operator

One, arithmetic operators

* *: is a power result cannot exceed a range when the exponent is a decimal, the base cannot be a negative number

%: The number of operands on both sides of the remainder is an integer, if not to intercept, to remove all the fractional parts

Note: When a string participates in an operation and needs to be converted to an integer, the value is zero if it cannot be converted to an integer, ' 2 ' +1=3 ' a ' +1=1

Second, numeric comparison operators

Greater than or equal: The result of the comparison is true or nonzero, false or 0

<=>: comparison operation $a <=> $b

When a is greater than B: The value is 1

When a is less than B: The value is-1

When a equals B: The value is 0

When using this operator, the operand is automatically converted to an integer, and the value of zero is not converted to an integer.

Note: Because the floating-point number is not accurate, do not compare the numbers with similar values.

Three, string comparison

Iv. string connection and repetition

. : The point is the connector $a = ' a '. ' B '-to ' AB '

In print, you can write print directly $a $b-the same as print $a. $b

X: Repeat (there are spaces before and after X, in order to be separated from the variable name) ' A ' x 5---' AAAAA '

Returns an empty string if the number of repetitions is less than 1

Four, logical operators

&& (and) | | (OR)! (not) XOR

This logical operator is left-to-right with the value of the Boolean type.

Five, assignment operators

$a +=1--is equivalent to $ A = $a +1

Can even wait: $a = $b =3---$a =3 $b =3

Vi. self-increasing self-reduction

++var--$var 2= $var 1 + + + $var 1 #这两种用法是不行的

1, can be used as a string of self-increment: $a = ' AZ '; $a + + becomes $a = ' ba '

2, can not be used as a string of self-subtraction: When $a--by the number operation, the string first converted to 0 and then self-subtraction

3, when there are numbers in the string, or non-alphabetic and numeric symbols (such as #[email protected]#$%^), the self-increment is first changed to 0 and then self-increment

Like ' ab$c ' we5a '

4, pre-increase: $b =++ $a # $a first increment in the assignment # $a =1 $b =++ $a; $a =2 $b =2

Post-increment: $b = $a + + # $a first assign and then increment # $a =1 $b = $a + +; $a =2 $b =1

Seven, comma

Is the function of a connection: $a +=1, $b = $a; ==> $a +=1; $b = $a;

No eggs.

Eight, conditional operators

Three operands: evaluates the conditional expression first, and executes the question mark (? The following operation, which executes a colon when false (:) After the operation.

$result = $var = = 0? 14:7 #一般只用于简单的条件 too complex is not as good as if statement looks intuitive

Ix. Precedence of operators (precedence)

Self-increment self-reduction highest, single operand higher than multi-operand (that is, value, assignment, such as higher than plus minus), numeric operations > Comparison operations (greater than or equal to what, but greater than less than (<>) higher than equals (= =) and not equal (! =))

> Bitwise arithmetic > Assignment operation (=) > Logical operation

X. The binding nature (associativity)

Binding associativity: When more than one symbol in an expression is the same priority, which is the first, which is the calculation? Is the left (left-associative), or the right (right-associative)

Usually left-associative: first, left, then right.

Right binding: Except (* *), generally not seen at all

For a chestnut: $a = $b =1 #一起赋值

$a = $b +=1 #先算b加一 re-assigned to a

When precedence is not known, parentheses are added to resolve all issues

The Perl language Primer--2--perl operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.