Perl module that accepts list and creates objects

  • A+
Category:Languages

I am working on an college problem ( in Perl ). We are working on creating modules and I need to write a simple module that "has get/set methods for four attributes: lastname, firstname, full_name and a list of children who are also person objects".

I think I have it down but it's the list of children who are also person objects that throws me. I guess the module needs to accept a list and then create a list of objects? Python is my core language so this one is throwing me. The get/set methods are working fine. Any ideas?

My module is here...

#!/usr/bin/perl  package Person;  sub new {     my $class = shift;     my $self = {         _firstName => shift,         _lastName  => shift,     };     bless $self, $class;     return $self; } sub setFirstName {     my ( $self, $firstName ) = @_;     $self->{_firstName} = $firstName if defined($firstName);     return $self->{_firstName}; }  sub getFirstName {     my( $self ) = @_;     return $self->{_firstName}; }  sub setLastName {     my ( $self, $lastName ) = @_;     $self->{_lastName} = $lastName if defined($lastName);     return $self->{_lastName}; }  sub getLastName {     my( $self ) = @_;     return $self->{_lastName}; }  sub getFullName {     my( $self ) = @_;     return $self->{_lastName}.",".$self->{_firstName}; }  1; 

My code is here.....

#!/usr/bin/perl  use Person;  $object = new Person("Elvis","Presley"); # Get first name which is set using constructor. $firstName = $object->getFirstName(); $lastName = $object->getLastName(); $fullname = $object->getFullName();  print "(Getting) First Name is : $firstName/n"; print "(Getting) Last Name is: $lastName/n"; print "(Getting) Full Name is: $fullname/n"; 

 


Just use a list of objects in the setter:

#! /usr/bin/perl use warnings; use strict; use feature qw{ say };  {   package Person;      sub new {         my $class = shift;         my $self = {             _firstName => shift,             _lastName  => shift,             _children => [],         };         return bless $self, $class     }      sub setFirstName {         my ($self, $firstName) = @_;         $self->{_firstName} = $firstName if defined $firstName;         return $self->{_firstName}     }      sub getFirstName {         my ($self) = @_;         return $self->{_firstName}     }      sub setLastName {         my ($self, $lastName) = @_;         $self->{_lastName} = $lastName if defined $lastName;         return $self->{_lastName}     }      sub getLastName {         my ($self) = @_;         return $self->{_lastName}     }      sub getFullName {         my ($self) = @_;         return $self->{_lastName} . ', ' . $self->{_firstName}     }      sub getChildren {         my ($self) = @_;         return @{ $self->{_children} }     }      sub setChildren {         my ($self, @children) = @_;         $self->{_children} = [ @children ];     }  }  my $object = 'Person'->new('Elvis', 'Presley');  # Get first name which is set using constructor. my $firstName = $object->getFirstName; my $lastName = $object->getLastName; my $fullname = $object->getFullName;  $object->setChildren('Person'->new('Lisa', 'Presley'),                      'Person'->new('Deborah', 'Presley'));  say "(Getting) First Name is: $firstName"; say "(Getting) Last Name is: $lastName"; say "(Getting) Full Name is: $fullname";  say "Children: "; say $_->getFullName for $object->getChildren; 

Note that there are modules to make building objects easier, e.g. Moo:

#! /usr/bin/perl use warnings; use strict; use feature qw{ say };  {   package Person;     use Moo;      has first_name => (is => 'ro');     has last_name => (is => 'ro');     has full_name => (is => 'lazy');     has _children => (is => 'ro',                       init_arg => undef,                       default => sub { [] });      sub _build_full_name {         my ($self) = @_;         return $self->last_name . ', ' . $self->first_name     }      sub add_child {         my ($self, $child) = @_;         push @{ $self->_children }, $child     }      sub children {         my ($self) = @_;         return @{ $self->_children }     }  }  my $object = 'Person'->new(first_name => 'Elvis',                            last_name  => 'Presley');  # Get first name which is set using constructor. my $firstName = $object->first_name; my $lastName = $object->last_name; my $fullname = $object->full_name;  $object->add_child($_) for 'Person'->new(first_name => 'Lisa',                                          last_name => 'Presley'),                            'Person'->new(first_name => 'Deborah',                                          last_name => 'Presley');  say "(Getting) First Name is: $firstName"; say "(Getting) Last Name is: $lastName"; say "(Getting) Full Name is: $fullname";  say "Children: "; say $_->full_name for $object->children; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: