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