in reply to Complex Data Structures?
result:
{ package Person; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self, $class); # Add member variables. $self->{Name} = @_ ? shift : die "Person Constructor expec +ted a name"; $self->{Rank} = @_ ? shift : die "Person Constructor expec +ted a rank"; $self->{SerialNumber} = @_ ? shift : die "Person Constructor expec +ted a serial number"; return $self; } sub Name { my $self = shift; return $self->{Name}; } sub SerialNum { my $self = shift; return $self->{SerialNumber}; } # And whatever other methods you want. } my $dad = Person->new( 'dad', 'top dog', 1 ); my %ByName; while( ReadPeopleDataFromSomewhere() ) { my $person = Person->new( $name, $rank, $serial ); # Error check for repeat names? # I didn't, but that might not be a bad idea. $ByName{$person->Name()} = $person; # Likewise, one hopes that serial numbers are unique. $BySerial{$person->SerialNum()} = $person; }
The above sample code is untested but gives you the idea. You should definately read perltoot.
|
|---|