http://qs321.pair.com?node_id=408918


in reply to Re^2: Detecting if a scalar has a number or string
in thread Detecting if a scalar has a number or string

If it acts just like a number as far as Perl is concerned, then it's a number as far as I'm concerned.

You can let Perl tell you what it thinks then...

#!perl use strict; use warnings; sub seems_like_number { my $thing = shift; use warnings qw/FATAL all/; # Promote warnings to fatal, so # they can be trapped. The effect is # lexically scoped. eval { $thing += 0; }; return $@ ? 0 : 1; } while ( <DATA> ){ chomp; if( seems_like_number( $_ ) ) { printf "%10s seems like a number.\n", $_; } else { printf "%10s doesn't seem like a number.\n", $_; } } __DATA__ 1234 1.234 -2.345 1-1+2 0.032 .321 ASDF ASDF1234 1234ASDF

The first line after __DATA__ is intentionally blank, to test whether or not an empty string will qualify as a number... and of course it won't.

Ok, now if Perl thinks it's not a number, you'll know about it, and if Perl doesn't object to it being considered a number, you'll know that too.


Dave