Perl's increment / decrement behavior is the same as in C

The behavior of increment / decrement in C is the same as in C. Perl's grammar is based heavily on C, and ink / decrement can be used with the same grammar as C.

Comparison of Perl increment and C language increment

Perl increments

Perl increment. Perl increments increment the number stored in the variable by 1. If the increment is prefixed, the evaluated value is returned. If the increment is postfixed, the value before evaluation is returned.

This behavior is exactly the same as in C.

# Perl increment
$num ++;
++ $num;

C language increment

C language increment. C language increment. C language increments increment the number stored in the variable by 1. If the increment is prefixed, the evaluated value is returned. If the increment is postfixed, the value before evaluation is returned.

#C language increment
num ++;
++ num;

Comparison of Perl Decrement and C Decrement

Perl Decrement

Perl decrement. Perl decrement decrements the number stored in the variable by one. If decrement is prefixed, the evaluated value is returned. If the decrement is postfixed, the pre-evaluation value is returned.

This behavior is exactly the same as in C.

# Perl decrement
$num--;
--$num;

C language decrement

C language decrement. C language decrement. C language decrement decrements the number stored in the variable by one. If decrement is prefixed, the evaluated value is returned. If the decrement is postfixed, the pre-evaluation value is returned.

#C language decrement
num--;
--num;

You can use the increment / decrement grammar you learned in C as it is in Perl

You can use the increment / decrement grammar you learned in C as it is in Perl.

This means that Perl has adopted the C grammar that users are accustomed to.

Perl is a programming language that has a high affinity with the C language.

I think the criticism of C language regarding increment / decrement is trivial

Some people speak very critically about the C language's increment / decrement specification.

  • Because the meaning changes between the prefix and the suffix, it is difficult to understand and it is easy to create bugs
  • Postfix increments are complicated to parse
  • The value is no longer immutable

But don't you think this is a trivial matter? Don't you feel the discussion like poking the corner of a heavy box?

Somehow, it's like putting yourself on the shelf and over-criticizing a person's little bad things.

If you get caught up in this kind of discussion, you might want to leave the place quickly.

Rather, why not re-evaluate Perl, which uses the widely used C grammar as it is.

Other similarities between Perl and C

In this article, I wrote that Perl's increment / decrement is similar to C, but Perl's for statements, if statements, while statements, operator types and priorities, scopes, and reference concepts are also C. Similar to language.

Associated Information