Although you can mix database fields and other parameters into a single hash, my first impression would be to avoid this and simply have an attribute in your object that contains a reference to the hash that contains the database fields.

Though, exactly how to do this all depends on how you are getting the external hash into your constructor. The two common methods are "pass by value" and "pass by reference":

# Pass by value: $obj= My::Class->new( %hash ); $obj= My::Class=>new( key=>"value", key2=>$value2 ); # vs. pass by reference: $obj= My::Class->new( \%hash ); $obj= My::Class=>new( { key=>"value", key2=>$value2 } );
I prefer "pass by reference" because it leaves more room for API adjustments down the road, makes miscalling easier to detect, and few other reasons.

So you could write a constructor something like this:

sub new { my( $class, $hashRef )= @_; my $self= bless $hashRef, $class; return $self; }
but that could cause problems because it makes an object out of the calling code's hash directly. So, for example,
my %hash= ( key=>"value", key2=>$value2 ); my $obj1= My::Class->new( \%hash ); $hash{key}= "cheap"; # This changes $obj1! my $obj2= My::Class->new( \%hash );
would modify $obj1 and then make $obj2 an alias to $obj1 (so you'd really only have one object).

So you'd want to copy the values (usually doing some validation, but it doesn't sound like you want to any of that):

sub new { my( $class, $hashRef )= @_; my $self= bless {%$hashRef}, $class; return $self; } # or sub new { my( $class, $hashRef )= @_; my $self= bless {}, $class; @{$self}{keys %$hashRef}= values %$hashRef; return $self; } # or sub new { my( $class, $hashRef )= @_; my $self= bless {}, $class; %$self= %$hashRef; return $self; } # etc.
To show just a couple of the ways to do it.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: OO Perl and Design Issues by tye
in thread OO Perl and Design Issues by jonjacobmoon

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.