"Perl for Perl Newbies" - Part 5 - Good Programming PracticesClass AccessorsExample

4.1. Example

Here's an example class with some accessors and a script that uses it:

package Person;

use strict;
use warnings;

sub new
{
    my $class = shift;

    my $self = {};
    bless $self, $class;

    $self->_init(@_);

    return $self;
}

sub _init
{
    my $self = shift;
    my $args = shift;

    $self->_first_name($args->{'first_name'});
    $self->_last_name($args->{'last_name'});

    $self->_age($args->{'age'});

    return;
}

sub _first_name
{
    my $self = shift;

    if (@_)
    {
        my $new_first_name = shift;
        $self->{'_first_name'} = $new_first_name;
    }

    return $self->{'_first_name'};
}

sub _last_name
{
    my $self = shift;

    if (@_)
    {
        my $new_last_name = shift;
        $self->{'_last_name'} = $new_last_name;
    }

    return $self->{'_last_name'};
}

sub _age
{
    my $self = shift;

    if (@_)
    {
        my $new_age = shift;
        $self->{'_age'} = $new_age;
    }

    return $self->{'_age'};
}

sub greet
{
    my $self = shift;

    print "Hello ", $self->_first_name(), " ", $self->_last_name(), "\n";

    return;
}

sub increment_age
{
    my $self = shift;

    $self->_age($self->_age()+1);

    return;
}

sub get_age
{
    my $self = shift;

    return $self->_age();
}

1;
#!/usr/bin/perl

use strict;
use warnings;

use Person;

my $shlomif =
    Person->new(
        {
            first_name => "Shlomi",
            last_name => "Fish",
            age => 32,
        }
    );

$shlomif->greet();
$shlomif->increment_age();

print "Happy Birthday, Shlomi, your age is now ", $shlomif->get_age(), ".\n";

my $newton =
    Person->new(
        {
            first_name => "Isaac",
            last_name => "Newton",
            age => 366,
        }
    );

$newton->greet();
print "Newton would have been ", $newton->get_age(),
    " years old today if he had been alive.\n"
    ;

Written by Shlomi Fish