DBD::mysql Integer Type Coercion
DBD::mysql
knows that certain MySQL column types are integers with this call:
$sth->execute;
$nums = $sth->{'mysql_is_num'} || [];
You can iterate over $nums
and see which columns are numeric-type columns. However, when you pull the data out, it’s converted to a string. So, what used to be as simple as this:
my $accounts = $sth->fetchall_arrayref({});
now requires this kind of trickery:
my @accounts = ();
my $is_num = $sth->{'mysql_is_num'} || [];
while (my $row = $sth->fetchrow_arrayref) {
push @accounts, {
map {
$Acct_Fields[$_] => ## key
($is_num->[$_] ## check to see if it's int
? 0 + $row->[$_] ## coerce to integer/numeric
: $row->[$_]) ## leave it alone
} (0 .. $#Acct_Fields) ## number of keys
};
}
This seems to work as a primary test to see if a scalar ($val
) is an number-ish thing:
my $spv_flags = B::svref_2object(\$val)->FLAGS & (B::SVp_IOK | B::SVp_NOK);
I borrowed that from Mojo::JSON
’s serializer (it also has a 0+ and 1* test for good measure).
Last modified on 2014-09-10