in reply to How to eliminate warning message on hash value?

$$keyptr is undef. (Well, it could be the result of $curuser being undefined, but that would also get you a warning earlier.) Assuming that's normal,
$$keyptr = $$db{CurrentKey} = $SavSubtree[$curuser][$$db{RSubtreeno}]; if ( defined($$keyptr) ) { my $datapos = $RSubtree{$curuser}{$$keyptr}; if ( defined($datapos) ) { $ret = GetDataRecord( $db, $keyptr, $dataptr, $datapos ); if ( $ret == TRUE ) { flock( $$db{btree}, LOCK_UN ); return ""; } } }

What's with == TRUE? Perl subs don't tend to define what true value they return on success.

$$keyptr = $$db{CurrentKey} = $SavSubtree[$curuser][$$db{RSubtreeno}]; if ( defined($$keyptr) ) { my $datapos = $RSubtree{$curuser}{$$keyptr}; if ( defined($datapos) ) { if ( GetDataRecord( $db, $keyptr, $dataptr, $datapos ) ) { flock( $$db{btree}, LOCK_UN ); return ""; } } }

Replies are listed 'Best First'.
Re^2: How to eliminate warning message on hash value?
by Marshall (Canon) on Dec 08, 2011 at 11:49 UTC
    As a minor note, I would get rid of the nested if and replace that with an "and".

    Update: fixed line wrap as per Ikegami's suggestion.
    line length settings are different in my normal editor than PM. When I do complex multi-part if's requiring more than one line, I do it like below with the conditions on top of each other and the connecting "or" or "and" to the left.

    if ( defined($$keyptr) ) { my $datapos = $RSubtree{$curuser}{$$keyptr}; if ( defined($datapos) #update and GetDataRecord( $db, $keyptr, $dataptr, $datapos ) ) { flock( $db->{btree}, LOCK_UN ); return ""; } }
    instead of:
    if ( defined($$keyptr) ) { my $datapos = $RSubtree{$curuser}{$$keyptr}; if ( defined($datapos) ) { if ( GetDataRecord( $db, $keyptr, $dataptr, $datapos ) ) { flock( $$db{btree}, LOCK_UN ); return ""; } } }

      As a minor note, I would get rid of the nested if and replace that with an "and".

      Your code gets wrapped (with default PM settings), and is thus less readable. The line break was intentional, at which point "if" works better.

        I updated the post. I didn't notice the line length issue before. Lines on PM are shorter than I normally use, but this is solved easily as shown. Very complicated conditions can be made readable in this "vertical" manner.

        Update: As a historical note, I learned this method from Daniel McCraken when I was working with FORTRAN IV some years ago.

Re^2: How to eliminate warning message on hash value?
by flexvault (Monsignor) on Dec 08, 2011 at 13:57 UTC

    If you use BerkeleyDB and you use c_get / db_put / etc., you get a '' on success and a 'Err message' on failure. So you would have to '!' the function call. Only for BerkeleyDB, I define my TRUE/FALSE in reverse. But as you pointed out, I should have done as you proposed and it would have been a true perl function call, only it still has to return to the application a '' for success.

    Thank you

    "Well done is better than well said." - Benjamin Franklin

      Please don't define TRUE to be something false!!! Call it SUCCESS or something. And if it does return '' as you say, then you'll get a warning for using == since '' is not a number. Use eq to do a string comparison.