When to use defined

前端 未结 3 948
礼貌的吻别
礼貌的吻别 2021-01-14 19:11

I am a little confused as which way to test parameters. Here are two examples from source code posted below. First is this

if(!defined($DBHdl) || !defined($ac

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 19:27

    $sth->fetchrow_hashref will either return undef or a reference to a hash. As such

    if (defined($row))
    

    and

    if ($row)
    

    are equivalent here. (undef is false, and reference is always true.) I opt for the simpler alternative.

    Same idea for $dbh->prepare.

提交回复
热议问题