Kraythorne has asked for the wisdom of the Perl Monks concerning the following question:

I have a HASH where each key contains an Object, but when I run loop to update the contents of the object it tries to call the method from the different object to the one I'm updating (hope my object oriented teminology is correct). For example:

the Hash is structured:
'file'<br> DT_File=HASH(0x3211db4)<br> 'Dedupe_level' => undef<br> 'Delimiter' => ','<br> 'Display_Record' => ARRAY(0x185becc)<br> empty array<br> 'Header' => 1<br> 'Header_Record' => ARRAY(0x185bc80)<br> empty array<br> 'Name' => 'current'<br> 'Record' => 1<br> <br> 'Salutation'<br> Salutation=HASH(0x3212924)<br> 'Dear' => 1<br> 'Fullname1' => ''<br> 'Fullname2' => ''<br> 'Initial1' => ''<br> 'Initial2' => ''<br> 'Sal_file' => 'current'<br> 'Surname1' => ''<br> 'Surname2' => ''<br> 'Title1' => ''<br> 'Title2' => ''<br>
where 'file' and 'Salutation' are the HASH keys and 'DT_File' and 'Salutation' are the objects

The code below cycles through the methods for each object and updates it with a new value from a master object. The problem I have is that the first loop which updates elements for the 'DT_File' object, errors saying that it cannot find the method in the 'Salutation' object?
#@file_parameters contains object method names foreach my $param (@file_parameters){<br> $current{'file'} -> $param($source_file{$key}->$param);<br> }
can anyone tell me why the code thinks I am looking for the method in the 'Salutation' object rather than the 'DT_File' object?

Thanks

Replies are listed 'Best First'.
Re: Object selection query
by Anonymous Monk on Jul 21, 2009 at 10:23 UTC
    Because you are
    $current{'file'}->$param( $source_file{$key}->$param ); $current{'file'}->$param( $source_file{$key}->$param ); $current{'file'}->$param( $source_file{$key}->$param() ); $source_file{$key}->$param();

      Anonymous Monk is right about the method calls - i am no OOP expert, but this seems a really nasty way to structure your objects and control method calls to them. The whole point of objects is that they look after themselves. Maybe OP could update each class with an 'update' method, which only takes the information it needs, say from a hash like the ones like you describe?

      Just a something something...
        Oh Dear, looks like I'm not doing too well with my first stab at OOP.

        The methods I'm using in this code are predominantly written so that the method can be used to set or call the value depending on whether you pass it any values:
        sub method{ my ($self) = shift; if (@_){ $self->{MethodName} = shift; } else{ ref $self ? $self->{MethodName} : "An un-named " . $self; } }
        Although I've not been doing this long enough to decide if this is the best way to skin this particular cat

        I'll put in an update method and see how that goes - I guess I just need more practice and direction to get it right. I'm not finding OOP too easy at the moment, need to read a few more books on it me thinks.

        Thanks for the feedback!