Here's an OO implementation that I consider more readable --
use strict;
my $barry = new Person ("Barry", 67);
$barry->AddKid(
new Person("Jenny",38,"BA") ,
new Person("Brendan",20,"student")
);
print "--- $barry->{NAME} 's " .
$barry->getKidCount . " children ---\n";
for my $kid(@{$barry->{CHILDREN}}){
print $kid->{$_} ."\t" for qw[NAME AGE EDUCATION];
print "-----------\n";
}
#-----------------------------
INIT{
package Person;
use strict;
sub new{
my ($class,$name,$age,$edu) = @_;
my $self= bless {
NAME=>$name||"Anonymous", AGE=>$age||0,
CHILDREN =>[], EDUCATION=>$edu || "",
}, $class;
return $self;
}
sub AddKid{
my $self = shift;
push @ {$self->{CHILDREN} }, @_;
}
sub getKidCount{
my $self = shift;
return scalar @{$self->{CHILDREN}};
}
1
}
Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax
|