find_or_create enhancement

[prev] [thread] [next] [Date index for 2004/05/12]

From: Branislav Zahradnik
Subject: find_or_create enhancement
Date: 20:19 on 12 May 2004
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});
}

find_or_create enhancement
Branislav Zahradnik 20:19 on 12 May 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52