in reply to Re: return if defined
in thread return if defined
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: return if defined
by uncoolbob (Novice) on Dec 20, 2012 at 12:43 UTC | |
|
Re^3: return if defined
by Anonymous Monk on Dec 20, 2012 at 13:00 UTC | |
by Anonymous Monk on Dec 20, 2012 at 13:39 UTC | |
by Anonymous Monk on Dec 20, 2012 at 13:55 UTC | |
by tobyink (Canon) on Dec 20, 2012 at 14:14 UTC |