octopusgrabbus
octopusgrabbus

Reputation: 10685

When to use defined

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($acct_no));

the way to test for undefined parameters?

Second, after assigning to a hashref

$ptMtrRecRef = $ptSelHdl->fetchrow_hashref;

is the best way to test for $ptMtrRecRef being defined to use

if(!$ptMtrRecRef)

or

if(!defined($ptMtrRecRef))?

###############################################################################
#  Returns count of meters per account number.

# $PkNam  -- package name discarded
# $DBHdl -- ICS database handle
# $acct_no -- water account number

sub mgbl_get_meter_count
{
    my ($PkNam, $DBHdl, $acct_no) = @_;

    die("mgbl_get_meter_count passed undef handles.\n")
        if(!defined($DBHdl) || !defined($acct_no));

    my $ptSelHdl;
    my $ptMtrRecRef;

    my $sql_statement = 
     "select count(*) from meter m where m.acct_no = ".$acct_no.";";

    $ptSelHdl = $DBHdl->prepare($sql_statement);

    die("Cannot prepare select count(*) from meter m\n")
      if(!$ptSelHdl || !$ptSelHdl->execute);

    $ptMtrRecRef = $ptSelHdl->fetchrow_hashref;

    return $ptMtrRecRef;
}

Upvotes: 3

Views: 175

Answers (3)

wuchang
wuchang

Reputation: 3069

Well , in perl script language, defined($a) is just a sub routine to test if $a is "undef",nothing else. So ,you will ask ,what is undef?

To be accurate, it is a perl subroutine ,the same as defined.But when it has no parameter, it can be considered as a perl-special scalar . For example , when you pop a value from an empty array ,it will return an undef.When you call subroutine "undef $b",then $b will become undef($b must be an left value),nothing else. Only in this case, defined($b) will return false.But if $c is an empty string like "" ,number zero ,or string "0" ,defined($c) will still return true;

But if you use a simple boolean expression instead of defined,it becomes totally different. A simple Boolean test will not distinguish among undef, zero, the empty string, and "0" .So , it absolutely depends on your pratical requirement when determining using defined() or just a boolean test.

Upvotes: 1

ikegami
ikegami

Reputation: 385779

$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.

Upvotes: 9

Rory Hunter
Rory Hunter

Reputation: 3460

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

Upvotes: 5

Related Questions