Date: | Wed, 12 May 2004 22:19:15 +0200 |
From: | Branislav Zahradnik <brano [...] blueorange.sk> |
To: | cdbi-talk [...] groups.kasei.com |
Subject: | find_or_create enhancement |
hi,
there is small enhancement of find_or_create method.
it allows to specify some "default" values for created object.
Class->find_or_create
(name => 'Name', surnname => 'Surname', {default => 1});
sub find_or_create {
my $class = shift;
my $default = ref $_[$#_] eq "HASH" && $#_ > 0 ? pop : {};
my $hash = ref $_[0] eq "HASH" ? shift: {@_};
my ($exists) = $class->search($hash);
return defined($exists) ? $exists : $class->create({%$hash,
%$default});
}