How to get the module numbers in Perl

Task. In Perl, the program has a variable which contains the number. You need to get the module of that number. The absolute value (absolute value) is this number which is minus removed (if it was).

A few examples:

  • The absolute value of 4 is the number 4.
  • The absolute value of 0 is the number 0.
  • The absolute value of -18 is 18.
  • The absolute value of -1.576 is 1.576.

The decision

The solution is to use the function abs, which is part of the Perl language.

Here is an example program which uses this function:

▶ Run

my $number = -14;

print abs($number);

The program will display the text 14.

Official documentation

Here is the output of the command perldoc -f abs:

    abs VALUE
    abs     Returns the absolute value of its argument. If VALUE is omitted,
            uses $_.

Other articles