with$schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, description => $user_scenario->{description}, }, { key => "u_user_scenario_name" }, );
when $user_scenario->{description} is "empty". First, what do you mean by "empty"? Scalars can't be empty. Empty string? Undef? Or since the scalar question in a hash element, non-existent? I'm going to assume "empty" means "undefined or non-existent".$schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, }, { key => "u_user_scenario_name" }, );
The straightforward approach:
if (defined($user_scenario->{description}) { $schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, description => $user_scenario->{description}, }, { key => "u_user_scenario_name" }, ); } else { $schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, }, { key => "u_user_scenario_name" }, ); }
Something more compact:
$schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, ( defined($user_scenario->{description}) ? ( description => $user_scenario->{description} ) : () ), }, { key => "u_user_scenario_name" }, );
That's not very readable. How about
my %fields; $fields{name} = $user_scenario->{name}; $fields{description} = $user_scenario->{description} if defined($fields{description}); $schema->resultset("UserScenario")->update_or_create( \%fields, { key => "u_user_scenario_name" }, );
In reply to Re: how to get out of subroutine without returning anything
by ikegami
in thread how to get out of subroutine without returning anything
by siddheshsawant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |