In Perl, how can I tell if a string is a number?

后端 未结 7 944
青春惊慌失措
青春惊慌失措 2021-02-14 22:33

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

7条回答
  •  遥遥无期
    2021-02-14 22:44

    You could just force it into a number then compare that to the original string.

    if( $value eq $value+0 ){
      print "$value is a number\n";
    }
    

    ( Note: it will only work for simple numbers, like 123 or 12.3 )

提交回复
热议问题