When to use defined

前端 未结 3 949
礼貌的吻别
礼貌的吻别 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:36

    In the case of the code you posted, I would also do as ikegami said, and use the shorter form.

    There are occasions when that isn't suitable, however, for example if a variable could have a legitimate value that would be treated as false if simply used in a true/false test. For example:

    my $value = 0;
    print "defined\n" if defined $value;  # prints 'defined'
    print "true\n" if $value;  # does not print anything
    

提交回复
热议问题