How do I check if a Perl scalar variable has been initialized?

后端 未结 5 1755
别跟我提以往
别跟我提以往 2020-12-13 12:59

Is the following the best way to check if a scalar variable is initialized in Perl, using defined?

my $var;

if (cond) {
    $var = \"string1\";         


        
5条回答
  •  孤街浪徒
    2020-12-13 13:36

    If you don't care whether or not it's empty, it is. Otherwise you can check

    if ( length( $str || '' )) {}
    

提交回复
热议问题