in reply to Use of wantarray Considered Harmful
I'm undecided on the issue. Consider the code I've seen on PerlMonks this week:
$field = $sth->fetchrow_array();
The result of fetchrow_array in scalar context is undefined or at least undocumenteddocumented as undefined, at least some of the time. What should fetch_array do in this situation? Let the result be undefined (GIGO), or provide something sensible? The latter requires wantarray (or at least a commitment to using a compatible op).
Given a "$scalar = x()", I expect that x() returns a scalar. I know very well that's not necessarily true
It is necessarily true. x() cannot return anything but a scalar in scalar context.
In scalar context | |
---|---|
sub x { ...; @x } | @x evaluates to the num of elements in @x, so x() returns the num of elements in @x |
sub x { ...; ($x,$y) } | ($x,$y) evaluates to $y, so x() returns $y |
sub x { ...; () } | () evaluates to undef, so x() returns undef |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Use of wantarray Considered Harmful
by kyle (Abbot) on Dec 12, 2008 at 21:34 UTC | |
by ikegami (Patriarch) on Dec 12, 2008 at 22:11 UTC | |
by gwadej (Chaplain) on Dec 12, 2008 at 21:54 UTC | |
Re^2: Use of wantarray Considered Harmful
by ysth (Canon) on Dec 14, 2008 at 03:15 UTC | |
by ikegami (Patriarch) on Dec 14, 2008 at 03:30 UTC |