uninit value puzzler

Discussion in 'Perl Misc' started by Walter Roberson, Jun 21, 2004.

  1. 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?


    --
    I predict that you will not trust this prediction.
     
    Walter Roberson, Jun 21, 2004
    #1
    1. Advertising

  2. Walter Roberson

    Bob Walton Guest

    Walter Roberson wrote:

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


    > 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?
    >


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jun 22, 2004
    #2
    1. Advertising

  3. In article <>,
    Bob Walton <> wrote:
    :Walter Roberson wrote:
    :> $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.
    --
    Oh, yeah, an African swallow maybe, but not a European swallow.
    That's my point.
     
    Walter Roberson, Jun 22, 2004
    #3
  4. Walter Roberson

    Ben Morrow Guest

    Quoth d:
    > Walter Roberson wrote:
    >
    > > $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}));
    >
    > 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

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces molit animos, tristesque mentes erigit. |
    Musica vel ipsas arbores et horridas movet feras. |
     
    Ben Morrow, Jun 22, 2004
    #4
  5. Walter Roberson

    Anno Siegel Guest

    Walter Roberson <-cnrc.gc.ca> wrote in comp.lang.perl.misc:
    > In article <>,
    > Bob Walton <> wrote:
    > :Walter Roberson wrote:
    > :> $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
     
    Anno Siegel, Jun 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Corey

    dropdown puzzler

    Jim Corey, Jan 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    Jon Booth
    Jan 16, 2004
  2. Richard

    odd image puzzler

    Richard, Jan 4, 2004, in forum: HTML
    Replies:
    10
    Views:
    770
    Sid Ismail
    Jan 4, 2004
  3. Anon

    xsd puzzler

    Anon, Oct 21, 2003, in forum: XML
    Replies:
    3
    Views:
    422
  4. Tim Daneliuk

    Tkinter Puzzler

    Tim Daneliuk, Jan 7, 2005, in forum: Python
    Replies:
    4
    Views:
    325
    Tim Daneliuk
    Jan 7, 2005
  5. Paul Rubin

    tkinter puzzler

    Paul Rubin, May 12, 2005, in forum: Python
    Replies:
    5
    Views:
    409
    Russell E. Owen
    May 16, 2005
Loading...

Share This Page