[Dbix-class] an extra undef result is coming while fetching result

John Romkey romkey at romkey.com
Wed Feb 15 14:33:09 GMT 2012


On Feb 15, 2012, at 9:16 AM, tom wrote:
> I have a query as follows and it is giving an extra 'undef' result while fetching the results. 
> 
>         my $item_cost_details_rs = $self->item_db()->resultset('ItemDetails'); 
>         my @min_cost_items = $item_cost_details_rs->search( 
>             {}, 
>                {
>              
>                 join        => 'item_mappings', 
>                 select      => [ 
>                     { min => 'item_mappings.item_mapping_cost' }, 
>                     'item_mappings.item' 
>                 ], 
>                 as          => [ 
>                     'cost', 
>                     'item' 
>                 ], 
>                 group_by    => [ 
>                     'item_mappings.item' 
>                 ] 
>             } 
>         ); 
>         foreach my $row (@min_cost_items){ 
>             my %details_item = $row->get_columns(); 
>             use Data::Dumper::Simple; 
>             print STDERR Dumper(%details_item); 
>         } 
> 
> 
> There are only 2 results to display. But in foreach loop it s getting an undef result in first looping. 2nd and 3rd looping give me the actual result. 
> 
> %details_item = ( 
>                   'item' => undef, 
>                   'cost' => undef 
>                 );

Offhand I don't know why you're seeing the problem you're seeing but I'd suggest rewriting your loop using the next iterator rather than returning an array of all results:

my $min_cost_items_rs = $item_cost_details_rs->search( ... );

while( my $row = $item_cost_items_rs->next ) {
	...
}

In general this is a more efficient construct - the amount of memory it uses should be independent of the size of the resultset. I suspect it will coincidentally solve your problem.
	- john romkey
	http://romkey.com/




More information about the DBIx-Class mailing list