in reply to Re^2: undef==defined sometimes? (body question)
in thread undef==defined sometimes?

Why don't you just return nothing at all on error, and get over the non-problem problem?

There was really no point in writing code here in hindsight. So ...

Added code for the uninitiated ...

use Data::Dumper; my @p = give(); my $p = give(); print Dumper( \@p , $p ); sub give { # Case of error. return; }

Replies are listed 'Best First'.
Re^4: undef==defined sometimes? (body question)
by perl-diddler (Chaplain) on May 03, 2008 at 11:14 UTC
    Well -- I could, but then the calling function would get garbage if I used it in an assignment.

    The function was *supposed* to return a list and only returned undef as an error condition (which wasn't the same as returning an empty list). The undef was reserved for 'illegal operations', like an attempt to compare two strings where one string was undefined. Otherwise, if nothing was found, but no error occurred, it returned a list of size 0 ("()").

    If I didn't explicitly return a value, wouldn't the results of whatever the last calculation be returned, essentially returning "left-overs" by accident? I'd rather return 'undef' and have consistently wrong behavior rather than random behavior -- at least if it is consistently wrong, I'm more likely to notice a problem. :-)

    -l

      If I didn't explicitly return a value, wouldn't the results of whatever the last calculation be returned

      return; return an empty list, just like return ();. If the function result is treated as a scalar, it will see undef.

      If you omit the return entirely, then you get the result of the last expression evaluated.