Empty Hash Values

Matt Lawrence matt.lawrence at virgin.net
Tue Apr 14 18:16:23 BST 2009


Jasper wrote:
> Adrian and I had a tiny chat about this in work, and he decided that
> subs that return a scalar should return undef explicitly (if they are
> just returning), and those that return a list should return.
>
> At the time I thought it seemed very sensible, but I'm on the fence again.
>
> Difficult to retroactively enforce, at any rate.
>   
Most of the time just returning does what you expect, it's false in 
every context and undef in scalar context.

sub false { return undef }
if (my ($foo) = false()) {
    # You will get here, but may not expect to
    print "false() is true\n";
}

The trouble arises when you make calls in list context and expect to get 
a scalar, in which case it pays to be explicit.

Matt



More information about the london.pm mailing list