Multiplicative Operators
Perl provides the C-like operators * (multiply), / (divide), and % (modulo). The * and / work
exactly as you would expect, multiplying or dividing their two operands.
Division is done in floating point, unless you’ve used any of the integer, bigint, bigrat, or bignum pragmatic modules. The %
operator converts its operands to integers before finding the remainder
according to integer division. (However, it does this integer division in
floating point if necessary, so your operands can be up to 15 digits long
on most 32-bit machines.) Assume that your two operands are called
$a and $b. Unlike a mathematical remainder function,
Perl’s % operator defines a sawtooth
function on the real number line that drops to 0 at the origin and at
every multiple of the divisor, ramping up to just under the divisor within
each divisor interval. Unlike a remainder function, it is not symmetric
around 0, but continues the sawtooth pattern on both sides of 0.
Mathematically, $a % $b is
therefore:
$a % $b == $a - ( POSIX::floor($a / $b) * $b )
When use integer is in scope,
% gives you direct access to the
modulus operator as implemented by your C compiler. This operator is not
well defined for negative operands, but it will execute faster.
Binary x is the repetition operator. Actually, it’s two operators. In scalar context, it returns a concatenated string consisting of the left operand repeated the number of times specified by the right operand. (For backward compatibility, it also ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access