Show me the code!
Here's a sample session to show how easily you can access existing data in an object-oriented fashion. In it, we have a simple configuration which names the table in our database and the primary key field. We process the config, then fetch a group of objects based on criteria passed in from the command-line.
Just set the variables starting the file to your relevant information.
#!/usr/bin/perl
use strict;
use SPOPS::Initialize;
my $TABLE = 'mytable';
my $ID = 'id';
my $DSN = 'DBI:mysql:test';
my $USER = 'test';
my $PASS = 'test';
{
my ( $search_field, $search_value ) = @ARGV;
unless ( defined $search_field and defined $search_value ) {
die "Usage $0 search-field search-value\n";
}
my $config = {
generic => {
class => 'My::Object',
isa => [ 'SPOPS::DBI' ],
rules_from => [ 'SPOPS::Tool::DBI::Datasource',
'SPOPS::Tool::DBI::DiscoverField' ],
field_discover => 'yes',
id_field => $ID,
base_table => $TABLE,
dbi_config => { dsn => $DSN,
username => $USER,
password => $PASS },
} };
SPOPS::Initialize->process({ config => $config });
my $iter = My::Object->fetch_iterator({ where => "$search_field = ?",
value => [ $search_value ] });
while ( my $o = $iter->get_next ) {
print "Object: (", $o->id, ")", "\n",
join( "\n",
map { "$_: $o->{ $_ }" }
@{ My::Object->field_list } ), "\n\n";
}
}