#!/usr/bin/perl use strict; use DBI; use SPOPS::Initialize; my ( $DB ); sub My::Sample::global_datasource_handle { unless ( $DB ) { $DB = DBI->connect( 'DBI:mysql:sample', 'me', 'pass' ) || die "Cannot connect: $DBI::errstr"; $DB->{RaiseError} = 1; } return $DB; } sub dump_objects { my ( $object_list ) = @_; foreach my $object ( @{ $object_list } ) { print <{a} b: $object->{b} c: $object->{c} d: $object->{d} DUMP } } { my %config = ( sample => { class => 'My::Sample', isa => [ 'SPOPS::DBI::MySQL', 'SPOPS::DBI' ], field => [ 'a', 'b', 'c', 'd' ], id_field => 'a', base_table => 'sampletable', }, ); SPOPS::Initialize->process({ config => \%config }); # Create a new object and save it: my $new_object = My::Sample->new({ a => 15, b => 'Smithson', c => 0.389, d => 'Arf!' }); $new_object->save; # Grab an existing object and modify a field my $saved_object = My::Sample->fetch( 15 ); $saved_object->{d} = 'Quack!'; $saved_object->save; # Grab all the objects my $all_objects = My::Sample->fetch_group({ order => 'a' }); dump_objects( $all_objects ); # Grab some of the objects my $some_objects = My::Sample->fetch_group({where=>'b LIKE ?', value=>['smith%']}); dump_objects( $some_objects ); }