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

后端 未结 7 958
青春惊慌失措
青春惊慌失措 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:51

    Try Scalar::Util::looks_like_number:

    E.g.:

    use Scalar::Util qw(looks_like_number);
    
    if (looks_like_number($thingy)) {
        print "looks like $thingy is a number...\n"
    }
    

提交回复
热议问题