Skip Menu |

This queue is for tickets about the Class-DBI CPAN distribution.

Report information
The Basics
Id: 13306
Status: stalled
Worked: 2 min
Priority: 0/
Queue: Class-DBI

People
Owner: Nobody in particular
Requestors: brano [...] blueorange.sk
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



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}); }
Show quoted text
> 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}); > }
Hi, Thanks for this, but I don't understand it. In the code it looks like $default is a hashref, but in your example it looks like it's a boolean. If you were to put together a test case (and ideally some docs) as well, it might help. Thanks, Tony