in reply to How to check if a scalar value is numeric or string?

This should give you a fairly accurate answer i.e. suppress warnings locally and compare the value to itself after adding a numeric value to it.
use strict; use warnings; my $integer = 777; my $string = 'hello'; my $float = 3.22 + 4.34; print _isNumeric($integer) . "\n"; print _isNumeric($string) . "\n"; print _isNumeric($float) . "\n"; sub _isNumeric { my ($value) = @_; no warnings; return 1 if ($value + 0) eq $value; return 0; }

Replies are listed 'Best First'.
Re^2: How to check if a scalar value is numeric or string?
by ikegami (Patriarch) on Aug 27, 2009 at 16:10 UTC
    That's a pretty good reimplementation of looks_like_number, but it doesn't answer the OP's question. He still gets the same answer he currently gets.
    sub _isNumeric { my ($value) = @_; no warnings; return 1 if ($value + 0) eq $value; return 0; } my $var = { 'key1' => '1', 'key2' => 1 }; print "Key1: It is a number\n" if _isNumeric( $var->{'key1'} ) ; print "Key2: It is a number\n" if _isNumeric( $var->{'key2'} ) ;
    Key1: It is a number Key2: It is a number