in reply to Re^2: What is the difference between |= and ||=?
in thread What is the difference between |= and ||=?

Assuming the empty string isn't a valid id, that will convert undef to zero.

Whether it makes sense or not can only be derived from context, but it seems to me that you might be silencing the symptom, not fixing the problem. Why is id undef in the first place? Why would it makes sense to set it to zero when it is?

Replies are listed 'Best First'.
Re^4: What is the difference between |= and ||=?
by Anonymous Monk on Apr 08, 2009 at 04:51 UTC

    It's undef because the hash reference is a value returned from an SQL query. If no rows are selected (for instance), then it's possible for the 'id' value to be undef. And when I perform some math operation (as noted by Marshall below), the warning is triggered. I could wrap the math operation code around a conditional to suppress the warning:

    if ($some_href->{id}) { if ($some_href->{id} > 0) { do_something(); } }
    But I'm not sure if that's better than using ||=.
      If no row was selected, then whose id is that? Sure smells fishy.

        The "id" was an example. But there are cases (especially when JOINS are involved) where some columns have NULL values or where rows are not yet inserted. In those instances, the particular value of the hash reference is undef.