Bug 18539 - Forbid Koha::Objects->find calls in list context
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@biblibre.com> BibLibre
Find is supposed for retrieving one result not multiple ones. Use search instead. Using find in a list context is not correct. ________________________________ Van: koha-devel-bounces@lists.koha-community.org <koha-devel-bounces@lists.koha-community.org> namens Julian Maurice <julian.maurice@biblibre.com> Verzonden: woensdag 13 december 2017 14:34:07 Aan: koha-devel@lists.koha-community.org Onderwerp: [Koha-devel] Bug 18539 - Forbid Koha::Objects->find calls in list context 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@biblibre.com> BibLibre _______________________________________________ Koha-devel mailing list Koha-devel@lists.koha-community.org http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel website : http://www.koha-community.org/ git : http://git.koha-community.org/ bugs : http://bugs.koha-community.org/
I know that find is supposed to retrieve only one result. That's why we shouldn't have to care about the context, it's never ambiguous. I should be allowed to write push @patrons, Koha::Patrons->find($borrowernumber); without specifying that I only want one record (using 'scalar') Le 13/12/2017 à 14:50, Marcel de Rooy a écrit :
Find is supposed for retrieving one result not multiple ones. Use search instead.
Using find in a list context is not correct.
------------------------------------------------------------------------ *Van:* koha-devel-bounces@lists.koha-community.org <koha-devel-bounces@lists.koha-community.org> namens Julian Maurice <julian.maurice@biblibre.com> *Verzonden:* woensdag 13 december 2017 14:34:07 *Aan:* koha-devel@lists.koha-community.org *Onderwerp:* [Koha-devel] Bug 18539 - Forbid Koha::Objects->find calls in list context 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@biblibre.com> BibLibre _______________________________________________ Koha-devel mailing list Koha-devel@lists.koha-community.org http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel website : http://www.koha-community.org/ git : http://git.koha-community.org/ bugs : http://bugs.koha-community.org/
-- Julian Maurice <julian.maurice@biblibre.com> BibLibre
Hi Julian,
@a = Koha::Patrons->find('foo'); # would result in @a = (undef)
And that leads to issues: if ( @a ) { # = 1 say $a[0]->borrowernumber; # BOOM } See also https://perlmaven.com/how-to-return-undef-from-a-function (was it the link you were talking about?) Cheers, Jonathan On Wed, 13 Dec 2017 at 10:34 Julian Maurice <julian.maurice@biblibre.com> wrote:
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@biblibre.com> BibLibre _______________________________________________ Koha-devel mailing list Koha-devel@lists.koha-community.org http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel website : http://www.koha-community.org/ git : http://git.koha-community.org/ bugs : http://bugs.koha-community.org/
Ok, but the proposed fix doesn't fix this code: @a = scalar Koha::Patrons->find('foo'); if (@a) { # still equals to 1 say $a[0]->borrowernumber; # still BOOM } And yes, that was the link ;) Le 13/12/2017 à 14:58, Jonathan Druart a écrit :
Hi Julian,
@a = Koha::Patrons->find('foo'); # would result in @a = (undef)
And that leads to issues: if ( @a ) { # = 1 say $a[0]->borrowernumber; # BOOM }
See also https://perlmaven.com/how-to-return-undef-from-a-function (was it the link you were talking about?)
Cheers, Jonathan
On Wed, 13 Dec 2017 at 10:34 Julian Maurice <julian.maurice@biblibre.com <mailto:julian.maurice@biblibre.com>> wrote:
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@biblibre.com <mailto:julian.maurice@biblibre.com>> BibLibre _______________________________________________ Koha-devel mailing list Koha-devel@lists.koha-community.org <mailto:Koha-devel@lists.koha-community.org> http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel website : http://www.koha-community.org/ git : http://git.koha-community.org/ bugs : http://bugs.koha-community.org/
-- Julian Maurice <julian.maurice@biblibre.com> BibLibre
participants (3)
-
Jonathan Druart -
Julian Maurice -
Marcel de Rooy