Perl DBI insert of hash How can this method that inserts a hashref into a database table be improved?  It assumes that the columns of the database table are named identically to the fields in the hashref.  The return value is the id field created by the insert.

sub insert(){
    my ($table, $data) = @_;
    my ($fields, $placeholders, $values) = ("", "", []);
    while (my ($field, $value) = each %$data){
        $fields.=", " if ($fields);
        $fields.=$field;
        $placeholders.=", " if ($placeholders);
        $placeholders.="?";
        push(@$values, $value);
    }
    $dbh-&gt;prepare("INSERT INTO $table ($fields) VALUES ($placeholders)")-&gt;execute(@$values);
    return $dbh-&gt;last_insert_id(undef, undef, undef, undef);
}


It assumes that $dbh is a correctly initialized global database handle.   

It is used like:

&amp;insert("animals", {
    "type"=&gt;"cat",
    "name"=&gt;"Patches",
    "date-of-birth"=&gt;"November 1, 2015",
    "date-of-death"=&gt;undef
});


Which will insert a record into the animals table:

id | type | name    | date-of-birth    | date-of-death
=====================================================
 1 | cat  | Patches | November 1, 2015 | null
This might be a good candidate to use SQL::Abstract in, as per DBIx::Class.



sub insert {
    # insert takes ( $table, \%fieldmap )
    my $sql = SQL::Abstract-&gt;new();
    my ( $query, @binds ) = $sql-&gt;insert( @_ );
    $dbh-&gt;prepare($query)-&gt;execute(@binds);
    return $dbh-&gt;last_insert_id(undef, undef, undef, undef);
}


This gives you a few easy features that may come in handy in the future
if you change your database, like:


easily change quoting character for tables/fields
guard against injection abuse in user-specified key names, because your code currently assumes all keys will be safe. But this will not necessarily be true, and is a vulnerability target even under taint