http://qs1969.pair.com?node_id=256304


in reply to how do i implement alternate constructors for an object

You could certainly implement an inheritance heirarchy to make the code cleaner. However, a simpler solution might be to pass the db handle and ticket number as args, check for their existence, and have the 'new' constructor call 'load' with them as arguments if they are:
package MyApp::Ticket; use strict; my @_args = qw( time trade_ref adr_buyer_bank adr_seller_bank seller_p +ays ); my @_atts = map { '_' . $_ } @_args; sub new { my ($class, %args) = @_; my $self = bless {}, $class; # if constructor has been passed a db handle and a ticket number, # get attributes from database. otherwise, get them from args my ($dbh, $ticket) = @args{ qw( dbh ticket ) }; if( $dbh && $ticket ) { $self->load( $dbh, $ticket ); } else { @{$self}{ @_atts } = @args{ @_args }; } return $self; } sub load { my ($self, $dbh, $ticket_number) = @_; my $sql = "SELECT * FROM tickets WHERE id = ?"; my $sth = $dbh->prepare($sql) or die $dbh->errstr; $sth->execute($ticket_number); my $row = $sth->fetchrow_hashref; @{$self}{ @_atts } = @row{ @_args }; }