Perl data type checking

I am using perl 5.8.0.

I need to check some values to see it they are floats. Our system does not have Data::Types so I can't use is_float. Is there something else that I can use? The only thing in Data is Dump.pm. I am not allowed to download anything to our system so I have to use what I have. Does anyone have any ideas? Thanks.

Allyson

Perl internally doesn't have different scalar types, all numbers are simply scalars. Depending on what you want, you could check if the number has a fractional part, or use a regular expression match.

if ($scalar =~ m/[^-+.\d]/ || $scalar !~ /\d/) { $what = "string, not number" }
elsif ($scalar == int($scalar)) { $what = "integer" }
else { $what = "float" }

The first condition will specifically require all numbers to have a digit, so "." doesn't evaluate to "0.0". If you want to change that, you could change the second regular expression. Perhaps it should be stricter and check that there is at most one decimal point, and that the sign comes before the numbers, if present. But hopefully this should at least get you going.

Thanks. That really helped.

Allyson

further reading:

perlfaq4 - perldoc.perl.org