Skip to content
yuki-kimoto edited this page Oct 1, 2012 · 1 revision

Fetch row

DBIx::Custom Documents >

Return value of execute method or select method is DBIx::Custom::Result object. DBIx::Custom::Result have many ways to fetch rows.

fetch - each row as array

Fetch a row and put it into array reference.

my $row = $result->fetch;

You get the following data.

['Perl', 'Ken']

Generally, fetch method is used with while loop. If there is no row to fetch, fetch method return undef.

while (my $row = $result->fetch) {
  my $title  = $row->[0];
  my $author = $row->[1];
}

fetch_one - only one row as array

Fetch only one row and put it into array reference. Automatically, Statement handle is finished by finish method of DBI's statement handle after one row is fetched.

my $row = $result->fetch_one;

You get the following data.

['Perl', 'Ken']

fetch_all - fetch all rows

Fetch all rows and put it into array of array reference.

my $rows = $result->fetch_all;

You can get the following data.

[
  ['Perl', 'Ken'],
  ['Ruby', 'Mike']
]

fetch_hash - fetch each row as hash

Fetch a row and put it into hash refernece.

my $row = $result->fetch_hash;

You get the following data.

{title => 'Perl', author => 'Ken'}

Generally, fetch_hash method is used with while loop. If there is no row to fetch, fetch_hash method return undef.

while (my $row = $result->fetch_hash) {
  my $title  = $row->{title};
  my $author = $row->{author};
}

fetch_hash_one or one - fetch only one row

Fetch only one row and put it into hash reference. Automatically, Statement handle is finished by finish method of DBI's statement handle after one row is fetched.

my $row = $result->fetch_hash_one;

You get the following data.

{title => 'Perl', author => 'Ken'}

one is alias for fetch_hash_one to write it shorter.

my $row = $result->one;

fetch_hash_all or all - fetch all rows as hash

Fetch all rows and put it into array of hash reference.

my $rows = $result->fetch_hash_all;

You get the following data.

[
  {title => 'Perl', author => 'Ken'},
  {title => 'Ruby', author => 'Mike'}
]

all is alias for fetch_hash_all to write it shorter.

my $rows = $result->all;
Clone this wiki locally