siddheshsawant has asked for the wisdom of the Perl Monks concerning the following question:

Hello friends .....I am stuck at one question in perl....I have wrote a subroutine which is called by someother subroutine.....The task of this subroutine is to insert value in database.The sub routine is as follows:

sub _get_user_scenario { my ($self, $schema, $user_scenario) = @_; my $user_scenario_rs = undef; eval { #get the number of user scenarios in the database table my $count = $schema->resultset("UserScenario")->count(); #insert user scenario, or if already in database, update the descripti +on $user_scenario_rs = $schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, description => $user_scenario->{description}, }, { key => "u_user_scenario_name" }, ) || die "Unable to update/insert data to table UserScenar +io: $!\n"; }; if($@) { #warn(); $self->log_writer()->write_error_row("Unable to get/insert dat +a to table UserScenario: $@"); return undef; } else { return $user_scenario_rs; } }

If $user_scenario->{description} is empty then I don't want to replace the description for the existing sceanrio.And I wanna keep the value which is already there in the database for that scenario.How to achieve it?Any suggestions? Thanks in advance!!!!

Replies are listed 'Best First'.
Re: how to get out of subroutine without returning anything
by ikegami (Patriarch) on Mar 30, 2010 at 15:45 UTC
    I think I got it. You want to replace
    $schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, description => $user_scenario->{description}, }, { key => "u_user_scenario_name" }, );
    with
    $schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, }, { 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".

    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" }, );

      A bit more compact and nearly readable -

      $schema->resultset("UserScenario")->update_or_create( { name => $user_scenario->{name}, ( description => $user_scenario->{description} ) x!! $user_scenari +o->{description}, }, { key => "u_user_scenario_name" }, );

      - if it wasn't for the "Highlander's List Asserter" (x!!) "secret operator", which Aristotle presented as the "boolean list squash operator" ;-)

Re: how to get out of subroutine without returning anything
by ikegami (Patriarch) on Mar 30, 2010 at 15:33 UTC

    I don't understand the question in the post, so I'll answer the question in the title in the hopes that they are the same.

    how to get out of subroutine without returning anything

    return; and return (); return an empty list (void and list context) or undef (scalar context). That's as little as you can return short of throwing an exception.

Re: how to get out of subroutine without returning anything
by jacaril (Beadle) on Mar 30, 2010 at 16:57 UTC

    Do you want o do anything with the data if the description is empty? If not the fast approach could be something like:

    sub _get_user_scenario { my ($self, $schema, $user_scenario) = @_; return undef if $user_scenario->{description} eq ""; ...