http://qs321.pair.com?node_id=545030

Generating dynamic SQL statements and need to plug in bind values? Maintain a single list of columns and let this function handle stringifications and preping your bind valuees array.
sub setup_bindings { # Produces strings useful for generating dynamic # SQL statements as well as an array of matching # binding values (because you use bind values, right?) #Args: # $_[0]: Ref to hash of values, keyed by column # $_[1]: Ref to array of columns used #Usage: # my ( $column_string, $bindings_string, $bound_values ) # = setup_bindings( \%value_for_column, \@columns ); # my $statement = " INSERT into YOUR_TABLE ( $column_string ) # VALUES ( $bindings_string )"; # $dbhandle->do( $statement, undef, @$bound_values ); #Or whatever +use you have- this is `do` from the DBI my $values = shift; my @columns = @{ +shift }; my @set_columns = (); my @set_bindings = (); my @bound_values = (); for my $col_2_bind ( @columns ) { push @set_columns, $col_2_bind; push @set_bindings, '?'; push @bound_values, $values->{$col_2_bind}; }; my $column_string = join ', ', @set_columns; my $bindings_string = join ', ', @set_bindings; return ($column_string, $bindings_string, \@bound_values); }