in reply to How hashes present keys and values.
Long answer:my($l_tablename, %l_data) = @_; my(@fields, @values); push(@fields, $_) && push(@values, $1_data{$_}) for(keys %1_data); my($1_prepare); { local $" = ','; # eliminate join statements $1_prepare = qq( INSERT INTO $1_tablename (@fields) VALUES (@values) ); }
The last statement, in order to put all the remaining values from @list into %fields should read: %fields = @list;@list = ('arg1', ('field1' => 'value1', 'field2' => 'value2')); # above same as: @list = ('arg1', 'field1', 'value1', 'field2', 'value2'); $table = shift(@list); # 'arg1' %fields = shift(@list); # 'field1'
There's probably a better way to do what you're trying to do. Writing an insert subroutine for each table in the database that allows new records might be a better solution. If you are really ambitious, you can even write an OO wrapper for each table, with select, insert, update, and delete methods. Then you can write things like:
instead of &update('person', { pk => 4, name => 'John B.' });my $person = new MyDB::Person(4); $person->update(name => 'John B.');
...or whatever. Just a few thoughts for you to mull over. {g}
|
|---|