I am using Perl to convert some XML to JSON. If the XML attribute is a number, I don\'t want to put quotes around it so that JSON will treat it as a number and not a string. How
Assuming you don't need to support unusual stuff (like sci-notation) this almost works (and is very simple):
#!/usr/bin/perl
my $foo = '1234.5';
if( $foo =~ /\d+/ ){
print "$foo is a number\n";
}
The reason it doesn't fully work is because you can have hyphens and dots anywhere (as many as you please) as long as you have at least one digit present). '--1--2' evaluates as zero, and '1.2.3.4.5' evals as 1.2 (the second dot and everything after are ignored). This may or may not be an issue for you.