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

Re: [MacPerl] objectification



Hi, Nick

As far as retrieval goes, based on your

	$object->retrieve($type, $key)

prototype, here is an illustration:

# script.plx

#!perl -w

use Character;

$me = new Character;

 my $history = $me->retrieve('history');
 foreach $i (reverse sort keys %$history) {
	  print $me->retrieve('history', $i), "\n";
 }
__END__

and

# Character class
package Character;

%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 ($self, $field, $key) = @_;
	 if (defined $key) {
     	return $self->{$field}->{$key};
	} else {
		return $self->{$field};
	}
}

1;

I haven't bothered with error-checking, as to whether a key exists for
example.

I think you can take things from there. Also, and I realize I don't know
the problem description, it sort of bothers me that you're using objects
to access what looks very much like static or class data, namely the
associative array %f. If this data structure is common to all objects in
your design, then it ought not be part of an object instance - it's a
class variable (package variable in Perl).

What I'm getting at is this: you haven't demonstrated that you need to use
OOP at all; you can drop the new() method and just work using package
methods. Does this make sense? :-)

Just some thoughts. Arved




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