uninit value puzzler

W

Walter Roberson

I am trying to figure out the logical error in the following code snippit.

Assume, please, that the code is 'use strict' and 'use warnings' clean.
Also assume, please, that $nhit_href is a valid hash reference, and that
it would be relatively common for $nhit_href->{$key} to not already
exist.


my $newcount = 1;
$newcount = $nhit_href->{$key} + 1
if exists $nhit_href->{$key} && defined $nhit_href->{$key};


The problem: from time to time (but not at all often), I get the message:

Use of uninitialized value in addition (+) at /usr/people/roberson/src/CacheUtils.pm line 242.

where line 242 is the second statement.


Have I perhaps missed something in operator precidences? Under what
circumstances might I be trying to increment an uninitialized
$nhit_href->{$key} considering that I -thought- the code is checking for
existance and definedness before attempting the increment?
 
B

Bob Walton

Walter said:
I am trying to figure out the logical error in the following code snippit.

Assume, please, that the code is 'use strict' and 'use warnings' clean.
Also assume, please, that $nhit_href is a valid hash reference, and that
it would be relatively common for $nhit_href->{$key} to not already
exist.


my $newcount = 1;
$newcount = $nhit_href->{$key} + 1
if exists $nhit_href->{$key} && defined $nhit_href->{$key};


The problem: from time to time (but not at all often), I get the message:

Use of uninitialized value in addition (+) at /usr/people/roberson/src/CacheUtils.pm line 242.

where line 242 is the second statement.
Have I perhaps missed something in operator precidences? Under what


If you have any doubt about precedence, use parens. Actually, you *do*
have a problem -- your if clause equivalent to:

... if exists($nhit_href->{$key} && defined($nhit_href->{$key}));

So use:

... if exists($nhit_href->{$key}) && defined($nhit_href->{$key});

to resolve the difficulty. Here is a quickie that shows what is going on:

sub a{
print "a got $_[0]\n";
}
sub b{
print "b got $_[0]\n";
}
$c=23;
$d=42;
print "hi " if a($c) && b($d);
print "\n**********\n";
print "xx " if a $c && b $d;
print "\n::::::::::\n";
__END__

You may note in the second case that sub b is called before sub a, and
that sub a's argument is 1, not the desired 23.
 
W

Walter Roberson

:> $newcount = $nhit_href->{$key} + 1
:> if exists $nhit_href->{$key} && defined $nhit_href->{$key};

:If you have any doubt about precedence, use parens. Actually, you *do*
:have a problem -- your if clause equivalent to:

: ... if exists($nhit_href->{$key} && defined($nhit_href->{$key}));

Thanks, Bob -- I was "too close to the code" to see the problem.
 
B

Ben Morrow

Quoth (e-mail address removed):
If you have any doubt about precedence, use parens. Actually, you *do*
have a problem -- your if clause equivalent to:

... if exists($nhit_href->{$key} && defined($nhit_href->{$key}));

So use:

... if exists($nhit_href->{$key}) && defined($nhit_href->{$key});

Or (better, IMHO)

.... if exists $nhit_href->{$key} and defined $nhit_href->{$key};

Ben
 
A

Anno Siegel

Walter Roberson said:
:> $newcount = $nhit_href->{$key} + 1
:> if exists $nhit_href->{$key} && defined $nhit_href->{$key};

:If you have any doubt about precedence, use parens. Actually, you *do*
:have a problem -- your if clause equivalent to:

: ... if exists($nhit_href->{$key} && defined($nhit_href->{$key}));

Thanks, Bob -- I was "too close to the code" to see the problem.

Unfortunately, this simple explanation is wrong.

Named unary operators, like "exists" and "defined", have higher
precedence than "&&". In particular

perl -MO=Deparse -e 'print if exists $x{a} && defined $x{a}'

prints "print $_ if exists $x{'a'} and defined $x{'a'};". So the
logic is correct and the mystery remains.

If %$nhit_href were a tied array that could explain everything. But
you'd know about that, wouldn't you?

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top