检查Perl中的字符串是否为空的正确方法是什么? [英] What is the proper way to check if a string is empty in Perl?

查看:376
本文介绍了检查Perl中的字符串是否为空的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用此代码检查字符串是否为空:

I've just been using this code to check if a string is empty:

if ($str == "")
{
  // ...
}

也与不等于运算符相同...

And also the same with the not equals operator...

if ($str != "")
{
  // ...
}

),但我不知道这是正确的方式,或者如果有任何不可预见的缺点。

This seems to work (I think), but I'm not sure it's the correct way, or if there are any unforeseen drawbacks. Something just doesn't feel right about it.

推荐答案

对于Perl中的字符串比较,使用 eq ne

For string comparisons in Perl, use eq or ne:

if ($str eq "")
{
  // ...
}

== != 运算符是数字比较运算符。在比较之前,他们将尝试将两个操作数转换为整数。

The == and != operators are numeric comparison operators. They will attempt to convert both operands to integers before comparing them.

查看 perlop 手册页获取更多信息。

See the perlop man page for more information.

这篇关于检查Perl中的字符串是否为空的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆