加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在Perl中查找标量变量的数据类型

发布时间:2020-12-15 21:58:01 所属栏目:大数据 来源:网络整理
导读:我有一个接受用户输入的功能.输入可以是整数,浮点数或字符串.我有三个重载函数,应根据输入数据的DATA TYPE调用.例如,如果用户输入整数(例如100),则应调用具有整数参数的函数.如果用户输入字符串(例如“100”),则应调用具有字符串参数的函数. 所以我需要找出
我有一个接受用户输入的功能.输入可以是整数,浮点数或字符串.我有三个重载函数,应根据输入数据的DATA TYPE调用.例如,如果用户输入整数(例如100),则应调用具有整数参数的函数.如果用户输入字符串(例如“100”),则应调用具有字符串参数的函数.

所以我需要找出输入数据的数据类型.使用正则表达式,我能够区分整数和浮点数(因为我只需要找出类型,我不喜欢使用cpan.org提供的库),但我无法弄清楚如何区分字符串中的整数. Perl对待“100”和100对待?有没有办法解决这个问题?

解决方法

来自perldoc perldata:

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会愉快地从数字转换为字符串,反之亦然.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读