[Koha-devel] Bug 18539 - Forbid Koha::Objects->find calls in list context
Julian Maurice
julian.maurice at biblibre.com
Wed Dec 13 14:34:07 CET 2017
Hi developers,
I stumbled upon a line of code recently and I can't figure out why it
has be done this way. I hope you can help me :)
The line in question is in Koha::Objects::find:
croak 'Cannot use "->find" in list context' if wantarray;
I read the two bugs (18539 and 18179) and the link given by Jonathan but
I still don't understand why the call in list context has been
forbidden. Why not simply return undef (an explicit undef) when no
records have be found ? It would work as expected in scalar and list
contexts.
Here is a possible rewrite of 'find' to better explain what I mean:
sub find {
my ( $self, @pars ) = @_;
my $object = undef;
@pars = grep { defined } @pars;
if (@pars) {
my $result = $self->_resultset()->find(@pars);
if ($result) {
$object = $self->object_class()->_new_from_dbic($result);
}
}
return $object;
}
@a = Koha::Patrons->find('foo'); # would result in @a = (undef)
{a => K::P->find('foo'), b => 'bar'}; # would result in {a => undef, b
=> 'bar'}
Please tell me what you think.
--
Julian Maurice <julian.maurice at biblibre.com>
BibLibre
More information about the Koha-devel
mailing list