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

Hello Monks

I am working on Perl Database project and I am stuck at one problem. In my project I am using DBIx::Class to perform the database operations.But for the following function my script is getting aborted.The function causing problem is as follows:

sub _get_user_scenario { my ($self, $schema, $user_scenario) = @_; my $user_scenario_rs = undef; my $user_scenario_desc; # In the case where the description is not Null if($user_scenario->{description}){ $user_scenario_desc= $user_scenario->{description}; }else{ Line #799 ===> $user_scenario_desc = $schema->resultset("UserS +cenario")->search({ "name" =>$user_scenario->{name}, })->first->description; } 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 d +escription $user_scenario_rs = $schema->resultset("UserScenario")->update_o +r_create( { name => $user_scenario->{name}, description => $user_scenario_desc, }, { key => "u_user_scenario_name" }, ) || die "Unable to update/insert data to table UserScena +rio: $!\n"; }; if($@){ #warn(); $self->log_writer()->write_error_row("Unable to get/insert da +ta to table UserScenario: $@"); return undef; }else{ $self->log_writer()->write_row("Inserted values into user_sce +nario table"); print"Inserted values into user_scenario table\n"; return $user_scenario_rs; } }

Here I am trying to insert some values into user_scenario table.Among those values I am trying to insert the value for description if it is already defined in the hash else I am trying to extract it's value using search function. But this case is not working for the user_scenarios whose description is not stored in the hash as well as not present in the database.I am getting an error like this for line # 799 is as follows(in the code line # is specified)

Can't call method "description" on an undefined value at DataLoader/Inserter.pm line 799.

Kindly help me out to deal with this DBIx error. Thanks in advance !!!!

Replies are listed 'Best First'.
Re: A doubt about DBIx::Class
by Your Mother (Archbishop) on Jun 23, 2010 at 21:20 UTC

    It seems like you already know exactly what's going on(?). When-

    $schema->resultset("UserScenario")->search({ "name" =>$user_scenario->{name}, })->first

    -finds no UserScenario record, you get an error trying to call the method "description" on nothing. Bit like trying to do undef->do_something.

OT: doubt != question
by Your Mother (Archbishop) on Jun 23, 2010 at 21:13 UTC

    Off topic: "doubt" is not an acceptable synonym for "question" outside of the Subcontinent. Please do not use it and let your local peers know it is confusing, at best, for native English speakers.

Re: A doubt about DBIx::Class
by choroba (Cardinal) on Jun 23, 2010 at 20:54 UTC
    {} missing around description?
      No it works if the descriptiono is present in the Database .... But if it is not then it gives error like cant call method description on the Null Value