In Perl, how can I concisely check if a $variable is defined and contains a non zero length string?

前端 未结 9 985
名媛妹妹
名媛妹妹 2020-12-02 07:08

I currently use the following Perl to check if a variable is defined and contains text. I have to check defined first to avoid an \'uninitialized value\' warnin

9条回答
  •  孤街浪徒
    2020-12-02 07:46

    In cases where I don't care whether the variable is undef or equal to '', I usually summarize it as:

    $name = "" unless defined $name;
    if($name ne '') {
      # do something with $name
    }
    

提交回复
热议问题