# Perl operator Details _ Basics Tutorial

Source: Internet
Author: User
Arithmetic operators: + (plus),-(minus), * (multiply),/(except), * * (Power),% (take more),-(Monocular negative)
(1) The cardinality of the power can not be negative, such as ( -5) * * 2.5 # error;
(2) The result of power can not exceed the limit of computer expression, such as 999999 # error
(3) The operand of the remainder, if not an integer, rounded to an integer; the right-hand side of the operator cannot be zero
(4) Monocular negative can be used for variables:-\$y; # equivalent to \$y *-1
Second, the integer comparison operator

Table 3.1. integer comparison operator

 operator description greater than == equals less than or equal to is greater than or equal to

The operator <=> result is:
0-Two values equal
1-The first value is large
1-The second value is large
Three, string comparison operator

Table 3.2. string comparison operator

 Operator Describe LT Less than GT Greater than eq Equals le Less than or equal to GE Greater than or equal to NE Not equal to CMP Compare, return 1, 0, or-1

Four, the logical operator
Logic or: \$a | | \$b or \$a or \$b
Logic with: \$a && \$b or \$a and \$b
Logical Non:! \$a or not \$a
Logical XOR OR: \$a xor \$b
Five, bitwise operator
Bit and:&
Bit or: |
Bit non: ~
Bitwise XOR OR: ^
Move left: \$x << 1
Right Shift: \$x >> 2
Note: Do not use & for negative integers, because Perl will convert them to unsigned numbers.
Six, assignment operator

Table 3.3. Assignment operator

 Operator Describe = Assignment only += Addition and assignment -= Subtraction and assignment *= Multiplication and assignment /= Division and Assignment %= Remainder and assignment **= Exponentiation and Assignment &= Bitwise AND Assignment |= Bitwise OR and Assignment ^= Bitwise XOR and Assignment

Table 3.4. Assignment Operator Example

 An expression An equivalent expression \$a = 1; None (Basic Assignment) \$a-= 1; \$a = \$a-1; \$a *= 2; \$a = \$a * 2; \$a/= 2; \$a = \$a/2; \$a%= 2; \$a = \$a% 2; \$a **= 2; \$a = \$a * * 2; \$a &= 2; \$a = \$a & 2; \$a |= 2; \$a = \$a | 2; \$a ^= 2; \$a = \$a ^ 2;

. = Multiple occurrences can occur in an assignment statement, such as:
\$value 1 = \$value 2 = "a string";
. = as a child expression
(\$a = \$b) + + 3;
Equivalent to
\$a = \$b;
\$a + 3;
It is not recommended to use this approach.
Vii. self-reducing operator: + + 、--(same as in C + +)
. Do not use this operator on both sides of a variable: + + \$var--# Error
. Do not use it again in the same expression after the variable has been increased/reduced: \$var 2 = \$var 1 + + + + \$var 1; # error
. in Perl, + + can be used for strings, but when the end character is ' z ', ' z ', ' 9 ', such as:
\$stringvar = "ABC";
\$stringvar + +; # \$stringvar contains ' Abd ' now

\$stringvar = "ABC";
\$stringvar + +; # \$stringvar contains ' ABD ' now

\$stringvar = "Abz";
\$stringvar + +; # \$stringvar now contains ' aca '

\$stringvar = "Agzzz";
\$stringvar + +; # \$stringvar now contains ' ahaaa '

\$stringvar = "AB4";
\$stringvar + +; # \$stringvar now contains ' AB5 '

\$stringvar = "bc999";
\$stringvar + +; # \$stringvar now contains ' bd000 '
. Do not use--,perl will first convert the string to a number and then subtract from it
\$stringvar = "ABC";
\$stringvar--; # \$stringvar =-1 now

. If the string contains non-alphanumeric characters, or digits in letters, the value of the + + operation is converted to a numeric zero, so the result is 1, such as:
\$stringvar = "Ab*c";
\$stringvar + +;
\$stringvar = "ab5c";
\$stringvar + +;
Eight, string joins and repeat operators
Join:.
Repeat: X
Join and assign a value (similar to + =):. =
Cases:
\$newstring = "T" x 5;
\$a = "be";
\$a. = "Witched"; # \$a is now ' bewitched '
Nine, comma operator
The preceding expression is preceded by an operation, such as:
\$var 1 = 1, \$var 2 = \$var 1;
Equivalent to
\$var 1 = 1;
\$var 2 = \$var 1;
The only reason to use this operator is to improve the readability of the program and to combine two expressions that are closely related, such as:
\$val = 26;
\$result = (+ + \$val, \$val + 5); # \$result = 32
Note that the meaning is different if there are no parentheses here:
\$val = 26;
\$result = + + \$val, \$val + 5; # \$result = 27
Ten, the condition operator
Similar to C, the condition is 1: The value 2, when the condition is true, the value 1, the false time value 2, such as:
\$result = \$var = = 0? 14:7;
\$result = + (\$divisor = = 0 0: \$dividend/\$divisor);
In PERL 5, you can also use a conditional operator on the left side of an assignment to select a variable that is assigned a value, such as:
\$condvar = = 43? \$var 1: \$var 2 = 14;
\$condvar = = 43? \$var 1 =: \$var 2 = 14;
Xi. Order of Operators

Table 3.6. Operator Order

 Operator Describe ++, -- Self-increasing, self-reducing -, ~, ! Monocular ** Squares =~, !~ Pattern matching *, /, %, x Multiply, divide, take over, repeat +, -, . Add, subtract, join <<, >> Shift - e,- R, etc. File status <, <=, >, >=, lt, le, gt, ge Unequal comparison = =, !=, <=>, eq, ne, cmp Equality comparisons & Bit and |, ^ Bit or, bit XOR or && Logic and || Logical OR .. List range ? and : Conditional operator =, +=, -=, *=, assigning values And so on , Comma operator not Low-precedence Logical Not and Low-precedence Logical AND or, xor Low-precedence logical OR and XOR

. Operator Associativity (associativity):

Table 3.7. Operator associativity

 Operator Combination of ++, -- No -, ~, ! Right-to-left ** Right-to-left =~, !~ Left-to-right *, /, %, x Left-to-right +, -, . Left-to-right <<, >> Left-to-right - e,- R, No <, <=, >, >=, lt, le, gt, ge Left-to-right = =, !=, <=>, eq, ne, cmp Left-to-right & Left-to-right |, ^ Left-to-right && Left-to-right || Left-to-right .. Left-to-right ? and : Right-to-left =, +=, -=, *=, Right-to-left And so on , Left-to-right not Left-to-right and Left-to-right or, xor Left-to-right

Recommendation:
1, when you are unsure whether an operator executes first, be sure to use parentheses.
2, use multirow, spaces, and so on to improve the readability of the program.

Related Keywords:
Related Article

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: info-contact@alibabacloud.com 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.