in reply to Re: (Ovid) Re: OO Perl and Design Issues
in thread OO Perl and Design Issues

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")

Replies are listed 'Best First'.
Re: (tye)Re: OO Perl and Design Issues
by jonjacobmoon (Pilgrim) on Jan 01, 2002 at 05:20 UTC
    (First, I made a similiar post already but it seems to have not made it to through the gates of the monastery. I apologize if it shows up twice.) You make a very good point about the dangers here, and I will certainly implement your ideas. However, I am a little stumped how the constructors you listed would prevent the problem. Can you elaborate? For instance, how does: my $self= bless {%$hashRef}, $class; protect the objects from being changed in the way you describe. One thing I did was make %_permitted be a hash that is created from the table data as well, so only those columns in the table can be in the object and not anything extra, so that you if you add to that hash randomly the hash will have the value but the object will not allow it. So, if it is not in the _permitted hash then it is not part of the object. Does that address the issue in part, or did I misunderstand?