How to check a variable content if only digits in Perl?

How can I check if a variable only contains digits (numbers) ?

If $a only contains digits{



Open in new window


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

if ($a =~m/\D) {
  # here be it having non-digits
} else {
  # here be digits only
Pretty sure that's right...
Oops, syntax error there...
Make that:

if ($a =~ m/\D/) {

From the Perl FAQ

   How do I determine whether a scalar is a number/whole/integer/float?
       Assuming that you don't care about IEEE notations like "NaN" or "Infinity", you probably just want to use a regular

               if (/\D/)            { print "has nondigits\n" }
               if (/^\d+$/)         { print "is a whole number\n" }
               if (/^-?\d+$/)       { print "is an integer\n" }
               if (/^[+-]?\d+$/)    { print "is a +/- integer\n" }
               if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
               if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number\n" }
               if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
                               { print "a C float\n" }

       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.

       If you're on a POSIX system, Perl supports the "POSIX::strtod" function.  Its semantics are somewhat cumbersome, so
       here's a "getnum" wrapper function for more convenient access.  This function takes a string and returns the number it
       found, or "undef" for input that isn't a C float.  The "is_numeric" function is a front end to "getnum" if you just
       want to say, "Is this a float?"

               sub getnum {
                       use POSIX qw(strtod);
                       my $str = shift;
                       $str =~ s/^\s+//;
                       $str =~ s/\s+$//;
                       $! = 0;
                       my($num, $unparsed) = strtod($str);
                       if (($str eq '') || ($unparsed != 0) || $!) {
                                       return undef;
                       else {
                               return $num;

               sub is_numeric { defined getnum($_[0]) }

       Or you could check out the String::Scanf module on the CPAN instead. The "POSIX" module (part of the standard Perl
       distribution) provides the "strtod" and "strtol" for converting strings to double and longs, respectively.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

TolgarAuthor Commented:

How can I make digits only  in IF and non-digtis in ELSE using this method?

I think it would be:

if ($a !~ m/\D) {
  # here be it having digits
} else {
  # here be non-digits only

Word of warning... if you try these, be sure to test using a totally blank variable - I'm not positive which set that would fall into.
TolgarAuthor Commented:
Well, these back and forth slashes make the code little weird. It changes the rest of the code in terms of variable declarations and so on. I can even see it before compiling by looking at the color on emacs.

How can I fix it?

 I tried to put // but it did not work.

Again, as per the FAQ

if ( $a =~ /^\d+$/)  {
   print "$a only contains digits\n";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sorry, if you put exactly what I said, I screwed up in the exact same spot...
I forgot to include the forward slash at the end:

if ($a !~ m/\D/) {
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.