in reply to Re: Re: How to say 'oops' in OOPs?
in thread How to say 'oops' in OOPs?

If the error is really unexpected, don't be afraid to croak or die. eval/die is comparable to try/throw in other languages. If the error is expected to occur, then undef is probably the best returned value.

Personally, I think "return undef;" is more clear to the reader than "return;" even though they have an identical effect. Explicitly returning undef tells the reader that hey, dammit, the caller is probably watching for a false/undef return.

Replies are listed 'Best First'.
Re^4: How to say 'oops' in OOPs?
by adrianh (Chancellor) on Apr 08, 2003 at 21:55 UTC
    Personally, I think "return undef;" is more clear to the reader than "return;" even though they have an identical effect.

    Actually, they're not identical ;-)

    A raw return will return an empty list in a list context and undef in scalar context. This can make writing code that needs to behave itself in either context easier. See return for more info.

Re^4: How to say 'oops' in OOPs? (blank return is subtle)
by Aristotle (Chancellor) on Apr 09, 2003 at 09:51 UTC
    As adrianh said, a blank return actually equates to
    return wantarray ? () : undef;
    or probably more correctly
    return !defined(wantarray) || wantarray ? () : undef;

    Makeshifts last the longest.