所以我需要找出输入数据的数据类型.使用正则表达式,我能够区分整数和浮点数(因为我只需要找出类型,我不喜欢使用cpan.org提供的库),但我无法弄清楚如何区分字符串中的整数. Perl对待“100”和100对待?有没有办法解决这个问题?
解决方法
Scalars aren’t necessarily one thing or another. There’s no place to declare a scalar variable to be of type “string”,type “number”,type “reference”,or anything else. Because of the automatic conversion of scalars,operations that return scalars don’t need to care (and in fact,cannot care) whether their caller is looking for a string,a number,or a reference. Perl is a contextually polymorphic language whose scalars can be strings,numbers,or references (which includes objects). Although strings and numbers are considered pretty much the same thing for nearly all purposes,references are strongly-typed,uncastable pointers with builtin reference-counting and destructor invocation.
因此,对于整数标量,您只需要提前决定如何处理它们.根据上下文,Perl会愉快地从数字转换为字符串,反之亦然.