Perl operators and C operators are similar in type and have the same priority
Perl operators and C operators are similar in type and have the same operator precedence.
Perl operators
Perl operator. We have picked up the same operators in C language and the ones with similar functions. Addition operator, subtraction operator, subtraction operator, subtraction operator, remainder operator, arithmetic operator such as increment / decrement, numerical comparison operator, logical operator, single term operator, shift operator, arrow operator, The three-term operator is taken up.
# Addition operator my $ret = $x + $y; # Subtraction operator my $ret = $x-$y; # Subtraction operator my $ret = $x * $y; # Subtraction operator my $ret = $x / $y; #Remainder operator my $ret = $x%$y; #Increment / decrement my $ret = ++ $x; my $ret = $x ++; my $ret = --$x; my $ret = $x-; # Numerical comparison operator my $ret = $x> $y; my $ret = $x> = $y; my $ret = $x <$y; my $ret = $x <= $y; my $ret = $x == $y; my $ret = $x! = $y; # Logical operator my $ret = $x && $y; my $ret = $x || $y; my $ret =! $x; # Unary operator my $ret = + $x; my $ret =-$x; #Right shift operator my $ret = $x << $y; #Left shift operator my $ret = $x >> $y; # Substitution operator my $ret = $x; my $ret + = $x; my $ret-= $x; my $ret * = $x; my $ret / = $x; my $ret%= $x; my $ret >> = $x; my $ret << = $x; #Arrow operator my $ret = $foo->{x}; # If you define an accessor, the above can be written as my $ret = $foo->x; # Ternary operator my $ret = $flag? $x: $y;
C operator
C language operator. Addition operator, subtraction operator, subtraction operator, subtraction operator, remainder operator, arithmetic operator such as increment / decrement, numerical comparison operator, logical operator, single term operator, shift operator, arrow operator, The three-term operator is taken up.
// Addition operator int32_t ret = x + y; // Subtraction operator int32_t ret = x --y; // Multiplication operator int32_t ret = x * y; // Division operator int32_t ret = x / y; // Residual operator int32_t ret = x%y; // Increment / Decrement int32_t ret = ++ x; int32_t ret = x ++; int32_t ret = --x; int32_t ret = x-; // Numeric comparison operator int32_t ret = x> y; int32_t ret = x> = y; int32_t ret = x <y; int32_t ret = x <= y; int32_t ret = x == y; int32_t ret = x! = y; // Logical operator int32_t ret = x && y; int32_t ret = x || y; int32_t ret =! x; // Unary operator int32_t ret = + x; int32_t ret = -x; // Right shift operator int32_t ret = x << y; // Left shift operator int32_t ret = x >> y; // Assignment operator int32_t ret = x; int32_t ret + = x; int32_t ret-= x; int32_t ret * = x; int32_t ret / = x; int32_t ret%= x; int32_t ret >> = x; int32_t ret << = x; // Arrow operator int32_t ret = foo->x; // Ternary operator int32_t ret = flag? x: y;
Comparison of Perl operators and C operators
Numerical operators, numeric comparison operators, single-term operators, logical operators, shift operators, increment / decrement, assignment operators, and ternary operators of Perl operators have the same functions as C language.
The assignment operator is the same as in C in that it returns an expression. It is evaluated as an assignment expression, not an assignment statement.
Perl's arrow operators are similar to C in that you can access fields through references.
If you define a method for accessing a field called an accessor, Perl field access and C field access will look the same when you get the value.
The C language has a ternary operator, but it also exists in Perl. An operator that can return a value depending on the condition.
Perl operator precedence
Perl operator precedence. I am writing from the highest one.
Left join->Unbound++ - Right join! Unary + unary- Left join * /%x Left join +- Left join << >> Unbonded <> <=> = Unbonded ==! = Left join && Left join || Assignment operators such as right join = + =-= * =
C language operator precedence
The precedence of C language operators. I am writing from the highest one.
Left join ++-->Rightjoin! Unary + unary-++- Left join * /% Left join +- Left join << >> Left join <=>> = Left join ==! = Left join && Left join || Right join = + =-= * = / =%= << = >> = and other assignment operators
Comparison of Perl operator precedence and C operator precedence
Let's compare the precedence of Perl operators with the precedence of C operators. In Perl, it is the same as in C, except that the arrow operator has higher precedence than the increment / decrement operator.
In Perl, you can use the operator precedence that you learned in C almost as it is.
Some people over-criticize C-language operators and operator priorities without being based on facts, such as being prone to bugs.
Perl, on the other hand, accepts the C language operator precedence as it is.
This means that Perl takes care so that users who have learned the C language can use their knowledge as it is without any confusion.
C language is an introductory material and is a programming language that can be said to come out without fail. Perl is ready for users familiar with the C language to get started quickly without any confusion.
Other similarities between Perl and C
In this article, I wrote that Perl's operator types and operator priorities are similar to C, but Perl's if statements, for statements, while statements, increments / decrements, and operator priorities. The order, scope, and reference concept are similar to the C language grammar.
- Perl if statements are similar to C if statements
- Perl for statement has the same grammar as C language for statement
- Perl while statement has the same grammar as C language while statement
- Perl increments / decrements work the same as in C
- Perl can use a single scope in the same way as C
- Perl reference functionality is the same as C pointer functionality