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

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

    I think (from recent experiences) that you're making a mistake doing any kind of manual XML->JSON conversion. I encountered many gotchas in the process, not least of which involved incorrectly escaped characters.

    I would recommend parsing your XML with one of the many XML::* modules (I used XML::Simple) and then rendering it as JSON using JSON::XS. JSON::XS allows you to convert a Perl data structure to JSON; XML::Simple parses XML to a Perl data structure. In the mean time you can manipulate the Perl data structure as you wish.

    Bottom line is that you no longer care about quoting/escaping characters.

提交回复
热议问题