I'd opt for polymorphic constructors. You're initializing the same fields two different ways, from two different sources. Let your constructor examine the object being passed in; if it's a database handle, use that, and if not, see if it has the fields you want.
Here's a simple sketch:
HTHpackage MyApp::Ticket; sub new { my ($class, $from, $extra) = @_; if (UNIVERSAL::isa($from, 'DBI')) # it's a DB handle { my $sth = $from->prepare(...); $sth->execute($extra); # and so on } else { $self->{_time} = $from->{time}; # etc. } bless $self, $class; } # Called like so: my $dbh = DBI->connect(...); my $dbticket = MyApp::Ticket->new($dbh, $ticketno); # or like so: my %args; my $ticket = MyApp::Ticket->new(\%args);
Update
Better to use isa() instead of can().
In reply to Re: how do i implement alternate constructors for an object
by VSarkiss
in thread how do i implement alternate constructors for an object
by thunders
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |