On 08/09/2006 05:36 PM,
wrote:
> 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();
I got your program to work, but I had to assign $color_set to
$chash before the line below.
> $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.
The code you posted pretty much should work so long as $chash
is set to something meaningful. You have an annoying warning
that I'll show you how to get rid of.
> 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} };
The line above produces an annoying warning; the line uses
$self->{MY_COLORS}, which contains only *one* value, a scalar
reference to a hash, as the data for an anonymous hash
(denoted by { ... }); hashes with an uneven number of elements
are usually messed up.
I don't know why you're creating an anonymous hash then
returning it; you already have a much better hash to return in
$self->{MY_COLORS}, so why not just return it? Perhaps you
were thinking of the {...} as a block. Anyway, change it:
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};
> }
>
A. Sinan Unur showed you how to use some perl modules to do
this differently, and I think I'll do that too:
package My::Chash2;
use Class::Struct;
struct 'My::Chash2' => [ my_colors => '%' ];
sub color_code {
my $self = shift;
$self->my_colors(shift());
}
package main;
my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
my $chash = My::Chash2->new();
$chash->my_colors($composite_colors);
print $chash->color_code('blue'), "\n";
__HTH__