You can use your constructor for creation a new object and define its properties by default (include getting dbh, because I think you will store new tickets in the database):
our $properties = { time => undef, trad_ref => undef, adr_buyer_bank => 'test', adr_seller_bank => undef, seller_pays => undef, }; sub new { my $class = shift; my $self = {}; map { $self->{$_} = $properties->{$_} } keys %$properties; bless $self, $class; $self->_init(@_); return $self; } sub _init { my $self = shift; if(@_) { my %args = @_; map { $self->{$_} = $args{$_} if exists $self->{$_} } keys %ar +gs; } $self->{__dbh} = DBI->connection(...) unless defined $self->{__dbh +}; } sub load { my $self = shift; 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->_init(%$row); return 1; }
Use its in the script or another module:
my $ticket = MyApp::Ticket->new();
or using input parameters:
my $ticket = MyApp::Ticket->new(time => time(), trad_ref => 34567, adr_buyer_bank => 'blah', adr_seller_bank => 'blah, seller_pays => 99.99);
For load existing ticket use method load, as you wrote:
my $ticket = MyApp::Ticket->new(); my $res = $ticket->load(12345);
It's just rough but idea is you need only one constructor and others are methods. Of course, I didn't implement errors catch but it's another story.
      
--------------------------------
SV* sv_bless(SV* sv, HV* stash);

In reply to Re: how do i implement alternate constructors for an object by nite_man
in thread how do i implement alternate constructors for an object by thunders

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.