[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] objectification



short cap:
dealing with objects I'm getting a referance rather than a value (I think). Help! :)

long cap:
Lately for a program I'm working on I decidced to use objects for one of the modules (to simplify changes in how the data is accessed. ie a nice black box). Despite the fact that noone seems to want to help in the making of an object module (asside from rehashing the subchapter from the camel book) I've managed to get it working for the most part. But I'm having a problem which I vaguely know what is but not entirely sure. Here's some snippits of code/pseudocode, the question follows...
###the module###
package Charecter;
%f = (
     history => {
               '947035791' => 'string1,
               '947035790' => 'string2,
               '947015631' => 'string3',
               ...
     }
     ...
);
sub new {
     my $that  = shift;
     my $class = ref($that) || $that;
     my $self  = { %f };
     return bless $self, $class;
}
sub retrieve {
     my ($that, $type, @field, $field, $output);
     $that  = shift;
     $type  = ref($that) || die "$that is not an object";
     @field = @_;
     $field = @field;
     #snip
     if ($field == 2) {
          unless (exists $that->{$field[0]}->{$field[1]} ) {
               die "Can't access '$field[0] $field[1]' field in object of class $type"; }
          $output = $that->{$field[0]}->{$field[1]};
     }
     #snip
     return $output;
}
###the code in question###
$me = new Charecter;
#snip
{
	     my %history = $me->retrieve('history');
	     foreach $i (reverse sort keys %history) {
	          $time = &Time::get_time('text', $i);     ###you can ignore this
	          $time =~ s/.*day, //;                                ###and this
	          $OUT .= '<TR><TD bgcolor="#333333">'.
	               $time .
	               '</TD><TD bgcolor="#111111">'.
	               $me->retrieve('history', $i);         ###the error in question
	     }
}
__END__

The problem I'm having is with the last line above (not including the "}"). I've tried different things, each garnering different errors; but what it comes down to (I belive) is that $i is  a reference rather than a key: 'HASH(0x8be02c)' vs '947035791', or suchlike.

So the question is how can I fix this, or in some other way do this? (this being the code loop). Can I do this? I'm considering/open to making a new method to do it rather than having it in the code. The keys for $me->{history}->{keys_go_here} are all numbers of roughly the same sise as noted. (actually seconds since epoch so everlarger, but still). Before I switched to using an object rather than standard hashes and suchlike the code worked (minor alterantions such as the "my %history" line wasn't there and the last line was "$history{$i};". Any help is appreciated.

le meas,
--Nick

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org