I am a fan of the defined-or operator, but in this case both of your solutions remove the short-circuiting. uncoolbob's original sub did not generate the new result if the existing result was already defined, and that good feature should be preserved.
How about something like this? Using one variable called $result simplifies the code and allows a single return statement (I am not dogmatic about having only one return statement, but I think it is nicer in a case like this).
sub my_find_or_create { my $result = $self->find_by_something(@args); unless (defined $result) { # create result here } return $result; }
In reply to Re^2: return if defined
by Anonymous Monk
in thread return if defined
by uncoolbob
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |