Perl module that accepts list and creates objects

create objects in perl
how to print an object in perl
objects and class in perl
perl return object
object reference perl
perl object hash attribute
perl object oriented global variable
perl calling module functions

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;

oop - Perl module that accepts list and creates objects, 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  In Perl OO, this is the method invocant -- typically a class name for constructors and an object for other methods. Hashes flatten to and can be initialized by lists. It's a common trick to emulate named arguments to subroutines. e.g.

Object::Accessor, $obj = Object::Accessor->new( @list ); # create object with accessors its accessors on your own module, or to store an Object::Accessor object inside your own object, The validators can be anything that Params::Check 's allow function accepts. Note that lvalue support on AUTOLOAD routines is a perl 5.8.​x feature. To understand this tutorial, you should be familiar with using subroutines and data structures in Perl.. An Introduction to Objects. A subroutine encapsulates some behavior into a named container, allowing you to think of it in terms of what inputs it expects and what outputs it returns.

Object::InsideOut, Object::InsideOut - Comprehensive inside-out object support module List field. # With standard 'get_X' and 'set_X' accessors. # Takes 'INFO' as an optional you to sub-class other Perl class, and access their methods from your own objects. The newly created object and this parameter hash ref are then sent to the :Init  The explanation of the module. A module in Perl is a namespace in the file corresponding to that namespace. The package keyword creates the namespace. A module name My::Math maps to the file My/Math.pm. A module name A::B::C maps to the file A/B/C.pm somewhere in the directories listed in @INC.

perlobj, This document provides a reference for Perl's object orientation features. That explicit association is created by the built-in bless function, which is This can be done in a variety of ways, including using Inside-Out objects or modules from CPAN. The @ISA array contains a list of that class's parent classes, if any. H ow can I display a list of all installed CPAN / Perl modules under Unix like operating systems? There are various ways to list all installed Perl / CPAN modules under Unix, Linux, and MS-Windows operating systems.

Day 19 -- Object-Oriented Programming in Perl, Classes in Perl; Creating a Class; Blessing a Constructor A module is a package, and a package is a class for all practical purposes. The bless() function takes that reference to $this, tells the object it references that it's now a Cocoa, and You can also use an array instead of a hash to store the instance variables. It means a method call can behave differently depending on the type of the object that calls it. Perl OOP rules. There are three important rules in Perl object oriented programming: A class is a package. An object is a reference that knows its class. A method is a subroutine. Defining the first Perl class. We are going to define the Product class in Perl.

[PDF] Perl 4: Applied Perl, create reusable modules in object-oriented Perl. Learning takes time. Context: scalar and list, and the use of scalar expressions in numeric or string contexts  The BEGIN and END blocks are particularly useful when creating Perl modules. What are Perl Modules? A Perl module is a reusable package defined in a library file whose name is the same as the name of the package (with a .pm on the end). A Perl module file called "Foo.pm" might contain statements like this.

Comments
  • "I guess the module needs to accept a list and then create a list of objects?" Well if you pass it a list of Person objects then there's nothing to create. Your object needs another field, _children which is an array. You can initialise it with an empty array in the constructor _children => [] and add to it using another setter. You would create a number of children in the same way as you created $object and assign them with something like $object->setChildren($kid1, $kid2, $kid3)