perl

What is the meaning of the % operator?


I saw recently in on an answer here on Stack Overflow where a calculation was done to determine whether a value was odd or even. There was a % used which always returned 0 or 1 depending on the value it was compared with.

I am trying to figure out what the usage is of this % and even though I searched on google, I could only find % used in hash.

$result = $str % 2;

Solution

  • It's the "modulo" operator. See perlop.

    [...] computes the division remainder of its first argument with respect to its second argument.