If you are experiencing a "MySQL client ran out of memory' error, it may be because you are retrieving a very large table. By default, MySQL will try to buffer all of it into memory which will cause that error.

In order to force MySQL to process each row, one at a time, set the mysql_use_result value of the db handler to 1:

$dbh= DBI->connect("DBI:mysql:<database>", "root", "<password>");
$dbh->{'mysql_use_result'}=1;