Perl How to: Find a Given Variable String is numeric or not

This tutorial explains how to check whether a given string is a number or not in Perl, along with code examples.

You can also refer to my previous post on How to Convert String to Number in Perl.

There are several methods in Perl to determine whether a given string is numeric or not:

Perl: How to Check Whether a String is Numeric or Not

There are several methods in Perl to determine whether a given string is numeric or not:

  • Using the Scalar::Util Module:

    The Scalar::Util module provides the looks_like_number() function, which checks whether a given variable is a number or not. For example, looks_like_number("111") returns a boolean value. You can use this function in a conditional if statement to check if a string is numeric.

  • Appending a Variable with Zero:

    Another approach is to append a variable with zero and use the result in a conditional if statement. If the resulting value is numeric, the string is considered numeric.

  • Using the Regexp::Common Module:

    The Regexp::Common module provides regular expressions for checking real and integer numbers. You can use these regular expressions to validate whether a string represents a numeric value.

  • #1 Scalar:Util qw function:

    The Scalar::Util module has a function looks_like_number , which converts string literal into a number and checks result is a number or not.

    Here is an example isNumeric Extension method on the String class

    use warnings;
    use strict;
    
    use Scalar::Util qw(looks_like_number);
    
    my $var1="123";
    print "$var1 contains", looks_like_number($var1) ? '' : ' not', " a number\n";
    print "a",looks_like_number($var1),"test";
    
    my $var2="abc";
    print "$var2 contains", looks_like_number($var2) ? '' : ' not', " a number\n";
    
    my $var3="1a23";
    print "$var3 contains", looks_like_number($var3) ? '' : ' not', " a number\n";
    
    my $var4="__";
    print "$var4 contains", looks_like_number($var4) ? '' : ' not', " a number\n";
    

    Output:

    123 contains a number
    a1testabc contains not a number
    1a23 contains not a number
    __ contains not a number
    

    Another example using conditional expression.

    looks_like_number function also used in Conditional expressions to execute branch of a code based true and false values.

    Conditional if statements are used to execute the appropriate block of code based on the result of the looks_like_number function.

    if a given string is a number, returns a boolean truth value, or else returns a false value.

    Two variables, $strNumberand$str, are defined with example strings.

    use warnings;
    use strict;
    
    use Scalar::Util qw(looks_like_number);
    
    my $strNumber = "123.45";
    my $str = "abc";
    
    if (looks_like_number($strNumber)) {
        print "$strNumber is number.\n";
    } else {
        print "$strNumber is not number.\n";
    }
    
    if (looks_like_number($str)) {
        print "$str is number.\n";
    } else {
        print "$str is not number.\n";
    }
    
  • #2: using regular expression

The Regexp::Common🔗 module is used to process and test regular expressions in Perl.

It provides functions for working with common regular expression patterns.

Before using the module, it needs to be installed in the environment. This can be done using the cpan command, as shown in the code comment.

 cpan Regexp::Common

Two common regular expression patterns are used to checks for real and floating numbers

  • $RE{num}{real} checks for real numbers.
  • $RE{num}{int} checks for integers.

The is_number subroutine is defined to check if a given string is a number. It uses the $RE{num}{real} pattern to match real numbers.

Test cases are provided to demonstrate the usage of the is_number subroutine.

Here is an example

 use warnings;
use strict;
use Regexp::Common;

is_number("111");
is_number("abc");
is_number("123.11");

# Sub routine for Checking a Number
sub is_number {
    if ($_[0] =~ /$RE{num}{real}/) {
        print "$_[0] is a number\n";
    }
    else{
        print "$_[0] is not a number\n";
     }
}
  • #3 Append a variable with zero using the + operator:

The third approach involves appending a variable with zero using the + operator and then checking if the result is equal to a numeric value.

However, it is not recommended to use this approach since you have to compare the result with a given value.

use warnings;
use strict;

my $var1="123";
if ( $var1 + 0 eq 123) {
    print "$var1 is a number\n";
}
else{
    print "$var1 is not a number\n";
}

The same functionality can be achieved using the eval function.

eval is a function used to execute expressions. In this example, the expression {$var1 += 0} appends the value with zero.

use warnings;
use strict;

my $var1="123";
if (eval { $var1 += 0 }) {

    print "$var1 is a number\n";
}
else{
    print "$var1 is not a number\n";
}

In summary, these examples cover different ways to check whether a string is numeric or not.