[Dbix-class] Problem getting data from resultset

Patrick Meidl patrick at pantheon.at
Fri Jul 6 14:22:31 GMT 2012


On Fri, Jul 06 2012, Kenneth S Mclane <ksmclane at us.ibm.com> wrote:

> Ok, I now have this code:
> 
> my @fields = qw/ department.department_code account_code account_name 
> account_policy compliance.percent_compliant metrics.num_servers 
> metrics.num_subsystems progress.percent_complete /;
>         foreach my $field (@fields) {
>                 $ws->write($row, $col, $field);
> 
>                 $col++;
>         }
>         $col=0;
>         $row++;
>         while (my @data =$sr->next) {
>                 foreach my $data (@data) {
>                         foreach my $field (@fields) {
>                                 if ($field =~ /\./) {
>                                         $field = join( "->", split( /\./, 
> $field));
>                                 }
>                                 $ws->write($row, $col, $data->$field);
> 
>                                 $col++;
>                         }
>                         $col=0;
>                 } 
>                 $row++;
>         }
> Which gives this error:
> Caught exception in dbms::Controller::AccountView->list "Can't locate 
> object method "department->department_code" via package 
> "dbms::Model::ORANGES::Account" at 
> /opt/catalyst/dbms/script/../lib/dbms/Controller/AccountView.pm line 65."

perl doesn't eval $data->$field in the way you expect it to. you would
have to do something like

my $val = $data;
foreach my $method (split(/\./, $field)) {
    $val = $val->$method;
}
$ws->write($row, $col, $val);

to make your code more robust, you should add some sanity checking, like

if ($val->can($method)) { ... }

HTH

    patrick

-- 
Patrick Meidl ........................ patrick at pantheon.at
Vienna, Austria ...................... http://gplus.to/pmeidl




More information about the DBIx-Class mailing list