Skip to content

Instantly share code, notes, and snippets.

@devoid
Created February 3, 2012 07:25
Show Gist options
  • Save devoid/1728749 to your computer and use it in GitHub Desktop.
Save devoid/1728749 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# Test whether a scalar in perl is numeric or not.
#
# ( ( $i + 0 ) ne $i ) ? "not numeric" : "numeric";
#
# - ` $i + 0 ` converts scalar to numeric
# - which should then not equal $i unless it is a numeric
#
use strict;
use String::Random qw(random_regex random_string);
print "value\tis a numeric\n";
for my $i (qw(foo bar 1 2.3 -23.5 -2 0 -5bc b23 b23.5 23.b 5.5b bin inf nan -inf)) {
my $x = "yes";
$x = "no" if(($i + 0) ne $i);
print "$i\t$x\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment