B
Brett.R.Davis
I am writing my first Perl Class and have hit a brick wall.
What I need to do is have a class method copy a hash into the data
member %MY_HASH.
I have coded a constructor and a method my_hash which is *supposed* to
either:
1. return the HASH reference or
2. write a copy the contents of a hash into the MY_COLORS data member.
Then I wanted a second data member that uppon being written to with a
symbolic name, would use the MY_COLORS hash to provide the numberical
code for that COLOR as shown:
my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
my $color_set=Chash->new();
$chash->my_colors($composite_colors);
$chash->color_code('blue');
print $chash{'color_code'};
The output should be:
2
However, I am never able to get the object $chash to take in the
$composite_colors reference, and copy it into the data member
MY_COLORS. I get comments like "odd number of elements in anonymous
hash". If I data dump the contents of the data member MY_COLORS it
shows that the $composite_colors hash is being copied in as a key to
the has instead of essentially becoming the MY_COLORS hash.
Any assistance would be greatly appreciated!!!!
Brett
Here is the code.
package Chash;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
$self->{MY_COLORS} = {};
$self->{COLOR_CODE} = undef;
bless($self, $class);
return $self;
}
sub my_colors {
my $self = shift;
if (@_) { $self->{MY_COLORS} = $_[0] }
return { $self->{MY_COLORS} };
}
sub color_code {
my $self = shift;
if (@_) {
if (ref $self->{MY_COLORS} eq "HASH") {
$self->{COLOR_CODE} = $self->{MY_COLORS}->{$_[0]};
}
else {
$self->{COLOR_CODE} = shift;
}
}
return $self->{COLOR_CODE};
}
What I need to do is have a class method copy a hash into the data
member %MY_HASH.
I have coded a constructor and a method my_hash which is *supposed* to
either:
1. return the HASH reference or
2. write a copy the contents of a hash into the MY_COLORS data member.
Then I wanted a second data member that uppon being written to with a
symbolic name, would use the MY_COLORS hash to provide the numberical
code for that COLOR as shown:
my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
my $color_set=Chash->new();
$chash->my_colors($composite_colors);
$chash->color_code('blue');
print $chash{'color_code'};
The output should be:
2
However, I am never able to get the object $chash to take in the
$composite_colors reference, and copy it into the data member
MY_COLORS. I get comments like "odd number of elements in anonymous
hash". If I data dump the contents of the data member MY_COLORS it
shows that the $composite_colors hash is being copied in as a key to
the has instead of essentially becoming the MY_COLORS hash.
Any assistance would be greatly appreciated!!!!
Brett
Here is the code.
package Chash;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
$self->{MY_COLORS} = {};
$self->{COLOR_CODE} = undef;
bless($self, $class);
return $self;
}
sub my_colors {
my $self = shift;
if (@_) { $self->{MY_COLORS} = $_[0] }
return { $self->{MY_COLORS} };
}
sub color_code {
my $self = shift;
if (@_) {
if (ref $self->{MY_COLORS} eq "HASH") {
$self->{COLOR_CODE} = $self->{MY_COLORS}->{$_[0]};
}
else {
$self->{COLOR_CODE} = shift;
}
}
return $self->{COLOR_CODE};
}