Is there an operator in perl, or even a function in some module, that will get the magnitude of a number similar to what is denoted by the mathematical notation |x|.
e.g.
x = 42, y = -42
|x| = |y| = 42
If there isn’t one, what is the simplest way of extracting such a magnitude? I can think of two that I have used before:
$x = -42;
# quick regex for whole integers, can be extended to take decimals into account
$y = $x =~ /(\d+)/;
# Using if statements
$y = $x*-1 if $x < 0;
I would have to create a new function to use either of these examples if I wanted to get the magnitude without adding extra lines to my code (I need to retrieve these magnitudes quite often). I am concerned that I might have missed something even easier.
There’s the
absfunction for that.