Perl: checking if a value is an integer

Articles may may have files attached at the end of the post

While some languages offer a isint function, I could not find it in perl.
This tutorial will show how to find if a variable contains an integer or not.

  1. #!/usr/bin/env perl
  2. #
  3. use strict;
  4. use warnings;
  5.  
  6. my @values = ("word and word", "12", 12, "a23");
  7.  
  8. sub isint{
  9.   my $val = shift;
  10.   return ($val =~ m/^\d+$/);
  11. }
  12.  
  13. foreach (@values){
  14.   if(isint($_)){
  15.       print $_." is an integer\n";
  16.   }else{
  17.     print $_." is NOT an integer\n";
  18.   }
  19. }

will ouput:

$ ./isint.pl 
word and word is NOT an integer
12 is an integer
12 is an integer
a23 is NOT an integer