which is much easier to use and looks cleaner.
All you have to do is create your Prefs class with an AUTOLOAD function, as below:
package App::Prefs;
use base 'App';
use strict;
__PACKAGE__->table("preferences");
__PACKAGE__->columns( ALL => qw/ id name value/ );
sub AUTOLOAD {
my $self = shift;
my $newval = shift;
my $attr = our $AUTOLOAD;
$attr =~ s/.*:://;
return if($attr eq "DESTROY");
my $row = $self->search({ name => "$attr" })->first();
if(defined($newval)) {
if(!$row) {
$row=$self->insert({ name => "$attr" });
}
$row->value($newval);
$row->update();
}
return defined($row) ? $row->value : undef;
}
So calling an accessor with no matching "name" in the preferences table will return "undef", calling an accessor with a parameter will insert that name/value pair into the table, and thereafter calling the accessor will return the correct value for the name matching the accessor