0

Question is just as the title says. Let's say a script prompts the user to input something, and stores said input into a variable. What's the easiest way to check if that user input/variable is a number in perl?

Eric
  • 41
  • 1
  • 8

1 Answers1

4

From the Perl FAQ How do I determine whether a scalar is a number/whole/integer/float?:

There are also some commonly used modules for the task. Scalar::Util (distributed with 5.8) provides access to perl's internal function looks_like_number for determining whether a variable looks like a number. Data::Types exports functions that validate data types using both the above and other regular expressions. Thirdly, there is Regexp::Common which has regular expressions to match various types of numbers. Those three modules are available from the CPAN.

Perl comes with excellent documentation. You should review everything linked from perldoc perltoc at least once a year.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Also, look at Unicode::UCD::num(). It returns undef if its string argument does not have a consistent numeric value; otherwise it returns the numeric quantity represented. It is looking for sequences of numeric characters, so no decimal points, nor signs, nor scientific notation. But it can be used to catch spoofing where look-alike digits from different scripts are used to give a visual appearance of a different number than actually represented – khw May 10 '17 at 18:08