<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p style="margin-top: 0px; margin-bottom: 0px;">Find is supposed for retrieving one result not multiple ones. Use search instead.</p>
<p style="margin-top: 0px; margin-bottom: 0px;">Using find in a list context is not correct.<br>
</p>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>Van:</b> koha-devel-bounces@lists.koha-community.org <koha-devel-bounces@lists.koha-community.org> namens Julian Maurice <julian.maurice@biblibre.com><br>
<b>Verzonden:</b> woensdag 13 december 2017 14:34:07<br>
<b>Aan:</b> koha-devel@lists.koha-community.org<br>
<b>Onderwerp:</b> [Koha-devel] Bug 18539 - Forbid Koha::Objects->find calls in list context</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:10pt;">
<div class="PlainText">Hi developers,<br>
<br>
I stumbled upon a line of code recently and I can't figure out why it<br>
has be done this way. I hope you can help me :)<br>
<br>
The line in question is in Koha::Objects::find:<br>
<br>
    croak 'Cannot use "->find" in list context' if wantarray;<br>
<br>
I read the two bugs (18539 and 18179) and the link given by Jonathan but<br>
I still don't understand why the call in list context has been<br>
forbidden. Why not simply return undef (an explicit undef) when no<br>
records have be found ? It would work as expected in scalar and list<br>
contexts.<br>
<br>
Here is a possible rewrite of 'find' to better explain what I mean:<br>
<br>
    sub find {<br>
        my ( $self, @pars ) = @_;<br>
<br>
        my $object = undef;<br>
<br>
        @pars = grep { defined } @pars;<br>
        if (@pars) {<br>
            my $result = $self->_resultset()->find(@pars);<br>
            if ($result) {<br>
                $object = $self->object_class()->_new_from_dbic($result);<br>
            }<br>
        }<br>
<br>
        return $object;<br>
    }<br>
<br>
@a = Koha::Patrons->find('foo'); # would result in @a = (undef)<br>
{a => K::P->find('foo'), b => 'bar'}; # would result in {a => undef, b<br>
=> 'bar'}<br>
<br>
Please tell me what you think.<br>
<br>
-- <br>
Julian Maurice <julian.maurice@biblibre.com><br>
BibLibre<br>
_______________________________________________<br>
Koha-devel mailing list<br>
Koha-devel@lists.koha-community.org<br>
<a href="http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel">http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel</a><br>
website : <a href="http://www.koha-community.org/">http://www.koha-community.org/</a><br>
git : <a href="http://git.koha-community.org/">http://git.koha-community.org/</a><br>
bugs : <a href="http://bugs.koha-community.org/">http://bugs.koha-community.org/</a><br>
</div>
</span></font></div>
</body>
</html>