微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Perl中的increment()和decrement( – )字符串

with perl -e’$ string =“a”; print $ string;’我们得到b,
但是用perl -e’$ string =“b”; print – $ string;’我们得到-1。

所以,如果我们可以递增为什么我们不能递减?

EDITED
自动减量运算符不是神奇的”perlop

Perl给了我们很多设施,为什么不是这个?这不是批评,但不会预期类似的操作符的类似行为?有什么特别的原因吗?

解决方法

perlop(1)解释说这是真的,但没有给出理由:

The auto-increment operator has a little extra builtin magic to it. [If applicable,and subject to certain constraints,] the increment is done as a string,preserving each character within its range,with carry[…]

The auto-decrement operator is not magical.

得到-1的原因是因为当解释为数字时,“b”变成0,因为它没有前导数字(相反,“4b”变成4)。

原文地址:https://www.jb51.cc/Perl/173237.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐