What is the proper way to check if a string is empty in Perl?

后端 未结 6 1887
日久生厌
日久生厌 2020-12-25 09:18

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

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

And also the same with the not equals operato

6条回答
  •  天命终不由人
    2020-12-25 09:45

    You probably want to use "eq" instead of "==". If you worry about some edge cases you may also want to check for undefined:

    if (not defined $str) {
    
    # this variable is undefined
    
    }
    

提交回复
热议问题