in reply to Saving array reference

DBI::st appears to re-use internally an arrayref (I just got bitten by this today). You will have to store the record yourself:
my $first = undef; while (my $record = $sth->fetchrow_arrayref) { unless (defined $first) { @{$first} = @{$record} } # other stuff }
..although I'm unclear as to why you can't just stop retrieving records after the first, or why you can't just use a 'limit 1' clause in your SQL.

Also my DBI doesn't define a fetch method on statement handles, only fetchrow_* and fetchall_*, so you may need to re-jig my example.


Update: jZed++ for pointing out fetch is an alias for fetchrow_arrayref. Thanks, I've managed to miss that in every reading of perldoc DBI.

Replies are listed 'Best First'.
Re^2: Saving array reference
by jZed (Prior) on Apr 30, 2005 at 20:28 UTC
    fetch() is a synonym for fetchrow_arrayref().
Re^2: Saving array reference
by Errto (Vicar) on May 01, 2005 at 01:34 UTC
    Good suggestions. Just two minor points: first, in fairness to the DBI developers, the fact that fetchrow_arrayref reuses the same reference isn't just "internal", but is actually documented in the DBI docs:
    Note that the same array reference is returned for each fetch, so don’t store the reference and then use it after a later fetch.
    Second, the 'limit n' syntax isn't used across all databases, though for anyone on MySQL that's certainly a good suggestion. But Oracle, for example, doesn't have it.
Re^2: Saving array reference
by mhearse (Chaplain) on Apr 30, 2005 at 20:18 UTC
    Thanks, works great. You right though, I guess I could just do another query (strictly for the first match). Just wasn't thinking that way at the time. My thought was to save the first match for later use.