Print the kind of number

Print whether a number is integer, decimal, positive, negative .. etc.,

 #!/usr/bin/perl
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;
} else {
return $num;
}
}
sub is_numeric { defined scalar &getnum }
$_ = "-0.65";
if (/\D/){
print "is a nondigit"; }
else {
print "is a digit or someother"; }
# warn "is a nondigit"  if /\D/;
warn "not a natural number" unless /^\d+$/;
warn "not a integer" unless /^[-+]?\d+$/;
warn "not an decimal number" unless /^-?\d+\.?\d*$/;
print is_numeric( defined scalar &getnum);
print getnum("A143");

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s