<div dir="ltr"><div><div><div>Hi Julian,</div></div></div><div dir="ltr"><div><div><div><br><br>> @a = Koha::Patrons->find('foo'); # would result in @a = (undef)<br></div></div></div></div><div dir="ltr"><div><div><div></div><br></div><div>And that leads to issues:<br></div>if ( @a ) { # = 1<br></div>  say $a[0]->borrowernumber; # BOOM<br><div>}</div><div><br></div><div>See also <a href="https://perlmaven.com/how-to-return-undef-from-a-function">https://perlmaven.com/how-to-return-undef-from-a-function</a> (was it the link you were talking about?)</div><div><br></div><div>Cheers,</div><div>Jonathan<br></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, 13 Dec 2017 at 10:34 Julian Maurice <<a href="mailto:julian.maurice@biblibre.com" target="_blank">julian.maurice@biblibre.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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 <<a href="mailto:julian.maurice@biblibre.com" target="_blank">julian.maurice@biblibre.com</a>><br>
BibLibre<br>
_______________________________________________<br>
Koha-devel mailing list<br>
<a href="mailto:Koha-devel@lists.koha-community.org" target="_blank">Koha-devel@lists.koha-community.org</a><br>
<a href="http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel" rel="noreferrer" target="_blank">http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel</a><br>
website : <a href="http://www.koha-community.org/" rel="noreferrer" target="_blank">http://www.koha-community.org/</a><br>
git : <a href="http://git.koha-community.org/" rel="noreferrer" target="_blank">http://git.koha-community.org/</a><br>
bugs : <a href="http://bugs.koha-community.org/" rel="noreferrer" target="_blank">http://bugs.koha-community.org/</a><br>
</blockquote></div></div>