On 2011-01-13 08:18, John W. Krahn wrote:
If you want to return a false value it is usually better to use return
with no value:
return;
I prefer subs to normally return a single scalar, which can be a direct
value like undef, or a reference to a more complex data structure, etc.
If you allow subs that do C<return;>, then you need to be careful if
elsewhere in the code there are things like:
my $h = { a => foo(), b => bar() };
because if both foo() and bar() would just return, this ends up in meaning:
my $h = { a => b };
and that is clearly not what was meant by the coder.
To prevent this, you can change the code to
my $h = { a => scalar foo(), b => scalar bar() };
but that feels like fixing it at the wrong end to me.
Yeah, maybe the '=>' should enforce scalar context, though that would
just lead to other surprises.
Simple rule: a sub that in normal circumstances returns a scalar, should
not do C<return;> but do C<return undef;> in the special case.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/